Advertisement
Guest User

Untitled

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