-
Notifications
You must be signed in to change notification settings - Fork 0
/
white_belt_final.cpp
167 lines (153 loc) · 4.38 KB
/
white_belt_final.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <iomanip>
#include <tuple>
#include <set>
#include <map>
#include <stdexcept>
using namespace std;
class Date {
public:
Date() {}
Date(int new_year, int new_month, int new_day) {
year = new_year;
if (new_month < 1 || new_month > 12) {
throw out_of_range("Month value is invalid: "
+ to_string(new_month));
}
month = new_month;
if (new_day < 1 || new_day > 31) {
throw out_of_range("Day value is invalid: "
+ to_string(new_day));
}
day = new_day;
}
explicit Date(const string& date) {
int new_year, new_month, new_day;
bool ok = true;
istringstream date_stream(date);
ok = ok && (date_stream >> new_year);
ok = ok && (date_stream.peek() == '-');
date_stream.ignore(1);
ok = ok && (date_stream >> new_month);
ok = ok && (date_stream.peek() == '-');
date_stream.ignore(1);
ok = ok && (date_stream >> new_day);
ok = ok && date_stream.eof();
if (!ok) {
throw out_of_range("Wrong date format: " + date);
}
*this = Date(new_year, new_month, new_day);
}
int GetYear() const {
return year;
}
int GetMonth() const {
return month;
}
int GetDay() const {
return day;
}
private:
int year, month, day;
};
bool operator<(const Date& lhs, const Date& rhs) {
return make_tuple(lhs.GetYear(), lhs.GetMonth(), lhs.GetDay()) <
make_tuple(rhs.GetYear(), rhs.GetMonth(), rhs.GetDay());
}
istream& operator>>(istream& is, Date& d) {
string date;
is >> date;
d = Date(date);
return is;
}
ostream& operator<<(ostream& os, const Date& d) {
os << setfill('0');
os << setw(4) << d.GetYear() << '-';
os << setw(2) << d.GetMonth() << '-';
os << setw(2) << d.GetDay();
return os;
}
class Database {
public:
Database() {}
void AddEvent(const Date& date, const string& event) {
event_list[date].insert(event);
}
string DeleteEvent(const Date& date, const string& event) {
if (event_list[date].find(event) != event_list[date].end()) {
event_list[date].erase(event);
return "Deleted successfully";
} else {
return "Event not found";
}
}
string DeleteDate(const Date& date) {
size_t event_num = event_list[date].size();
event_list[date].clear();
return "Deleted " + to_string(event_num) + " events";
}
set<string> Find(const Date& date) const {
if (event_list.find(date) != event_list.end()) {
return event_list.at(date);
}
set<string> result;
return result;
}
void Print() const {
for (const auto& item : event_list) {
if (item.second.size() != 0) {
for (const auto& event : item.second) {
cout << item.first << " " << event << endl;
}
}
}
}
private:
map<Date, set<string>> event_list;
};
ostream& operator<<(ostream& os, const set<string>& event_list) {
for (const auto& event : event_list) {
cout << event << endl;
}
return os;
}
int main() {
Database db;
Date date;
string event;
string command;
while (getline(cin, command)) {
try {
string request;
stringstream cmd(command);
cmd >> request;
if (request == "Add") {
cmd >> date >> event;
db.AddEvent(date, event);
}
else if (request == "Del") {
cmd >> date;
if (cmd.peek() == ' ') {
cmd >> event;
cout << db.DeleteEvent(date, event) << endl;
} else {
cout << db.DeleteDate(date) << endl;
}
}
else if (request == "Find") {
cmd >> date;
cout << db.Find(date);
}
else if (request == "Print") {
db.Print();
} else if (!request.empty()) {
cout << "Unknown command: " << request << endl;
return 1;
}
} catch (const exception& ex) {
cout << ex.what() << endl;
return 1;
}
}
return 0;
}