-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathverify_table.cpp
70 lines (57 loc) · 2 KB
/
verify_table.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
#include <fstream>
#include <iostream>
#include <regex>
#include <stdexcept>
#include <string>
using namespace std;
void error(const string& msg) {
throw runtime_error(msg);
}
// Verify that this table is properly laid out (i.e., every row has the right number of fields)
// Verify that the numbers add up (the last line claims to be the sum of the columns above)
int main(int argc, char* argv[]) {
if (argc < 2) {
cout << "Usage: " << argv[0] << " filename\n";
return 0;
}
try {
ifstream in(argv[1]);
if (!in) error("no input file");
string line; // input buffer
int lineno = 0;
regex header(R"(^[\w ]+(\t[\w ]+)*$)"); // header line
regex row(R"(^([\w ]+)(\t\d+)(\t\d+)(\t\d+)$)"); // data line
if (getline(in, line)) { // check header line
if (!regex_match(line, header))
error("no header");
}
// column totals:
int boys = 0, girls = 0;
while (getline(in, line)) {
++lineno;
smatch matches;
if (!regex_match(line, matches, row))
error("bad line: " + to_string(lineno));
if (in.eof()) cout << "at eof\n";
// check row:
int curr_boy = stoi(matches[2]);
int curr_girl = stoi(matches[3]);
int curr_total = stoi(matches[4]);
if (curr_boy + curr_girl != curr_total) error("bad row sum");
if (matches[1] == "Alle klasser") { // last line
if (curr_boy != boys) error("boys don't add up");
if (curr_girl != girls) error("girls don't add up");
if (!(in >> ws).eof()) error("characters after total line");
return 0;
}
// update totals:
boys += curr_boy;
girls += curr_girl;
}
error("didn't find total line");
}
catch (runtime_error& e) {
cerr << e.what() << '\n';
return 1;
}
}