-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe9.cpp
93 lines (79 loc) · 2.17 KB
/
e9.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
//
// Created by martin on 10/22/22.
//
#include "../std_lib.h"
template < typename T >
T from_string (const string &s)
{
istringstream iss { s };
T t;
if (!( iss >> t ))
{
throw runtime_error("from_string error");
}
return t;
}
int main (int argc, char *argv[])
{
ifstream ifs { "../c23/table.txt" };
if (!ifs)
{
throw runtime_error("input file not found");
}
string line;
int line_no { 0 };
regex header { R"(^[\w ]+(\t[\w ]+)*$)" };
regex row { R"(^([\w ]+)\t(\d+)\t(\d+)\t(\d+)$)" };
if (getline(ifs, line))
{
++line_no;
smatch matches;
if (!regex_match(line, matches, header))
{
throw runtime_error("no header found");
}
}
auto boys { 0 };
auto girls { 0 };
while (getline(ifs, line))
{
++line_no;
smatch matches;
if (!regex_match(line, matches, row))
{
throw runtime_error("bad line: " + to_string(line_no));
}
auto current_boy { from_string<int>(matches[2]) };
auto current_girl { from_string<int>(matches[3]) };
auto current_total { from_string<int>(matches[4]) };
if (current_boy + current_girl != current_total)
{
throw runtime_error("bad row sum");
}
if (matches[1] == "Alle klasser")
{
if (current_boy != boys)
{
throw runtime_error("boys don't add up");
}
if (current_girl != girls)
{
throw runtime_error("girls don't add up");
}
if (!( ifs >> ws ).eof())
{
throw runtime_error("characters after total line");
}
}
else
{
boys += current_boy;
girls += current_girl;
}
}
cout << "total boys: " << boys << endl;
cout << "total girls: " << girls << endl;
cout << "total students: " << boys + girls << endl;
cout << "eof at line: " << line_no << endl;
return EXIT_SUCCESS;
}