-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
83 lines (72 loc) · 2.02 KB
/
main.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
#include "database.h"
#include "date.h"
#include "condition_parser.h"
#include "node.h"
#include <iostream>
#include <sstream>
#include <stdexcept>
using namespace std;
string ParseEvent(istream& is)
{
string event_;
if (is.peek() == ' ')
{
is.ignore(1);
while (is.peek() == ' ')
{
is.ignore(1);
}
}
getline(is, event_, '\n');
return event_;
}
int main() {
Database db;
for (string line; getline(cin, line); ) {
istringstream is(line);
string command;
is >> command;
if (command == "Add") {
const auto date = ParseDate(is);
const auto event = ParseEvent(is);
db.Add(date, event);
}
else if (command == "Print") {
db.Print(cout);
}
else if (command == "Del") {
auto condition = ParseCondition(is);
auto predicate = [condition](const Date& date, const string& event) {
return condition->Evaluate(date, event);
};
int count = db.RemoveIf(predicate);
cout << "Removed " << count << " entries" << endl;
}
else if (command == "Find") {
auto condition = ParseCondition(is);
auto predicate = [condition](const Date& date, const string& event) {
return condition->Evaluate(date, event);
};
const auto entries = db.FindIf(predicate);
for (const auto& entry : entries) {
cout << entry << endl;
}
cout << "Found " << entries.size() << " entries" << endl;
}
else if (command == "Last") {
try {
cout << db.Last(ParseDate(is)) << endl;
}
catch (invalid_argument&) {
cout << "No entries" << endl;
}
}
else if (command.empty()) {
continue;
}
else {
throw logic_error("Unknown command: " + command);
}
}
return 0;
}