Advertisement
Argentum_02

main.cpp

Apr 9th, 2019
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. #include "database.h"
  2. #include "date.h"
  3. #include "condition_parser.h"
  4. #include <stdexcept>
  5.  
  6. #include "node.h"
  7. #include "test_runner.h"
  8. using namespace std;
  9.  
  10. string ParseEvent(istream& is) {
  11.   // Реализуйте эту функцию
  12.     string res;
  13.     getline(is,res);
  14.     while(res[0]==' '){
  15.         res.erase(0,1);
  16.     }
  17.     return res;
  18. }
  19.  
  20. void TestAll();
  21.  
  22. template <typename T> std::string type_name();
  23.  
  24. int main() {
  25.   TestAll();
  26.  
  27.   Database db;
  28.  
  29.   for (string line; getline(cin, line); ) {
  30.     istringstream is(line);
  31.  
  32.     string command;
  33.     is >> command;
  34.     if (command == "Add") {
  35.       const auto date = ParseDate(is);
  36.       const auto event = ParseEvent(is);
  37.       db.Add(date, event);
  38.     } else if (command == "Print") {
  39.       db.Print(cout);
  40.     } else if (command == "Del") {
  41.       auto condition = ParseCondition(is);
  42.       auto predicate = [condition](const Date& date, const string& event) {
  43.         return condition->Evaluate(date, event);
  44.       };
  45.       int count = db.RemoveIf(predicate);
  46.       cout << "Removed " << count << " entries" << endl;
  47.     } else if (command == "Find") {
  48.       auto condition = ParseCondition(is);
  49.       auto predicate = [condition](const Date& date, const string& event) {
  50.         return condition->Evaluate(date, event);
  51.       };
  52.  
  53.       const auto entries = db.FindIf(predicate);
  54.       for (const auto& entry : entries) {
  55.         cout << entry << endl;
  56.       }
  57.       cout << "Found " << entries.size() << " entries" << endl;
  58.     } else if (command == "Last") {
  59.       try {
  60.           cout << db.Last(ParseDate(is)) << endl;
  61.       } catch (invalid_argument&) {
  62.           cout << "No entries" << endl;
  63.       }
  64.  
  65.     } else if (command.empty()) {
  66.       continue;
  67.     } else {
  68.       throw logic_error("Unknown command: " + command);
  69.     }
  70.   }
  71.  
  72.   return 0;
  73. }
  74.  
  75. void TestParseEvent() {
  76.   {
  77.     istringstream is("event");
  78.     AssertEqual(ParseEvent(is), "event", "Parse event without leading spaces");
  79.   }
  80.   {
  81.     istringstream is("   sport event ");
  82.     AssertEqual(ParseEvent(is), "sport event ", "Parse event with leading spaces");
  83.   }
  84.   {
  85.     istringstream is("  first event  \n  second event");
  86.     vector<string> events;
  87.     events.push_back(ParseEvent(is));
  88.     events.push_back(ParseEvent(is));
  89.     AssertEqual(events, vector<string>{"first event  ", "second event"}, "Parse multiple events");
  90.   }
  91. }
  92.  
  93. void TestAll() {
  94.   TestRunner tr;
  95.   tr.RunTest(TestParseEvent, "TestParseEvent");
  96.   tr.RunTest(TestParseCondition, "TestParseCondition");
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement