-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
181 lines (166 loc) · 4.49 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
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
#include <iostream>
#include <sstream>
#include <set>
#include <map>
#include <iomanip>
#include <stdexcept>
using std::map;
using std::set;
using std::stringstream;
using std::setw;
using std::setfill;
using std::string;
using std::cout;
using std::cin;
using std::endl;
class Date {
private:
int year;
int month;
int day;
public:
void SetDate(int new_year, int new_month, int new_day) {
year = new_year;
if (new_month > 12 || new_month < 1) {
throw std::logic_error("Month value is invalid: " + std::to_string(new_month));
}
month = new_month;
if (new_day > 31 || new_day < 1) {
throw std::logic_error("Day value is invalid: " + std::to_string(new_day));
}
day = new_day;
}
int GetYear() const {
return year;
}
int GetMonth() const {
return month;
}
int GetDay() const {
return day;
}
};
bool operator<(const Date& lhs, const Date& rhs) {
if (lhs.GetYear() == rhs.GetYear()) {
if (lhs.GetMonth() == rhs.GetMonth()) {
return lhs.GetDay() < rhs.GetDay();
}
return lhs.GetMonth() < rhs.GetMonth();
}
return lhs.GetYear() < rhs.GetYear();
}
std::istream& operator>>(std::istream& input, Date& date) {
string dateStr;
input >> dateStr;
stringstream ss(dateStr);
bool ok = true;
int year;
ok = bool(ss >> year);
ok = ok && (ss.peek() == '-');
ss.ignore(1);
int month;
ok = ok && (ss >> month);
ok = ok && (ss.peek() == '-');
ss.ignore(1);
int day;
ok = ok && (ss >> day);
ok = ok && ss.eof();
if (!ok) {
throw std::logic_error("Wrong date format: " + dateStr);
}
date.SetDate(year, month, day);
return input;
}
std::ostream& operator<<(std::ostream& output, const set<string>& events) {
for(const auto& event : events) {
output << event << endl;
}
return output;
}
class Database {
private:
map<Date, set<string>> db;
public:
void AddEvent(const Date& date, const string& event) {
db[date].insert(event);
}
bool DeleteEvent(const Date& date, const string& event) {
if (db.count(date) && db[date].count(event)) {
db[date].erase(event);
return true;
}
return false;
}
size_t DeleteDate(const Date& date) {
size_t eCount = 0;
if(db.count(date)) {
eCount = db[date].size();
}
db.erase(date);
return eCount;
}
set<string> Find(const Date& date) const {
set<string> events;
if(db.count(date)) {
for (const auto &event: db.at(date)) {
events.insert(event);
}
}
return events;
}
void Print() const {
for(const auto& i : db) {
for(const auto& j : i.second) {
if(i.first.GetYear() != -1) {
cout << setfill('0');
cout << setw(4) << i.first.GetYear() << '-' << setw(2)
<< i.first.GetMonth() << '-' << setw(2) << i.first.GetDay()
<< ' ' << j << endl;
}
}
}
}
};
int main() {
Database db;
string command;
while (getline(cin, command)) {
stringstream ss(command);
string cmd;
try {
ss >> cmd;
if (cmd == "Add") {
Date date;
string event;
ss >> date;
ss >> event;
db.AddEvent(date, event);
} else if (cmd == "Find") {
Date date;
ss >> date;
cout << db.Find(date);
} else if (cmd == "Del") {
Date date;
ss >> date;
string event;
ss >> event;
if (event.empty()) {
cout << "Deleted " << db.DeleteDate(date) << " events" << endl;
} else {
if (db.DeleteEvent(date, event)) {
cout << "Deleted successfully" << endl;
} else {
cout << "Event not found" << endl;
}
}
} else if (cmd == "Print") {
db.Print();
} else if (!cmd.empty()) {
throw std::logic_error("Unknown command: " + command);
}
} catch (const std::exception& e) {
cout << e.what() << endl;
}
}
return 0;
}