Guest User

Untitled

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