Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <sstream>
  5. #include <iomanip>
  6. #include <map>
  7. #include <set>
  8.  
  9. using namespace std;
  10.  
  11. class Date{
  12. public:
  13. int GetYear() const{
  14. return year;
  15. }
  16. int GetMounth() const{
  17. return mounth;
  18. }
  19. int GetDay() const{
  20. return day;
  21. }
  22. Date(const int& new_year, const int& new_mounth, const int& new_day){
  23. day = new_day;
  24. mounth = new_mounth;
  25. year = new_year;
  26. }
  27. private:
  28. int day;
  29. int mounth;
  30. int year;
  31. };
  32.  
  33. //Определяет количество цыфр в числе
  34. int CountNumbers(const int& number){
  35. int a = number, counter = 0
  36. ;
  37. while (a > 0){
  38. a = a / 10;
  39. counter ++;
  40. }
  41. return counter;
  42. }
  43.  
  44. ostream& operator<< (ostream& stream, Date date){
  45. int count = CountNumbers(date.GetYear());
  46. if (count < 4){
  47. stream << setfill('0');
  48. stream << setw(4);
  49. }
  50. stream << date.GetYear() << "-";
  51. count = CountNumbers(date.GetMounth());
  52. if (count < 2){
  53. stream << setfill('0');
  54. stream << setw(2);
  55. }
  56. stream << date.GetMounth() << "-";
  57. count = CountNumbers(date.GetDay());
  58. if (count < 2){
  59. stream << setfill('0');
  60. stream << setw(2);
  61. }
  62. stream << date.GetDay();
  63. return stream;
  64. }
  65.  
  66. bool operator< (const Date& lhs, const Date& rhs){
  67. if (lhs.GetYear() < rhs.GetYear()) {
  68. return true;
  69. }
  70. else if (lhs.GetYear() == rhs.GetYear()) {
  71. if (lhs.GetMounth() < rhs.GetMounth()) {
  72. return true;
  73. }
  74. else if (lhs.GetMounth() == rhs.GetMounth()) {
  75. if (lhs.GetDay() < rhs.GetDay()) {
  76. return true;
  77. }
  78. else {
  79. return false;
  80. }
  81. }
  82. else {
  83. return false;
  84. }
  85. }
  86. else {
  87. return false;
  88. }
  89. }
  90.  
  91. //Проверяет, является ли введенная дата целым числом
  92. void CheckInteger(const string& date){
  93. for (auto iter : date){
  94. if (iter == '.' || iter == ','){
  95. throw runtime_error("Wrong date format " + date);
  96. }
  97. }
  98. }
  99.  
  100. //Проверка количества дефисов
  101. void CheckValidDate(const string& date){
  102. if (date[0] == '-' && date[1] == '-'){
  103. throw runtime_error("Wrong date format: " + date);
  104. }
  105. for (int i = 2; i < date.size(); i++){
  106. if (date[i - 1] == '-' && date[i] == '-' && date[i + 1] == '-'){
  107. throw runtime_error("Wrong date format: " + date);
  108. }
  109. }
  110. }
  111.  
  112. void CheckValidMounthAndDay(const string& date){
  113. stringstream ss(date);
  114. int year, day, month;
  115.  
  116. ss >> year;
  117. ss.ignore(1);
  118. ss >> month;
  119. ss.ignore(1);
  120. ss >> day;
  121.  
  122. if (month < 1 || month > 12){
  123. throw runtime_error("Month value is invalid: " + to_string(month));
  124. }
  125. if (day < 1 || day > 32){
  126. throw runtime_error("Day value is invalid: " + to_string(day));
  127. }
  128. }
  129.  
  130. //Проверяет наличие введенной команды
  131. void FindCommand(const string& st){
  132. bool fl = false;
  133. vector<string> tmp = {"Add", "Del", "Find", "Print"};
  134. for (auto iter : tmp){
  135. if (st == iter){
  136. fl = true;
  137. }
  138. }
  139. if (fl == false){
  140. string error = "Unknown command: " + st;
  141. throw runtime_error(error);
  142. }
  143. }
  144.  
  145. //Запись введенной даты в класс
  146. Date GetDate(const string& date){
  147. stringstream ss(date);
  148. int k;
  149. vector<int> tmp;
  150. for (int i = 0; i < 3; i++){
  151. ss >> k;
  152. ss.ignore(1);
  153. tmp.push_back(k);
  154. k = 0;
  155. }
  156. Date d = {tmp[0], tmp[1], tmp[2]};
  157. return d;
  158. }
  159.  
  160. bool operator==(const Date& lhs, const Date& rhs){
  161. if (lhs.GetYear() == rhs.GetYear() && lhs.GetMounth() == rhs.GetMounth() &&
  162. lhs.GetDay() == rhs.GetDay()){
  163. return true;
  164. } else {
  165. return false;
  166. }
  167. }
  168.  
  169. class Database{
  170. public:
  171. void AddEvent(const Date& date, const string& event){
  172. database[date].insert(event);
  173. }
  174. void Print() const{
  175. for (auto iter : database){
  176. cout << iter.first << " ";
  177. for (auto jter : iter.second){
  178. cout << jter << " ";
  179. }
  180. cout << endl;
  181. }
  182. }
  183. void Find (const Date& date) const{
  184. for (auto iter : database.at(date)){
  185. cout << iter << endl;
  186. }
  187. }
  188. int DeleteDate(const Date& date) {
  189. if (database.count(date) > 0){
  190. int counter = database.at(date).size();
  191. database.erase(date);
  192. return counter;
  193. }
  194. return 0;
  195.  
  196. }
  197. bool DeleteEvent(const Date& date, const string& event){
  198. if (database.count(date) == 0 || database.at(date).count(event) == 0){
  199. return false;
  200. } else {
  201. set<string> tmp = database.at(date);
  202. tmp.erase(event);
  203. database[date] = tmp;
  204. return true;
  205. }
  206. }
  207.  
  208. private:
  209. map<Date, set<string>> database;
  210. };
  211.  
  212. int main(){
  213. Database db;
  214.  
  215. string line;
  216. while (getline(cin, line)){
  217. stringstream ss(line);
  218. string command, date, event;
  219. getline(ss,command,' ');
  220. try {
  221. FindCommand(command);
  222. if (command == "Print"){
  223. db.Print();
  224. } else {
  225. //т.к. для Print не нужна ни дата ни событие, то
  226. //далее выполняем операции не для Print
  227. getline(ss, date, ' ');
  228.  
  229. //Проверка даты
  230. CheckInteger(date);
  231. CheckValidDate(date);
  232. CheckValidMounthAndDay(date);
  233.  
  234. //Записываем введенную дату
  235. Date new_date = GetDate(date);
  236.  
  237. if (command == "Add"){
  238. getline(ss, event, '\0');
  239. db.AddEvent(new_date,event);
  240. }
  241. if (command == "Find"){
  242. db.Find(new_date);
  243. }
  244. if (command == "Del"){
  245. if (ss.rdbuf()->in_avail() == 0){
  246. //т.е. поток пустой, в нем не осталось события
  247. cout << "Deleted " << db.DeleteDate(new_date) << " events" << endl;
  248. } else {
  249. getline(ss, event, '\0');
  250. bool fl = db.DeleteEvent(new_date, event);
  251. if (fl == true){
  252. cout << "Deleted successfully" << endl;
  253. } else {
  254. cout << "Event not found";
  255. }
  256. }
  257. }
  258. }
  259. }catch (exception& ex){
  260. cout << ex.what() << endl;
  261. }
  262. }
  263. return 0;
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement