-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexec23-10.cpp
55 lines (45 loc) · 1.45 KB
/
exec23-10.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
#include <fstream>
#include <iostream>
#include <map>
#include <regex>
#include <string>
using namespace std;
struct Row {
int boy = 0, girl = 0, total = 0;
};
// Read a table, and write a new table where the rows with the same initial digit
// (indicating the year: first grades start with 1) are merged.
int main(int argc, char* argv[]) {
if (argc < 3) {
cout << "Usage: " << argv[0] << " input_file output_file\n";
return 0;
}
ifstream ifs(argv[1]);
if (!ifs) {
cerr << "can't open input file " << argv[1] << endl;
return 1;
}
ofstream ofs(argv[2]);
if (!ofs) {
cerr << "can't open output file " << argv[2] << endl;
return 1;
}
regex header(R"(^[\w ]+(\t[\w ]+)*$)"); // header line
regex row(R"(^(\d+)\w+\t(\d+)\t(\d+)\t(\d+)$)"); // data line
string line;
if (getline(ifs, line) && regex_match(line, header))
ofs << line << '\n';
map<int, Row> data; // grade -> (boy, girl, total)
while (getline(ifs, line)) {
smatch matches;
if (!regex_match(line, matches, row))
continue;
int grade = stoi(matches[1]);
data[grade].boy += stoi(matches[2]);
data[grade].girl += stoi(matches[3]);
data[grade].total += stoi(matches[4]);
}
for (const auto& p : data)
ofs << p.first << '\t' << p.second.boy << '\t' << p.second.girl << '\t' << p.second.total << '\n';
return 0;
}