Advertisement
dimon-torchila

Untitled

Feb 9th, 2022
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.50 KB | None | 0 0
  1. #include<iostream>
  2. #include<exception>
  3. #include<map>
  4. #include<set>
  5. #include<sstream>
  6. #include<iomanip>
  7. #include<vector>
  8.  
  9. using namespace std;
  10.  
  11. class Date {
  12. public:
  13.     Date(int year, int month, int day) {
  14.         this->year = year;
  15.         if (month > 12 || month < 1)
  16.             throw exception();
  17.         this->month = month;
  18.         if (day > 31 || day < 1)
  19.             throw exception();
  20.         this->day = day;
  21.     }
  22.     int GetYear() {
  23.         return year;
  24.     }
  25.     int GetMonth() {
  26.         return month;
  27.     }
  28.     int GetDay() {
  29.         return day;
  30.     }
  31. private:
  32.     int year;
  33.     int month;
  34.     int day;
  35. };
  36.  
  37. class DataBase {
  38. public:
  39.     void AddEvent(const Date& date, const string& event) {
  40.         holidays[date].insert(event);
  41.     }
  42.     bool DeleteEvent(const Date& date, const string& event) {
  43.         if (holidays.count(date) > 0 && holidays[date].count(event) > 0)
  44.             holidays[date].erase(event);
  45.     }
  46.     int DeleteDate(const Date& date)
  47.     {
  48.         if (holidays.count(date) == 0)
  49.             return 0;
  50.         else {
  51.             int event_counts = holidays[date].size();
  52.             holidays.erase(date);
  53.             return event_counts;
  54.         }
  55.     }
  56.     set<string> FindDate(const Date& date) {
  57.         set<string> events;
  58.         if (holidays.count(date) > 0) {
  59.             for (const auto& x : holidays[date])
  60.                 events.insert(x);
  61.             return events;
  62.         }
  63.         else
  64.             return {};
  65.     }
  66.     void Print() const {
  67.         for (const auto& item : holidays) {
  68.             for (const string& event : item.second) {
  69.                 cout << item.first << " " << event << endl;
  70.             }
  71.         }
  72.     }
  73. private:
  74.     map<Date, set<string>> holidays;
  75. };
  76.  
  77. Date ParseDate(const string& s) {
  78.     stringstream s_stream(s);
  79.     int year;
  80.     s_stream >> year;
  81.     if (s_stream.peek() == '-')
  82.         throw exception();
  83.     s_stream.ignore(1);
  84.     int month;
  85.     s_stream >> month;
  86.     if (s_stream.peek() == '-')
  87.         throw exception();
  88.     s_stream.ignore(1);
  89.     int day;
  90.     s_stream >> day;
  91.     return Date(year, month, day);
  92. }
  93.  
  94. bool operator<(const Date& lhs, const Date& rhs) {
  95.     return vector<int>{lhs.GetYear(), lhs.GetMonth(), lhs.GetDay()} < vector<int>{rhs.GetYear(), rhs.GetMonth(), rhs.GetDay()};
  96. }
  97.  
  98. ostream& operator<<(istream& stream, const Date& date) {
  99.     stream << setw(4) << setfill('0') << date.GetYear() << "-" << setw(2) << setfill('0') << date.GetMonth() << "-" << setw(2) << setfill('0') << date.GetDay();
  100.     return stream;
  101. }
  102.  
  103. int main() {
  104.     try {
  105.         DataBase db;
  106.         string command_line;
  107.         while (getline(cin, command_line)) {
  108.             stringstream ss(command_line);
  109.             string command;
  110.             ss >> command;
  111.             if (command == "Add") {
  112.                 string date_str, event;
  113.                 ss >> date_str >> event;
  114.                 const Date date = ParseDate(date_str);
  115.                 db.AddEvent(date, event);
  116.             }
  117.             else if (command == "Del") {
  118.                 string date_str;
  119.                 ss >> date_str;
  120.                 string event;
  121.                 if (!ss.eof()) {
  122.                     ss >> event;
  123.                 }
  124.                 const Date date = ParseDate(date_str);
  125.                 if (event.empty()) {
  126.                     const int count = db.DeleteDate(date);
  127.                     cout << "Deleted " << count << " events" << endl;
  128.                 }
  129.                 else {
  130.                     if (db.DeleteEvent(date, event)) {
  131.                         cout << "Deleted successfully" << endl;
  132.                     }
  133.                     else {
  134.                         cout << "Event not found" << endl;
  135.                     }
  136.                 }
  137.             }
  138.             else if (command == "Find") {
  139.                 string date_str;
  140.                 ss >> date_str;
  141.                 const Date date = ParseDate(date_str);
  142.                 for (const string& event : db.FindDate(date)) {
  143.                     cout << event << endl;
  144.                 }
  145.             }
  146.             else if (command == "Print") {
  147.                 db.Print();
  148.             }
  149.             else if (command.empty()) {
  150.                 break;
  151.             }
  152.             else {
  153.                 throw logic_error("Unknown command: " + command);
  154.             }
  155.         }
  156.     }
  157.     catch (const exception& e) {
  158.         cout << e.what() << endl;
  159.     }
  160.     return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement