-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.cpp
More file actions
180 lines (156 loc) · 5 KB
/
database.cpp
File metadata and controls
180 lines (156 loc) · 5 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
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
using namespace std;
class Date {
public:
Date(int new_year, int new_month, int new_day) {
year = new_year;
if (new_month > 12 || new_month < 1) {
throw logic_error("Month value is invalid: " + to_string(new_month));
}
month = new_month;
if (new_day > 31 || new_day < 1) {
throw logic_error("Day value is invalid: " + to_string(new_day));
}
day = new_day;
}
int GetYear() const {
return year;
}
int GetMonth() const {
return month;
}
int GetDay() const {
return day;
}
private:
int year;
int month;
int day;
};
// определить сравнение для дат необходимо для использования их в качестве ключей словаря
bool operator<(const Date& lhs, const Date& rhs) {
// воспользуемся тем фактом, что векторы уже можно сравнивать на <:
// создадим вектор из года, месяца и дня для каждой даты и сравним их
return vector<int>{lhs.GetYear(), lhs.GetMonth(), lhs.GetDay()} <
vector<int>{rhs.GetYear(), rhs.GetMonth(), rhs.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;
}
class Database {
public:
void AddEvent(const Date& date, const string& event) {
storage[date].insert(event);
}
bool DeleteEvent(const Date& date, const string& event) {
if (storage.count(date) > 0 && storage[date].count(event) > 0) {
storage[date].erase(event);
return true;
}
return false;
}
int DeleteDate(const Date& date) {
if (storage.count(date) == 0) {
return 0;
} else {
const int event_count = storage[date].size();
storage.erase(date);
return event_count;
}
}
set<string> Find(const Date& date) const {
if (storage.count(date) > 0) {
return storage.at(date);
} else {
return {};
}
}
void Print() const {
for (const auto& item : storage) {
for (const string& event : item.second) {
cout << item.first << " " << event << endl;
}
}
}
private:
map<Date, set<string>> storage;
};
Date ParseDate(const string& date) {
istringstream date_stream(date);
bool ok = true;
int year;
ok = ok && (date_stream >> year);
ok = ok && (date_stream.peek() == '-');
date_stream.ignore(1);
int month;
ok = ok && (date_stream >> month);
ok = ok && (date_stream.peek() == '-');
date_stream.ignore(1);
int day;
ok = ok && (date_stream >> day);
ok = ok && date_stream.eof();
if (!ok) {
throw logic_error("Wrong date format: " + date);
}
return Date(year, month, day);
}
int main() {
try {
Database db;
string command_line;
while (getline(cin, command_line)) {
stringstream ss(command_line);
string command;
ss >> command;
if (command == "Add") {
string date_str, event;
ss >> date_str >> event;
const Date date = ParseDate(date_str);
db.AddEvent(date, event);
} else if (command == "Del") {
string date_str;
ss >> date_str;
string event;
if (!ss.eof()) {
ss >> event;
}
const Date date = ParseDate(date_str);
if (event.empty()) {
const int count = db.DeleteDate(date);
cout << "Deleted " << count << " events" << endl;
} else {
if (db.DeleteEvent(date, event)) {
cout << "Deleted successfully" << endl;
} else {
cout << "Event not found" << endl;
}
}
} else if (command == "Find") {
string date_str;
ss >> date_str;
const Date date = ParseDate(date_str);
for (const string& event : db.Find(date)) {
cout << event << endl;
}
} else if (command == "Print") {
db.Print();
} else if (!command.empty()) {
throw logic_error("Unknown command: " + command);
}
}
} catch (const exception& e) {
cout << e.what() << endl;
}
return 0;
}