-
Notifications
You must be signed in to change notification settings - Fork 0
/
grader.h
139 lines (124 loc) · 3.4 KB
/
grader.h
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#ifndef GRADER_H
#define GRADER_H
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
std::stringstream nextLine(std::istream& is) {
std::string temp;
std::getline(is, temp);
std::stringstream os(temp);
return os;
}
bool isNumber(std::string& str) {
std::string::const_iterator it = str.begin();
if(!isdigit(*it) && *it != '-')
return false;
for(;it != str.end(); it++) {
if(!isdigit(*it))
return false;
}
return true;
}
struct Question {
std::string ans;
int pts;
int tiebreaker_order;
};
class tieFunctor {
private:
int rank;
public:
tieFunctor(int in) {
rank = in;
}
bool operator()(Question q) {
return q.tiebreaker_order == rank;
}
};
class Score {
private:
std::string team_name;
int team_num;
std::vector<int> points;
int total_points;
std::vector<Question> questions;
public:
Score(void) { //ctor
team_name = "NONE";
team_num = -1;
total_points = -1;
time_bonus = 0;
}
~Score(void) { //dtor
points.clear();
}
bool operator<(const Score& other_team) const {
if(total_points == other_team.total_points) {
size_t tie_number = 1;
while(tie_number <= questions.size()) {
tieFunctor pred(tie_number);
int pos = std::distance(questions.begin(), std::find_if(questions.begin(), questions.end(), pred));
if(points[pos] == other_team.points[pos]) {
tie_number++;
}
else {
return points[pos] > other_team.points[pos];
}
}
}
return total_points > other_team.total_points;
//TODO: Logic for tiebreakers
}
void scoreAns(Question key, std::string ans) {
if(isNumber(key.ans)) {
int penalty = 0;
if(key.ans != ans)
penalty = key.pts;
points.push_back(key.pts - penalty);
}
else {
int errors = -2;
for(size_t i = 0; i < key.ans.length(); i++) {
if(key.ans[i] != ans[i])
errors++;
}
if(errors <= 0) {
points.push_back(key.pts);
}
else {
int penalty = errors*100;
points.push_back(std::max(0, key.pts - penalty));
}
}
}
void sumPoints() {
total_points = std::accumulate(points.begin(), points.end(), 0);
total_points = std::accumulate(points.begin(), points.end(), 0) + time_bonus;
}
void addBonus(const int time) {
time_bonus = 4*(600-time);
}
std::string getTeamName() const {
return team_name;
}
int getTeamNum() const {
return team_num;
}
int getTotalScore() const {
return total_points;
}
void setTeamNum(const int num_in) {
team_num = num_in;
}
void setTeamName(std::string& name_in) {
team_name = name_in;
}
void setQuestions(std::vector<Question> q_in) {
questions = q_in;
}
};
#endif // GRADER_H