SHARE
TWEET

WhiteBeltCapstone

a guest Apr 26th, 2019 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <set>
  3. #include <map>
  4. #include <exception>
  5. #include <sstream>
  6. #include <string>
  7. #include <iomanip>
  8.  
  9. using namespace std;
  10.  
  11. //вспомогательные функции
  12. int CountWords(const string& s) {
  13.     stringstream is(s);
  14.     int number_of_words = 0;
  15.     string word;
  16.     while (is >> word) {number_of_words++;}
  17.     return number_of_words;
  18. }
  19.  
  20. void WrongDateFormat(const stringstream& flow, const string& date) {
  21.     string error = "Wrong date format: " + date;
  22.     throw runtime_error(error);
  23. }
  24.  
  25. void EnsureNextSymbolAndSkip(stringstream& stream, const string& input_date) {
  26.     if (stream.peek() != '-') {
  27.         WrongDateFormat(stream, input_date);
  28.     }
  29.     stream.ignore(1);
  30. }
  31.  
  32. void EnsureCorrectFinal(stringstream& stream, const string& input_date) {
  33.     if (stream.peek() != EOF) {
  34.         WrongDateFormat(stream, input_date);
  35.     }
  36. }
  37.  
  38. //структура Дейт, функции и операторы для нее
  39. struct Date {
  40.     int year = 0;
  41.     int month = 0;
  42.     int day = 0;
  43. };
  44.  
  45. void operator<<(ostream& output, const Date& date) {
  46.     output << setfill('0');
  47.     output << setw(4) << date.year << '-'
  48.     << setw(2) << date.month << '-' << setw(2) << date.day;
  49. }
  50.  
  51. bool operator<(const Date& lhs, const Date& rhs) {
  52.     if (lhs.year == rhs.year) {
  53.         if (lhs.month == lhs.month) {
  54.             return lhs.day < rhs.day;
  55.         }
  56.         return lhs.month < rhs.month;
  57.     }
  58.     return lhs.year < rhs.year;
  59. }
  60.  
  61. Date ParseDate(const string& s){
  62.     Date date;
  63.     string save_the_date = s;
  64.     stringstream stream(s);
  65.     stream >> date.year;
  66.     EnsureNextSymbolAndSkip(stream, save_the_date);
  67.     stream >> date.month;
  68.     EnsureNextSymbolAndSkip(stream, save_the_date);
  69.     stream >> date.day;
  70.     EnsureCorrectFinal(stream, save_the_date);
  71.     return date;
  72. }
  73.  
  74. void CheckDate (const Date& new_date) {
  75.     int new_month = new_date.month;
  76.     if (new_month > 12 || new_month < 1) {
  77.         string ss = "Month value is invalid: " + to_string(new_month);
  78.         throw runtime_error(ss);
  79.     }
  80.     int new_day = new_date.day;
  81.     if (new_day > 31 || new_day < 1) {
  82.         string ss = "Day value is invalid: " + to_string(new_day);
  83.         throw runtime_error(ss);
  84.     }
  85. }
  86.  
  87. //КЛАСС
  88. class Database {
  89. private:
  90.     map<Date, set<string>> db;
  91.    
  92. public:
  93.    
  94.     void AddEvent(const Date& date, const string& some_event) {
  95.         db[date].insert(some_event);
  96.     }
  97.    
  98.     bool DeleteEvent(const Date& date, const string& some_event) {
  99.         if (db.count(date) == 0 || db[date].count(some_event) == 0) {
  100.             return false;
  101.             //cout << "Event not found" << endl;
  102.         }
  103.         else {
  104.             set<string> day = db[date];
  105.             day.erase(some_event);
  106.             db[date] = day;
  107.             return true;
  108.             //cout << "Deleted successfully" << endl;
  109.         }
  110.     }
  111.    
  112.     int DeleteDate(Date& date) {
  113.         int res = db.at(date).size();
  114.         db.erase(date);
  115.         return res;
  116.     }
  117.    
  118.     void FindDate(const Date& date) const{
  119.       if (db.count(date) > 0) {
  120.         for (auto s: db.at(date)) {
  121.             cout << s << endl;
  122.         }
  123.       }
  124.     }
  125.    
  126.     void PrintAll() const{
  127.         for (auto x:db) {
  128.             set<string> events = x.second;
  129.             for (string s: events) {
  130.                 cout << x.first;
  131.                 cout << " " << s << endl;
  132.             }
  133.         }
  134.     }
  135. };
  136.  
  137. //Проверка команды из потока ввода
  138. void CheckCommand(const string& some_command, Database& database) {
  139.     set<string> commands = {"Del", "Find", "Add"};
  140.     string action;
  141.     string input_date;
  142.     string new_event;
  143.    
  144.     int words = CountWords(some_command);
  145.     stringstream stream(some_command);
  146.       if (words == 1) {
  147.           if (some_command == "Print") {
  148.             database.PrintAll();
  149.           }
  150.           else {
  151.             string error = "Unknown command: " + some_command;
  152.             throw runtime_error(error);
  153.           }
  154.         }
  155.       else if (words > 1) {
  156.             getline(stream, action, ' ');
  157.             if (commands.count(action) == 0 ) {
  158.             string error = "Unknown command: " + action;
  159.             throw runtime_error(error);
  160.             }
  161.             else if (words == 2) {
  162.                 getline(stream, input_date);
  163.                 Date new_date = ParseDate(input_date);
  164.                 CheckDate(new_date);
  165.                 if (action == "Del") {
  166.                   cout << "Deleted " << database.DeleteDate(new_date) << " events" << endl;
  167.                 }
  168.                 else if (action == "Find") {
  169.                     database.FindDate(new_date);
  170.                 }
  171.             }
  172.             else if (words == 3){
  173.                 getline(stream, input_date, ' ');
  174.                 Date new_date = ParseDate(input_date);
  175.                 CheckDate(new_date);
  176.                 getline(stream, new_event);
  177.                 if (action == "Del") {
  178.                     if (database.DeleteEvent(new_date, new_event)) {
  179.                         cout << "Deleted successfully" << endl;
  180.                     }
  181.                     else {
  182.                         cout << "Event not found" << endl;
  183.                     }
  184.                 }
  185.                 else if (action == "Add") {
  186.                     database.AddEvent(new_date, new_event);
  187.                 }
  188.         }
  189.     }
  190. }
  191.  
  192. //мэйн
  193. int main() {
  194.     Database yandex;
  195.     string command;
  196.    
  197.     while (getline(cin, command)) {
  198.         try {
  199.             if (command > "") {
  200.                 CheckCommand(command, yandex);
  201.             }
  202.         } catch (exception& ex) {
  203.             cout << ex.what() << endl;
  204.         }
  205.     }
  206.    
  207.     return 0;
  208. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top