Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <map>
  5. #include <vector>
  6. #include <set>
  7. #include <iomanip>
  8. using namespace std;
  9.  
  10. class Date {
  11. public:
  12. int GetYear() const {
  13. return year;
  14. }
  15. int GetMonth() const{
  16. return month;
  17. }
  18. int GetDay() const {
  19. return day;
  20. }
  21.  
  22. void PutDay(const int& d){
  23. day = d;
  24. //return day;
  25. }
  26. void PutMonth (const int& m){
  27. month = m;
  28. //return month;
  29. }
  30. void PutYear(const int& y){
  31. year = y;
  32. //return year;
  33. }
  34. private:
  35. int year;
  36. int month;
  37. int day;
  38. };
  39.  
  40. bool operator<(const Date& lhs, const Date& rhs){
  41.  
  42. if(lhs.GetYear() == rhs.GetYear() && lhs.GetMonth() == rhs.GetMonth()){
  43. return lhs.GetDay() < rhs.GetDay();
  44. }
  45. else if (lhs.GetYear() == rhs.GetYear()){
  46. return lhs.GetMonth() < rhs.GetMonth();
  47. }
  48. else return lhs.GetYear() < rhs.GetYear();
  49.  
  50. }
  51.  
  52. bool operator==(const Date& lhs, const Date& rhs){
  53. return (lhs.GetYear() == rhs.GetYear() &&
  54. lhs.GetMonth() == rhs.GetMonth() &&
  55. lhs.GetDay() == rhs.GetDay());
  56.  
  57.  
  58. }
  59.  
  60. class Database {
  61. public:
  62. void AddEvent(const Date& date, const string& event){
  63. Map_DE [date].insert(event);
  64. }
  65.  
  66. bool DeleteEvent(const Date& date, const string& event){
  67. bool r = 0;
  68. if (Map_DE.count(date)!= 0){
  69. for (const auto& i:Map_DE.at(date)){
  70. if (i == event) {
  71. Map_DE.at(date).erase(event);
  72. r = 1;
  73. cout << "Deleted successfully" << endl;
  74. return r;
  75. }
  76.  
  77. }
  78. }
  79. cout << "Event not found" << endl;
  80. return r;
  81.  
  82. }
  83. int DeleteDate(const Date& date){
  84. int n = 0;
  85. if ( Map_DE.count(date)!= 0){
  86. set <string> nt;
  87. nt = Map_DE.at(date);
  88. n = nt.size();
  89. Map_DE.erase(date);
  90. return n;
  91.  
  92. }
  93. return n;
  94. }
  95.  
  96. void Find(const Date& date) const{
  97. if ( Map_DE.count(date)!= 0){
  98. for (const auto& i:Map_DE.at(date)){
  99.  
  100. cout << i << endl;
  101. }
  102. }
  103. }
  104.  
  105. void Print() const {
  106. for(const auto& m: Map_DE){
  107.  
  108. for(const auto& a: m.second){
  109. cout << setw(4) << setfill('0') << m.first.GetYear() << "-" <<
  110. setw(2) << m.first.GetMonth() << "-" << setw(2) << m.first.GetDay() <<" "<< a << endl;
  111.  
  112. }
  113.  
  114. }
  115. }
  116. private:
  117. map <Date, set<string>> Map_DE;
  118. };
  119. void Ensure (stringstream& stream) {
  120. if (stream.peek() != '-') {
  121. throw exception();
  122.  
  123. }
  124. stream.ignore(1);
  125. }
  126. void Ensure_end (stringstream& stream) {
  127. if (stream.peek() == EOF) {
  128. throw exception();
  129. }
  130. }
  131.  
  132.  
  133. Date ParseDate (const string& _date){
  134. Date newdate;
  135. stringstream input (_date);
  136. int yy, dd, mm;
  137. input >> yy;
  138. if(!input >> yy){
  139. throw exception();
  140. }
  141. Ensure(input);
  142. input >> mm;
  143. if(!input >> mm) {
  144. throw exception();
  145. }
  146. Ensure(input);
  147. input >> dd;
  148. if(!input >> dd) {
  149. throw exception();
  150. }
  151. Ensure_end(input);
  152.  
  153.  
  154. if (mm < 1 || mm > 12 ){
  155.  
  156. throw invalid_argument("Month value is invalid: " + to_string(mm));
  157. }
  158. else if (dd < 1 || dd > 31){
  159.  
  160. throw domain_error("Day value is invalid: " + to_string(dd));
  161.  
  162. }
  163. else if (mm > 0 && mm < 13 && dd > 0 && dd < 32){
  164. newdate.PutDay(dd);
  165. newdate.PutMonth(mm);
  166. newdate.PutYear(yy);
  167.  
  168. return newdate;
  169. }
  170.  
  171. }
  172.  
  173. int main() {
  174. Database db;
  175.  
  176. string command;
  177. while (getline(cin, command)) {
  178. stringstream stream (command);
  179. string comm, _date, event;
  180. stream >> comm;
  181. stream >> _date;
  182. stream >> event;
  183. Date date;
  184.  
  185. try {
  186.  
  187. if(comm == "Add" ){
  188. date = ParseDate (_date);
  189. db.AddEvent(date, event);
  190. }
  191. else if(comm == "Del"){
  192. date = ParseDate (_date);
  193. if(event == ""){
  194. cout << "Deleted " << db.DeleteDate(date) << " events" << endl;
  195. }
  196. else {
  197. db.DeleteEvent(date, event);
  198.  
  199. }
  200.  
  201. }
  202. else if(comm == "Find"){
  203. date = ParseDate (_date);
  204. db.Find(date);
  205. }
  206. else if(comm == "Print"){
  207. db.Print();
  208. }
  209. else if(comm == ""){
  210.  
  211. }
  212. else cout << "Unknown command: " << comm << endl;
  213. }
  214. catch (invalid_argument& ii){
  215. cout << ii.what();
  216. return 0;
  217. }
  218.  
  219. catch (domain_error& ex3){
  220. cout << ex3.what();
  221. return 0;
  222. }
  223.  
  224. catch (exception& ex1){
  225. cout << "Wrong date format: " << _date << endl;
  226. return 0;
  227. }
  228.  
  229.  
  230.  
  231.  
  232. }
  233.  
  234. return 0;
  235. }
  236. /*
  237. Add 0-1-2 event1
  238. Add 1-2-3 event2
  239. Find 0-1-2
  240.  
  241. Del 0-1-2
  242. Print
  243. Del 1-2-3 event2
  244. Del 1-2-3 event2
  245.  
  246. вывод
  247. event1
  248. Deleted 1 events
  249. 0001-02-03 event2
  250. Deleted successfully
  251. Event not found
  252.  
  253.  
  254. Month value is invalid: MONTH
  255. Add 1--1-32 task
  256. Add 1-0-32 task
  257. Add 1-13-32 task
  258. Add 1-+0-32 task
  259.  
  260. Day value is invalid: DAY
  261. Add 1-1--1 task
  262. Add 1-1-0 task
  263. Add 1-1-+0 task
  264. Add 1-1-32 task
  265.  
  266. Wrong date format: DATE
  267. Add f8-1-1 task
  268. Add 9f-1-1 task
  269. Add 1-11f-1 task
  270. Add 1-f12-1 task
  271. Add 1-1-f13 task!
  272. Add 1-1-14f task
  273. Add --15-1-1 task
  274. Add 1---2-1 task
  275. Add 1-1---17 task
  276. Add 1-1- task
  277. Add 1--1 task
  278. Add --111 task
  279. Add 1---+++1 task
  280. Add --++1 task
  281. Add 1-1-f task
  282. Del foo
  283.  
  284. Add 1-0- event1
  285.  
  286.  
  287. OK:
  288. Add 1-1-1 task
  289. Add -2-1-1 task
  290. Add 03-01-01 task
  291. Add +4-1-1 task
  292. Add 1-+6-1 task
  293. Add 1-1-+7 task
  294.  
  295.  
  296. Sorting:
  297. Input:
  298. Add 1-1-1 C
  299. Add 1-1-1 A
  300. Add 1-1-1 B
  301. Add 1-2-1 task2
  302. Add 1-1-2 task3
  303. Add 2-1-1 task1
  304. Output:
  305. 0001-01-01 A
  306. 0001-01-01 B
  307. 0001-01-01 C
  308. 0001-01-02 task3
  309. 0001-02-01 task2
  310. 0002-01-01 task1
  311.  
  312. Input:
  313. Find 1-1-1
  314. Output:
  315. A
  316. B
  317. C
  318. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement