#include <bits/stdc++.h> using namespace std;
class History { public: double food_rating; double svc_rating; double env_rating; string date; int version; History* next = nullptr; History* prev = nullptr; History(double f, double s, double e, string da, int v): food_rating(f), svc_rating(s), env_rating(e), date(da), version(v) {} };
class Custom { public: Custom(int d): id(d), head(new History(-1, -1, -1, "", 0)), version(1), latest(nullptr) {} int id; int version; History* head, *latest; History* createNewHistory() { if (latest == nullptr) { latest = new History(0, 0, 0, "", version); version++; head->next = latest; latest->prev = head; return latest; } else { latest->next = new History(latest->food_rating, latest->svc_rating, latest->env_rating, latest->date, version++); latest->next->prev = latest; return latest->next; } } };
class Manager { public: vector<Custom*> customs; Custom* add(int id) { Custom* cus = new Custom(id); customs.push_back(cus); return cus; } void insert(int id, double food, double svc, double env, string date) { Custom* cus = add(id); History* init = cus->createNewHistory(); init->food_rating = food, init->svc_rating = svc, init->env_rating = env, init->date = date; cus->latest = init; } bool modify(int id, double food, double svc, double env, string date) { auto it = find_if(customs.begin(), customs.end(), [&id](const Custom* tmp) { return tmp->id == id; }); if (it == customs.end()) { return false; } History* his = (*it)->createNewHistory(); if (food != -1) { his->food_rating = food; } if (svc != -1) { his->svc_rating = svc; } if (env != -1) { his->env_rating = env; } his->date = date; (*it)->latest = his; return true; } bool del(int id) { auto it = find_if(customs.begin(), customs.end(), [&id](const Custom* tmp) { return tmp->id == id; }); if (it == customs.end()) { return false; } customs.erase(remove(customs.begin(), customs.end(), *it)); return true; } void showHistory(int id, string dim) { cout << "History:" << endl; auto it = find_if(customs.begin(), customs.end(), [&id](const Custom* tmp) { return tmp->id == id; }); History* ptr = (*it)->latest; while (ptr != (*it)->head) { cout << "Version " << ptr->version << ": "; if (dim == "") { cout << "Food Rating " << ptr->food_rating << " Service Rating " << ptr->svc_rating << " Environment Rating " << ptr->env_rating << " Timestamp " << ptr->date << endl; } else { if (dim == "food") { cout << "Food Rating " << ptr->food_rating; } else if (dim == "service") { cout << "Service Rating " << ptr->svc_rating; } else if (dim == "environment") { cout << "Environment Rating " << ptr->env_rating; } cout << " Timestamp " << ptr->date << endl; } ptr = ptr->prev; } } void display(string dim) { sort(customs.begin(), customs.end(), [](const auto& a, const auto& b) { return a->latest->date > b->latest->date; }); if (dim != "") { for (const auto cus : customs) { History* ptr = cus->latest; cout << "Customer ID " << cus->id; if (dim == "food") { cout << " Food Rating " << ptr->food_rating; } else if (dim == "service") { cout << " Service Rating " << ptr->svc_rating; } else if (dim == "environment") { cout << " Environment Rating " << ptr->env_rating; } cout << " Timestamp " << ptr->date << endl; } } else { for (const auto cus : customs) { History* ptr = cus->latest; cout << "Customer ID " << cus->id << " Food Rating " << ptr->food_rating << " Service Rating " << ptr->svc_rating << " Environment Rating " << ptr->env_rating << " Timestamp " << ptr->date << endl; } } } void query(double food_lower, double food_upper, double svc_lower, double svc_upper, double env_lower, double env_upper) { sort(customs.begin(), customs.end(), [](const auto& a, const auto& b) { return a->id > b->id; }); for (const auto& cus : customs) { History* cu = cus->latest; if (cu->food_rating >= food_lower && cu->food_rating <= food_upper && cu->svc_rating >= svc_lower && cu->svc_rating <= svc_upper && cu->env_rating >= env_lower && cu->env_rating <= env_upper) { History* ptr = cus->latest; cout << "Customer ID " << cus->id << " Food Rating " << ptr->food_rating << " Service Rating " << ptr->svc_rating << " Environment Rating " << ptr->env_rating << " Timestamp " << ptr->date << endl; } } } };
void parse(Manager* manager, string line) { istringstream stream(line); string token, date; int id; stream >> token; if (token == "insert") { stream >> id; double food, svc, env; stream >> food >> svc >> env >> date >> token; date = date + " " + token; manager->insert(id, food, svc, env, date); cout << "Review inserted successfully" << endl; } else if (token == "modify") { stream >> id; double food = -1, svc = -1, env = -1; while (stream >> token && !(token[0] >= '0' && token[0] <= '9')) { if (token == "food") { stream >> food; } else if (token == "service") { stream >> svc; } else if (token == "environment") { stream >> env; } } stream >> date; date = token + " " + date; if (manager->modify(id, food, svc, env, date)) { cout << "Modification successful" << endl; } else { cout << "Customer ID not found, modification failed" << endl; } } else if (token == "delete") { stream >> id; if (manager->del(id)) { cout << "Deletion successful" << endl; } else { cout << "Customer ID not found, deletion failed" << endl; } } else if (token == "history") { string dim; stream >> id; if (!(stream >> dim)) { dim = ""; } manager->showHistory(id, dim); } else if (token == "range_query") { double food_lower = 1.0, food_upper = 5.0, svc_lower = 1.0, svc_upper = 5.0, env_lower = 1.0, env_upper = 5.0; double tmp; double *addr[] = {&food_lower, &food_upper, &svc_lower, &svc_upper, &env_lower, &env_upper}; int i = 0; while (stream >> tmp) { *(addr[i++]) = tmp; } manager->query(food_lower, food_upper, svc_lower, svc_upper, env_lower, env_upper); } else if (token == "display") { string dim; if (!(stream >> dim)) { dim = ""; } manager->display(dim); } else { assert(0); } }
int main() { int n; cin >> n; Manager* manager = new Manager(); string line; getline(cin, line); for (int i = 0; i < n; i++) { getline(cin, line); parse(manager, line); } }
|