-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
235 lines (192 loc) · 6.07 KB
/
main.cpp
File metadata and controls
235 lines (192 loc) · 6.07 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
using namespace std;
string COMMAND_DEL = "Del";
string COMMAND_FIND = "Find";
string COMMAND_ADD = "Add";
string COMMAND_PRINT = "Print";
//#region DATE
void RunCommand(stringstream &stream, string command) {
stream << command << endl;
}
class Date {
public:
Date(int c_year, int c_month, int c_day) {
year = abs(c_year);
if (c_month < 1 || c_month > 12) {
throw logic_error("Month value is invalid: " + to_string(c_month));
}
month = c_month;
if (c_day < 1 || c_day > 31) {
throw logic_error("Day value is invalid: " + to_string(c_day));
}
day = c_day;
}
int GetYear() const {
return year;
}
int GetMonth() const {
return month;
}
int GetDay() const {
return day;
}
private:
int year = 0;
int month = 0;
int day = 0;
};
bool operator<(const Date &d1, const Date &d2) {
return vector<int>{d1.GetYear(), d1.GetMonth(), d1.GetDay()} <
vector<int>{d2.GetYear(), d2.GetMonth(), d2.GetDay()};
}
ostream &operator<<(ostream &stream, const Date &date) {
stream << setw(4) << setfill('0') << date.GetYear() << "-"
<< setw(2) << setfill('0') << date.GetMonth() << "-"
<< setw(2) << setfill('0') << date.GetDay();
return stream;
}
void EnsureNextCharAndSkip(stringstream &stream, const string &date) {
if (stream.peek() != '-') {
throw logic_error("Wrong date format: " + date);
}
stream.ignore(1);
}
Date ParseDate(const string &date) {
stringstream date_stream(date);
int year, month, day;
date_stream >> year;
EnsureNextCharAndSkip(date_stream, date);
date_stream >> month;
EnsureNextCharAndSkip(date_stream, date);
date_stream >> day;
if (!date_stream.eof() || !date_stream) {
throw logic_error("Wrong date format: " + date);
}
return {year, month, day};
}
//#endregion
class Database {
public:
void Add(const Date &date, const string &event) {
events[date].insert(event);
}
set<string> Find(const Date &date) const {
if (events.count(date) > 0) {
return events.at(date);
} else {
return {};
}
}
bool Remove(const Date &date, const string &s) {
if (events.count(date) > 0 && events[date].count(s) > 0) {
events[date].erase(s);
return true;
}
return false;
}
int Remove(const Date &date) {
if (events.count(date) > 0) {
const unsigned int event_count = events[date].size();
events.erase(date);
return event_count;
}
return 0;
}
void PrintAll() const {
for (const auto &item : events) {
for (const auto &event : item.second) {
cout << item.first << " " << event << endl;
}
}
}
private:
map<Date, set<string>> events;
};
void ExecuteCommands(Database &db, istream &stream) {
try {
string command_line;
while (getline(stream, command_line)) {
// Добавляем строчку из стрима в стрим строки
stringstream ss(command_line);
// строка команды
string command;
ss >> command;
// если пустая строка - пропускаем к следующей команде
if (command.empty()) {
continue;
} else if (command == COMMAND_ADD) {
string date, event;
ss >> date >> event;
// парсим дату из строки
const Date d = ParseDate(date);
db.Add(d, event);
} else if (command == COMMAND_DEL) {
string date, event;
ss >> date;
// если не конец строки - парсим название события
if (!ss.eof()) {
ss >> event;
}
const Date d = ParseDate(date);
// Если события не было - удаляем по дате всю дату
if (event.empty()) {
cout << "Deleted " << db.Remove(d) << " events" << endl;
} else {
// если было передано событие, удаляем событие, или говорим что удалять нечего
if (db.Remove(d, event)) {
cout << "Deleted successfully" << endl;
} else {
cout << "Event not found" << endl;
}
}
} else if (command == COMMAND_FIND) {
string date;
ss >> date;
const Date d = ParseDate(date);
for (const auto &event : db.Find(d)) {
cout << event << endl;
}
} else if (command == COMMAND_PRINT) {
db.PrintAll();
} else {
throw logic_error("Unknown command: " + command);
}
}
}
catch (const exception &e) {
cout << e.what() << endl;
}
}
void RunProduction() {
Database db;
ExecuteCommands(db, cin);
}
void RunTest() {
Database db;
stringstream stream;
// RunCommand(stream, "Add 1-1-1 event1");
// RunCommand(stream, "Add qwerty qwerty");
RunCommand(stream, "Add 0-1-2 event1");
RunCommand(stream, "Add 1-2-3 event2");
RunCommand(stream, "Find 0-1-2");
// RunCommand(stream, "\t");
RunCommand(stream, "Del 0-1-2");
RunCommand(stream, "Print");
RunCommand(stream, "Del 1-2-3 event2");
RunCommand(stream, "Del 1-2-3 event2");
// RunCommand(stream, "Add qwerty qwerty");
// RunCommand(stream, "Add 1 1 1 event");
ExecuteCommands(db, stream);
}
int main() {
RunProduction();
return 0;
}