Advertisement
Guest User

Final_White

a guest
Sep 19th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.04 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // Final_White
  4. //
  5. // Created by Robert Bartuli on 18.09.19.
  6. // Copyright © 2019 Robert Bartuli. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <map>
  11. #include <string>
  12. #include <set>
  13. #include <algorithm>
  14. #include <iomanip>
  15.  
  16. using namespace std;
  17.  
  18. // Реализуйте функции и методы классов и при необходимости добавьте свои
  19.  
  20. class Date {
  21. public:
  22. int GetYear() const{
  23. return Year;
  24. };
  25. int GetMonth() const{
  26. return Month;
  27. };
  28. int GetDay() const{
  29. return Day;
  30. };
  31.  
  32. Date(string s){
  33. string raw_Year={},raw_Month={},raw_Day={};
  34.  
  35. if (s[0]=='-' || s[0]=='+') {
  36. raw_Year=s.substr(0,s.find("-",1));
  37. }else raw_Year=s.substr(0,s.find("-",0));
  38.  
  39. if (s.size()>=raw_Year.size()+1)
  40. raw_Month=s.substr(raw_Year.size()+1,s.size()-(s.size()-s.rfind("-")+1)-raw_Year.size());
  41. if (raw_Month[raw_Month.size()-1]=='-') raw_Month=raw_Month.substr(0,raw_Month.size()-1);
  42.  
  43.  
  44. if (s.size()>=raw_Year.size()+raw_Month.size()+2)
  45. raw_Day=s.substr(raw_Year.size()+raw_Month.size()+2,s.size()-1);
  46. // cout<<"Year="<<raw_Year<<" Month="<<raw_Month<<" Day="<<raw_Day<<endl;
  47.  
  48.  
  49. //Проверка на посторонние смволы в дате
  50. string temp1,temp2,temp3;//Дата с удаленными дифисами и + и -
  51.  
  52. if (raw_Year[0]=='-' || raw_Year[0]=='+') temp1=raw_Year.substr(1,raw_Year.size()-1);else temp1=raw_Year;
  53. if (raw_Month[0]=='-' || raw_Month[0]=='+') temp2=raw_Month.substr(1,raw_Month.size()-1);else temp2=raw_Month;
  54. if (raw_Day[0]=='-' || raw_Day[0]=='+') temp3=raw_Day.substr(1,raw_Day.size()-1);else temp3=raw_Day;
  55.  
  56.  
  57. if ((temp1.size()==0)||(temp2.size()==0)||(temp3.size()==0) ||((temp1+temp2+temp3).find_first_not_of("0123456789")!=-1))
  58. throw runtime_error("Wrong date format: "+s);
  59.  
  60. if (stoi(raw_Month)>12||stoi(raw_Month)<1) throw runtime_error("Month value is invalid: "+raw_Month);
  61. if (stoi(raw_Day)>12||stoi(raw_Day)<1) throw runtime_error("Day value is invalid: "+raw_Day);
  62.  
  63. Year=stoi(raw_Year);
  64. Month=stoi(raw_Month);
  65. Day=stoi(raw_Day);
  66. }
  67.  
  68.  
  69. private:
  70. int Year;
  71. int Month;
  72. int Day;
  73.  
  74.  
  75. };
  76.  
  77. bool operator<(const Date& lhs, const Date& rhs){
  78. if ((lhs.GetYear()==rhs.GetYear())&&(lhs.GetMonth()==rhs.GetMonth())) return (lhs.GetDay()<rhs.GetDay());
  79. if (lhs.GetYear()==rhs.GetYear()) return (lhs.GetMonth()<rhs.GetMonth());
  80. return (lhs.GetYear()<rhs.GetYear());
  81.  
  82. };
  83.  
  84. class Database {
  85. public:
  86. void AddEvent(const Date& date, const string& event){
  87. BD[date].insert(event);
  88. };
  89.  
  90. bool DeleteEvent(const Date& date, const string& event){
  91. if (BD.count(date) > 0) {
  92. if (BD.at(date).count(event)>0) {
  93. BD.at(date).erase(event);
  94. return true;
  95. }else return false;
  96. }else return false;
  97.  
  98. };
  99.  
  100.  
  101. int DeleteDate(const Date& date){
  102. int retur_count;
  103. if (BD.count(date) > 0) {
  104. retur_count=BD.at(date).size();
  105. BD.erase (date);
  106. return retur_count;
  107. } else return 0;
  108. };
  109.  
  110. void Find(const Date& date) const{
  111. if (BD.count(date) > 0) {
  112. for (auto s:BD.at(date)){
  113. cout<<s<<endl;
  114. };
  115.  
  116. }
  117. };
  118.  
  119. void Print() const{
  120. for(auto& s: BD){
  121.  
  122. for (auto s_event:s.second){
  123. cout<<setfill ('0')<<setw(4) <<s.first.GetYear()<<"-"<<setw(2)<<s.first.GetMonth()<<"-"<<setw(2)<<s.first.GetDay();
  124. cout<<" "<<s_event<<endl;
  125.  
  126. };
  127.  
  128. };
  129.  
  130. };
  131.  
  132. private:
  133. map<Date, set<string>> BD;
  134. };
  135.  
  136. int main() {
  137. Database db;
  138.  
  139.  
  140. string command,raw_date,raw_event;
  141. try {
  142. while (getline(cin, command)) {
  143.  
  144. //Обрабатывам команды без параметров
  145. if (count(command.begin(), command.end(), ' ')==0) {
  146. if (command=="Print"){
  147. db.Print();
  148. } else if (command.size()==0); else throw runtime_error("Unknown command: "+command);
  149. }
  150.  
  151. //Обрабатывам команды с одним параметром
  152. if (count(command.begin(), command.end(), ' ')==1) {
  153. if (command.substr(0,command.find(" "))=="Del"){
  154. //Обработка DEL
  155.  
  156. string raw_date=command.substr(4,command.rfind(" ")-4);
  157. int temp_del=db.DeleteDate(Date(raw_date));
  158. cout<<"Deleted "<<temp_del<<" events"<<endl;
  159.  
  160. } else if (command.substr(0,command.find(" "))=="Find"){
  161. //Обработка Find
  162.  
  163. string raw_date=command.substr(5,command.rfind(" ")-5);
  164.  
  165. db.Find(Date(raw_date));
  166.  
  167. } if ((command.substr(0,command.find(" "))=="Print")||
  168. (command.substr(0,command.find(" "))=="Add")||
  169. (command.substr(0,command.find(" "))=="Del")||
  170. (command.substr(0,command.find(" "))=="Find")) ;else throw runtime_error("Unknown command: "+command.substr(0,command.find(" ")));
  171. }
  172.  
  173. //Обрабатывам команды с двумя параметрами
  174.  
  175. if (count(command.begin(), command.end(), ' ')==2) {
  176. if (command.substr(0,command.find(" "))=="Del"){
  177. //Обработка DEL
  178.  
  179. string raw_date=command.substr(4,command.rfind(" ")-4);
  180. string raw_event=command.substr(command.rfind(" ")+1,command.size()-1);
  181.  
  182. if (db.DeleteEvent(Date(raw_date),raw_event)) cout<<"Deleted successfully"<<endl;
  183. else cout<<"Event not found"<<endl;
  184.  
  185.  
  186. } else if (command.substr(0,command.find(" "))=="Add"){
  187.  
  188. string raw_date=command.substr(4,command.rfind(" ")-4);
  189. string raw_event=command.substr(command.rfind(" ")+1,command.size()-1);
  190.  
  191. db.AddEvent(Date(raw_date), raw_event);
  192.  
  193. } else throw runtime_error("Unknown command: "+command.substr(0,command.find(" ")));
  194.  
  195. }
  196.  
  197. //Обрабатывам команды c более чем 2-мя параметрами
  198. if (count(command.begin(), command.end(), ' ')>2) throw runtime_error("Unknown command: "+command.substr(0,command.find(" ")));
  199.  
  200. }
  201.  
  202. return 0;
  203. } catch (runtime_error& ex) {
  204. cout << ex.what()<<endl; return 0;
  205. }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement