Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<exception>
- #include<map>
- #include<set>
- #include<sstream>
- #include<iomanip>
- #include<vector>
- using namespace std;
- class Date {
- public:
- Date(int year, int month, int day) {
- this->year = year;
- if (month > 12 || month < 1)
- throw exception();
- this->month = month;
- if (day > 31 || day < 1)
- throw exception();
- this->day = day;
- }
- int GetYear() {
- return year;
- }
- int GetMonth() {
- return month;
- }
- int GetDay() {
- return day;
- }
- private:
- int year;
- int month;
- int day;
- };
- class DataBase {
- public:
- void AddEvent(const Date& date, const string& event) {
- holidays[date].insert(event);
- }
- bool DeleteEvent(const Date& date, const string& event) {
- if (holidays.count(date) > 0 && holidays[date].count(event) > 0)
- holidays[date].erase(event);
- }
- int DeleteDate(const Date& date)
- {
- if (holidays.count(date) == 0)
- return 0;
- else {
- int event_counts = holidays[date].size();
- holidays.erase(date);
- return event_counts;
- }
- }
- set<string> FindDate(const Date& date) {
- set<string> events;
- if (holidays.count(date) > 0) {
- for (const auto& x : holidays[date])
- events.insert(x);
- return events;
- }
- else
- return {};
- }
- void Print() const {
- for (const auto& item : holidays) {
- for (const string& event : item.second) {
- cout << item.first << " " << event << endl;
- }
- }
- }
- private:
- map<Date, set<string>> holidays;
- };
- Date ParseDate(const string& s) {
- stringstream s_stream(s);
- int year;
- s_stream >> year;
- if (s_stream.peek() == '-')
- throw exception();
- s_stream.ignore(1);
- int month;
- s_stream >> month;
- if (s_stream.peek() == '-')
- throw exception();
- s_stream.ignore(1);
- int day;
- s_stream >> day;
- return Date(year, month, day);
- }
- bool operator<(const Date& lhs, const Date& rhs) {
- return vector<int>{lhs.GetYear(), lhs.GetMonth(), lhs.GetDay()} < vector<int>{rhs.GetYear(), rhs.GetMonth(), rhs.GetDay()};
- }
- ostream& operator<<(istream& stream, const Date& date) {
- stream << setw(4) << setfill('0') << date.GetYear() << "-" << setw(2) << setfill('0') << date.GetMonth() << "-" << setw(2) << setfill('0') << date.GetDay();
- return stream;
- }
- int main() {
- try {
- DataBase db;
- string command_line;
- while (getline(cin, command_line)) {
- stringstream ss(command_line);
- string command;
- ss >> command;
- if (command == "Add") {
- string date_str, event;
- ss >> date_str >> event;
- const Date date = ParseDate(date_str);
- db.AddEvent(date, event);
- }
- else if (command == "Del") {
- string date_str;
- ss >> date_str;
- string event;
- if (!ss.eof()) {
- ss >> event;
- }
- const Date date = ParseDate(date_str);
- if (event.empty()) {
- const int count = db.DeleteDate(date);
- cout << "Deleted " << count << " events" << endl;
- }
- else {
- if (db.DeleteEvent(date, event)) {
- cout << "Deleted successfully" << endl;
- }
- else {
- cout << "Event not found" << endl;
- }
- }
- }
- else if (command == "Find") {
- string date_str;
- ss >> date_str;
- const Date date = ParseDate(date_str);
- for (const string& event : db.FindDate(date)) {
- cout << event << endl;
- }
- }
- else if (command == "Print") {
- db.Print();
- }
- else if (command.empty()) {
- break;
- }
- else {
- throw logic_error("Unknown command: " + command);
- }
- }
- }
- catch (const exception& e) {
- cout << e.what() << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement