Advertisement
Guest User

Untitled

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