Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5. #include <math.h>
  6. #include <map>
  7. #include <fstream>
  8. #include <ostream>
  9. #include <iomanip>
  10. #include <set>
  11. #include <sstream>
  12.  
  13. using namespace std;
  14.  
  15. class Date {
  16.     public:
  17.     Date() {}
  18.     Date(int yearArg, int monthArg, int dayArg) {
  19.         year = yearArg;
  20.         month = monthArg;
  21.         day = dayArg;
  22.     }
  23.  
  24.     int GetYear() const {
  25.         return year;
  26.     }
  27.  
  28.     int GetMonth() const {
  29.         return month;
  30.     }
  31.  
  32.     int GetDay() const {
  33.         return day;
  34.     }
  35.  
  36. private:
  37.     int year, month, day;
  38. };
  39.  
  40. bool operator<(const Date &lhs, const Date &rhs) {
  41.     if (lhs.GetYear() != rhs.GetYear()) {
  42.         return lhs.GetYear() < rhs.GetYear();
  43.     }
  44.  
  45.     if (lhs.GetMonth() != rhs.GetMonth()) {
  46.         return lhs.GetMonth() < rhs.GetMonth();
  47.     }
  48.  
  49.     return lhs.GetDay() < rhs.GetDay();
  50. }
  51.  
  52. ostream &operator<<(ostream &stream, const Date &date) {
  53.     stream << setfill('0');
  54.     stream << setw(4) << date.GetYear() << "-"
  55.            << setw(2) << date.GetMonth() << "-"
  56.            << setw(2) << date.GetDay();
  57.     return stream;
  58. }
  59.  
  60. istream& operator>>(istream& stream, Date& date) {
  61.     int year, month, day;
  62.     char def1, def2;
  63.     string datName;
  64.     string error;
  65.  
  66.     stream >> datName;
  67.  
  68.     stringstream dateStream;
  69.     dateStream.str(datName);
  70.  
  71.     if (dateStream >> year >> def1 >> month >> def2 >> day) {
  72.         if (def1 != '-' || def2 != '-') {
  73.             error = "Wrong date format: " + datName;
  74.             throw runtime_error(error);
  75.         }
  76.         else if (dateStream.peek() != EOF) {
  77.             error =  "Wrong date format: " + datName;
  78.             throw runtime_error(error);
  79.         }
  80.         else if (month < 1 || month > 12) {
  81.             error = "Month value is invalid: " + to_string(month);
  82.             throw runtime_error(error);
  83.         }
  84.         else if (day < 1 || day > 31) {
  85.             error =  "Day value is invalid: " + to_string(day);
  86.             throw runtime_error(error);
  87.         }
  88.     }
  89.     else {
  90.         error = "Wrong date format: " + datName;
  91.         throw runtime_error(error);
  92.     }
  93.     date = Date(year, month, day);
  94.  
  95.     return stream;
  96. }
  97.  
  98. class Database {
  99. public:
  100.     void AddEvent(const Date &date, const string &event) {
  101.         db[date].insert(event);
  102.     }
  103.  
  104.     bool DeleteEvent(const Date &date, const string &event) {
  105.         if (db.count(date) == 0) {
  106.             return false;
  107.         }
  108.  
  109.         if (db[date].count(event) > 0) {
  110.             db[date].erase(event);
  111.             return true;
  112.         }
  113.  
  114.         return false;
  115.     }
  116.  
  117.     int DeleteDate(const Date &date) {
  118.         if (db.count(date) == 0) {
  119.             return 0;
  120.         }
  121.         int count = db[date].size();
  122.         db.erase(date);
  123.  
  124.         return count;
  125.     }
  126.  
  127.     vector<string> Find(const Date &date) const {
  128.         if (db.count(date) == 0) {
  129.             return {};
  130.         }
  131.         set<string> events = db.at(date);
  132.         vector<string> result(events.begin(), events.end());
  133.  
  134.         sort(begin(result), end(result));
  135.         return result;
  136.     }
  137.  
  138.     void Print() const {
  139.         vector<Date> dates;
  140.  
  141.         for (const auto& [key, value] : db) {
  142.             dates.push_back(key);
  143.         }
  144.  
  145.         sort(begin(dates), end(dates));
  146.  
  147.         for (const auto& date: dates) {
  148.             set<string> events = db.at(date);
  149.             vector<string> result(events.begin(), events.end());
  150.  
  151.             sort(begin(result), end(result));
  152.  
  153.             for (const auto& res: result) {
  154.                 cout << date << " " << res << endl;
  155.             }
  156.         }
  157.     }
  158.  
  159. private:
  160.     map<Date, set<string>> db;
  161. };
  162.  
  163. int main() {
  164.     Database db;
  165.  
  166.     string line;
  167.     string command;
  168.     while (getline(cin, line)) {
  169.         stringstream stream(line);
  170.         if (line.empty()) {
  171.             continue;
  172.         }
  173.         stream >> command;
  174.  
  175.         if (command == "Add") {
  176.             string event;
  177.             Date currentDate;
  178.             try {
  179.                 stream >> currentDate;
  180.             } catch (runtime_error& error) {
  181.                 cout << error.what() << endl;
  182.                 return 0;
  183.             }
  184.             stream >> event;
  185.             db.AddEvent(currentDate, event);
  186.         } else if (command == "Del") {
  187.             string event;
  188.             Date currentDate;
  189.             try {
  190.                 stream >> currentDate;
  191.             } catch (runtime_error& error) {
  192.                 cout << error.what() << endl;
  193.                 return 0;
  194.             }
  195.             stream >> event;
  196.  
  197.             if (!event.empty()) {
  198.                 bool isRemove = db.DeleteEvent(currentDate, event);
  199.                 cout << (isRemove ? "Deleted successfully" : "Event not found") << endl;
  200.             } else {
  201.                 int count = db.DeleteDate(currentDate);
  202.                 cout << "Deleted " + to_string(count) + " events" << endl;
  203.             }
  204.         } else if (command == "Find") {
  205.             Date currentDate;
  206.             try {
  207.                 stream >> currentDate;
  208.             } catch (runtime_error& error) {
  209.                 cout << error.what() << endl;
  210.                 return 0;
  211.             }
  212.             vector<string> events = db.Find(currentDate);
  213.             for (int i = 0; i < events.size(); i++) {
  214.                 if (i == 0) {
  215.                     cout << events[i];
  216.                     continue;
  217.                 }
  218.                 cout << " " << events[i];
  219.             }
  220.             if (!events.empty()) {
  221.                 cout << endl;
  222.             }
  223.         } else if (command == "Print") {
  224.             db.Print();
  225.         } else {
  226.             cout << "Unknown command: " + command;
  227.             return 0;
  228.         }
  229.     }
  230.  
  231.     return 0;
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement