-
Notifications
You must be signed in to change notification settings - Fork 0
/
question.cpp
101 lines (96 loc) · 2.43 KB
/
question.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
author: Julie Schneider 3/2020, Updated Fall 2021
Question class implementation definition
*/
#include "question.h"
Question::Question(Record catIn, Record quest, Record ans)
{
setCategory(catIn);
setQuestion(quest);
setAnswer(ans);
resetQuestionStatus();
setPoints(0);
}
std::string Question::toString() const
{
return "\n*****************" + std::to_string(points) + "********************\nCategory: " + category
+ "\nQuestion: " + question + "\n****************************************\n";
}
std::string Question::toFileString() const
{
std::string strCSV = category + "," + question + "," + answer + "\n";
return strCSV;
}
std::istream& operator>>(std::istream& is, Question& q)
{
std::cout << "\n************************Question Entry: \n";
std::cout << "*\tCategory: ";
is >> q.category;
std::cout << "*\tQuestion: ";
is >> q.question;
std::cout << "*\t Answer: ";
is >> q.answer;
std::cout << "*\t Score: ";
is >> q.points;
std::cout << "\n****************************************\n\n";
q.status = false;
return is;
}
std::ifstream& operator>>(std::ifstream& ifs, Question& q)
{
Record record;
ifs >> record;
if (record != "" || record != "\n") {
Record* fields = record.split(',');
if (record.getNumFieldsInRecord() == 4) {
q.category = fields[0];
q.question = fields[1];
q.answer = fields[2];
q.points = std::stoi(fields[3]);
q.status = false;
}
}
return ifs;
}
std::ostream& operator<<(std::ostream& os, const Question& q)
{
os << q.toString();
return os;
}
std::ofstream& operator<<(std::ofstream& ofs, const Question& q)
{
ofs << "" << q.toFileString();
return ofs;
}
bool operator==(const Question& q1, const Question& q2)
{
// Questions must be in the same category to be compared and
// then the points are compared.
if (q1.category == q2.category && q1.points == q2.points
&& q1.answer == q2.answer && q1.question == q2.question)
return true;
return false;
}
bool operator!=(const Question& q1, const Question& q2)
{
return !(q1 == q2);
}
bool operator>=(const Question& q1, const Question& q2)
{
return q1 == q2 || q1 > q2;
}
bool operator<=(const Question& q1, const Question& q2)
{
return q1 == q2 || q1 < q2;
}
bool operator>(const Question& q1, const Question& q2)
{
return !(q1 <= q2);
}
bool operator<(const Question& q1, const Question& q2)
{
// Questions must be in the same category to be compared.
if (q1.category == q2.category && q1.points < q2.points)
return true;
return false;
}