Advertisement
illfate

Untitled

Oct 24th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. #include <set>
  5. #include <vector>
  6. #include <sstream>
  7. #include <algorithm>
  8. #include <iomanip>
  9. #include <exception>
  10. #include <cctype>
  11. using namespace std;
  12. class Date {
  13. public:
  14. Date() {}
  15. Date(int Year, int Month, int Day) {
  16. year = Year;
  17. month = Month;
  18. day = Day;
  19. }
  20. int GetYear() const {
  21. return year;
  22. }
  23. int GetMonth() const {
  24. return month;
  25. }
  26. int GetDay() const {
  27. return day;
  28. }
  29. private:
  30. int year;
  31. int month;
  32. int day;
  33. };
  34. bool operator<(const Date& lhs, const Date& rhs) {
  35. if (lhs.GetYear() < rhs.GetYear()) {
  36. return true;
  37. }
  38. else if (lhs.GetYear() == rhs.GetYear()) {
  39. if (lhs.GetMonth() < rhs.GetMonth()) {
  40. return true;
  41. }
  42. else if (lhs.GetMonth() == rhs.GetMonth()) {
  43. if (lhs.GetDay() < rhs.GetDay()) {
  44. return true;
  45. }
  46. else {
  47. return false;
  48. }
  49. }
  50. else {
  51. return false;
  52. }
  53. }
  54. else {
  55. return false;
  56. }
  57. }
  58. Date DateParse(const string& str) {
  59. int year, month, day;
  60. Date date;
  61. stringstream dateStream;
  62. dateStream.str(str);
  63.  
  64. dateStream >> year;
  65. if (year > 9999 || year < 0) {
  66. string ex = "Wrong date format: " + str;
  67. throw runtime_error(ex);
  68. }
  69. if (dateStream.peek() == '-') {
  70. dateStream.ignore(1);
  71. }
  72. else {
  73. string ex = "Wrong date format: " + str;
  74. throw runtime_error(ex);
  75. }
  76.  
  77. dateStream >> month;
  78. if (dateStream.peek() == '-') {
  79. dateStream.ignore(1);
  80. }
  81. else {
  82. string ex = "Wrong date format: " + str;
  83. throw runtime_error(ex);
  84. }
  85. dateStream >> day;
  86. if (dateStream.peek() != EOF) {
  87. string ex = "Wrong date format: " + str;
  88. throw runtime_error(ex);
  89. }
  90. if (month < 1 || month > 12) {
  91. string ex = "Month value is invalid: " + to_string(month);
  92. throw runtime_error(ex);
  93. }
  94. if (day < 1 || day > 31) {
  95. string ex = "Day value is invalid: " + to_string(day);
  96. throw runtime_error(ex);
  97. }
  98. return { year, month, day };
  99. }
  100. istream& operator>>(istream& stream, Date& date) {
  101. int year, month, day;
  102. char c;
  103. stream >> year >> c >> month >> c >> day;
  104. date = Date(year, month, day);
  105. return stream;
  106. }
  107. ostream& operator<<(ostream& stream, const Date& date) {
  108. return stream << date.GetYear() << "-" << date.GetMonth() << "-" << date.GetDay();
  109. }
  110. class Database {
  111. public:
  112. void AddEvent(const Date& date, const string& event) {
  113.  
  114. Event[date].insert(event);
  115. }
  116. bool DeleteEvent(const Date& date, const string& event) {
  117. if (Event[date].erase(event) != 0) {
  118. return true;
  119. }
  120. else return false;
  121. }
  122. int DeleteDate(const Date& date) {
  123. if (Event.count(date) > 0) {
  124. int n = Event.at(date).size();
  125. Event.erase(date);
  126. return n;
  127. }
  128. else return 0;
  129. }
  130.  
  131. void Find(const Date& date) const {
  132. for (const auto& item : Event.at(date)) {
  133. cout << item << endl;
  134. }
  135. }
  136.  
  137. void Print() const {
  138. for (auto item_map : Event) {
  139. for (auto item_set : item_map.second) {
  140. cout << setw(4) << setfill('0') << item_map.first.GetYear() << '-' << setw(2) << setfill('0') << item_map.first.GetMonth() << '-' << setw(2) << setfill('0') << item_map.first.GetDay() << ' ' << item_set << endl;
  141. }
  142. }
  143. };
  144. private:
  145. map<Date, set<string>> Event;
  146. };
  147. int main() {
  148. Database db;
  149. Date date;
  150. string command;
  151. while (getline(cin, command)) {
  152. if (command == " " || command == "" || command == "/n") {
  153. continue;
  154. }
  155. istringstream streamer(command);
  156. string command2, input_date, input_event;
  157. streamer >> command2 >> input_date >> input_event;
  158. if (command2 == "Add") {
  159. try {
  160. if (input_event != "") {
  161. db.AddEvent(DateParse(input_date), input_event);
  162. }
  163. }
  164. catch (runtime_error& er) {
  165. cout << er.what() << endl;
  166. }
  167. }
  168. else if (command2 == "Print") {
  169. db.Print();
  170. }
  171. else if (command2 == "Find") {
  172. db.Find(DateParse(input_date));
  173. }
  174. else if (command2 == "Del") {
  175. try {
  176. if (input_event.empty()) {
  177. int n = db.DeleteDate(DateParse(input_date));
  178. cout << "Deleted " << n << " events" << endl;
  179.  
  180. }
  181. else {
  182. if (db.DeleteEvent(DateParse(input_date), input_event)) {
  183. cout << "Deleted successfully" << endl;
  184. }
  185. else cout << "Event not found" << endl;
  186. }
  187. }
  188. catch (runtime_error& er) {
  189. cout << er.what() << endl;
  190. }
  191. }
  192. else {
  193. cout << "Unknown command : " << command2 << endl;
  194. }
  195. }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement