Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <map>
- #include <sstream>
- #include <set>
- #include <string>
- using namespace std;
- struct Year
- {
- int value;
- Year(const int new_value )
- {
- value = new_value;
- }
- };
- struct Month
- {
- int value;
- Month(const int new_value)
- {
- if(new_value < 0 || new_value > 12)
- {
- throw runtime_error("Month value is invalid:" + to_string(new_value));
- }
- value = new_value;
- }
- };
- struct Day
- {
- int value;
- Day(const int new_value)
- {
- if(new_value <= 0 || new_value > 31)
- {
- throw runtime_error("Day value is invalid:" + to_string(new_value));
- }
- value = new_value;
- }
- };
- class Date {
- public:
- Date()
- {
- year = 0;
- month = 0;
- day = 0;
- }
- Date(const Year y, const Month m, const Day d)
- {
- year = y.value;
- month = m.value;
- day = d.value;
- }
- 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)
- {
- if(lhs.GetYear() < rhs.GetYear())
- return true;
- else if(lhs.GetMonth() < rhs.GetMonth())
- return true;
- else
- return lhs.GetDay() < rhs.GetDay();
- }
- istream& operator>>(istream& st, Date& date)
- {
- int y, m, d;
- char s1, s2;
- st >> y >> s1 >> m >> s2 >> d;
- if(s1 != '-' || s2 != '-')
- {
- stringstream tmp;
- tmp << "Wrong date format:" << y << s1 << m << s2 << d;
- throw invalid_argument(tmp.str());
- }
- date = Date(Year(y), Month(m), Day(d));
- return st;
- }
- ostream& operator<<(ostream& st,const Date& date)
- {
- st << setfill('0');
- st << setw(4) << date.GetYear() << '-' << setw(2) << date.GetMonth() << '-'
- << setw(2) << date.GetDay();
- return st;
- }
- 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[date].count(event) != 0)
- {
- base[date].erase(event);
- return true;
- }
- else
- return false;
- }
- int DeleteDate(const Date& date)
- {
- int nums = base[date].size();
- base.erase(date);
- return nums;
- }
- set<string> Find(const Date& date) /*const*/
- {
- return base[date];
- }
- void Print() const
- {
- for(const auto& i : base)
- {
- for(const auto& s : i.second)
- {
- cout << i.first << ' ' << s << endl;
- }
- }
- }
- private:
- map<Date, set<string>> base;
- };
- int main() {
- Database db;
- string line;
- while (getline(cin, line)) {
- stringstream ss;
- string command;
- ss << line;
- ss >> command;
- if(command == "Add")
- {
- Date new_date;
- string input;
- try
- {
- ss >> new_date >> input;
- db.AddEvent(new_date, input);
- }
- catch(exception& er)
- {
- cout << er.what();
- return 1;
- }
- }
- else if(command == "Del")
- {
- try
- {
- Date new_date;
- string input;
- ss >> new_date >> input;
- if(input == "")
- {
- cout << "Deleted " << db.DeleteDate(new_date) << " events" << endl;
- }
- else if(db.DeleteEvent(new_date, input))
- {
- cout << "Deleted successfully" << endl;
- }
- else
- {
- cout << "Event not found" << endl;
- }
- }catch(exception& ex)
- {
- cout << ex.what();
- return 1;
- }
- }
- else if(command == "Find")
- {
- try
- {
- Date new_date;
- ss >> new_date;
- for(const auto& i : db.Find(new_date))
- {
- cout << i << endl;
- }
- }catch(exception& ex)
- {
- cout << ex.what();
- return 1;
- }
- }
- else if(command == "Print")
- {
- db.Print();
- }
- else if(command == "")
- continue;
- else
- {
- cout << "Unknown command:" << command << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement