-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
193 lines (175 loc) · 4.97 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
182
183
184
185
186
187
188
189
190
191
192
193
#include <iostream>
#include <iomanip>
#include <map>
#include <set>
using namespace std;
class Date {
public:
Date(){
year = 0;
month = 0;
day = 0;
}
Date(int y, int m, int d){
if(m < 1 || m > 12){
throw runtime_error("Month value is invalid: " + to_string(m));
}
if(d < 1 || d > 31){
throw runtime_error("Day value is invalid: " + to_string(d));
}
year = y;
month = m;
day = d;
}
int GetYear() const{
return year;
}
int GetMonth() const{
return month;
}
int GetDay() const{
return day;
}
private:
int year;
int month;
int day;
};
ostream& operator<<(ostream& stream, const Date& date){
stream << setfill('0') << setw(4) << date.GetYear() << "-"
<< setfill('0') << setw(2) << date.GetMonth() << "-"
<< setfill('0') << setw(2) << date.GetDay();
return stream;
}
istream& operator>>(istream& stream, Date& date){
int y=0, m=0, d=0;
char first, second;
if(stream){
stream.ignore(1);
int x = stream.peek();
if(x < 48 || x > 57){
string res;
stream >> res;
throw runtime_error("Wrong date format: " + res);
}
stream >> y;
if(stream.peek() != '-'){
throw runtime_error("Wrong date format: " + to_string(y));
}
stream >> first;
stream >> m;
if(stream.peek() != '-'){
throw runtime_error("Wrong date format: " + to_string(y) + first + to_string(m));
}
stream >> second;
if(stream){
stream >> d;
char x = stream.peek();
if(first != '-' || second != '-' || (x != ' ' && x != '\n')){
string external;
stream >> external;
throw runtime_error("Wrong date format: " + to_string(y) + first
+ to_string(m) + second + to_string(d) + external);
}
date = {y, m, d};
}
}
return stream;
}
bool operator<(const Date& lhs, const Date& rhs){
if(lhs.GetYear() != rhs.GetYear()){
return lhs.GetYear() < rhs.GetYear();
}
if(lhs.GetMonth() != rhs.GetMonth()){
return lhs.GetMonth() < rhs.GetMonth();
}
if(lhs.GetDay() != rhs.GetDay()){
return lhs.GetDay() < rhs.GetDay();
}
return false;
}
class Database {
public:
void AddEvent(const Date& date, const string& event){
base[date].insert(event);
}
bool DeleteEvent(const Date& date, const string& event){
if(base.count(date) > 0){
if(base[date].count(event) > 0){
base[date].erase(event);
} else{
return false;
}
} else{
return false;
}
return true;
}
int DeleteDate(const Date& date){
int n = base[date].size();
base.erase(date);
return n;
}
void Find(const Date& date) const{
if(base.count(date) > 0) {
for (const auto &e : base.at(date)) {
cout << e << endl;
}
}
}
void Print() const{
for(const auto& d : base){
for(const auto& s : d.second){
cout << d.first << " " << s << endl;
}
}
}
private:
map<Date, set<string> > base;
};
int main() {
Database db;
string command;
try {
while (cin >> command) {
if (command == "Add") {
Date d;
string event;
if(cin.peek() == '\n'){
cout << "Wrong date format: " << endl;
break;
}
cin >> d;
cin >> event;
db.AddEvent(d, event);
} else if (command == "Del") {
Date d;
string event;
cin >> d;
if (cin.peek() != '\n') {
cin >> event;
}
if (event != "") {
if (db.DeleteEvent(d, event)) {
cout << "Deleted successfully" << endl;
} else {
cout << "Event not found" << endl;
}
} else {
cout << "Deleted " << db.DeleteDate(d) << " events" << endl;
}
} else if (command == "Find") {
Date d;
cin >> d;
db.Find(d);
} else if (command == "Print") {
db.Print();
} else {
cout << "Unknown command: " << command << endl;
}
}
} catch (const runtime_error& ex){
cout << ex.what() << endl;
}
return 0;
}