Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <map>
- #include <set>
- #include <vector>
- #include <sstream>
- #include <algorithm>
- #include <iomanip>
- #include <exception>
- #include <cctype>
- using namespace std;
- class Date {
- public:
- Date() {}
- Date(int Year, int Month, int Day) {
- year = Year;
- month = Month;
- day = Day;
- }
- int GetYear() const {
- return year;
- }
- int GetMonth() const {
- return month;
- }
- int GetDay() const {
- return day;
- }
- private:
- int year;
- int month;
- int day;
- };
- bool operator<(const Date& lhs, const Date& rhs) {
- if (lhs.GetYear() < rhs.GetYear()) {
- return true;
- }
- else if (lhs.GetYear() == rhs.GetYear()) {
- if (lhs.GetMonth() < rhs.GetMonth()) {
- return true;
- }
- else if (lhs.GetMonth() == rhs.GetMonth()) {
- if (lhs.GetDay() < rhs.GetDay()) {
- return true;
- }
- else {
- return false;
- }
- }
- else {
- return false;
- }
- }
- else {
- return false;
- }
- }
- Date DateParse(const string& str) {
- int year, month, day;
- Date date;
- stringstream dateStream;
- dateStream.str(str);
- dateStream >> year;
- if (year > 9999 || year < 0) {
- string ex = "Wrong date format: " + str;
- throw runtime_error(ex);
- }
- if (dateStream.peek() == '-') {
- dateStream.ignore(1);
- }
- else {
- string ex = "Wrong date format: " + str;
- throw runtime_error(ex);
- }
- dateStream >> month;
- if (dateStream.peek() == '-') {
- dateStream.ignore(1);
- }
- else {
- string ex = "Wrong date format: " + str;
- throw runtime_error(ex);
- }
- dateStream >> day;
- if (dateStream.peek() != EOF) {
- string ex = "Wrong date format: " + str;
- throw runtime_error(ex);
- }
- if (month < 1 || month > 12) {
- string ex = "Month value is invalid: " + to_string(month);
- throw runtime_error(ex);
- }
- if (day < 1 || day > 31) {
- string ex = "Day value is invalid: " + to_string(day);
- throw runtime_error(ex);
- }
- return { year, month, day };
- }
- istream& operator>>(istream& stream, Date& date) {
- int year, month, day;
- char c;
- stream >> year >> c >> month >> c >> day;
- date = Date(year, month, day);
- return stream;
- }
- ostream& operator<<(ostream& stream, const Date& date) {
- return stream << date.GetYear() << "-" << date.GetMonth() << "-" << date.GetDay();
- }
- class Database {
- public:
- void AddEvent(const Date& date, const string& event) {
- Event[date].insert(event);
- }
- bool DeleteEvent(const Date& date, const string& event) {
- if (Event[date].erase(event) != 0) {
- return true;
- }
- else return false;
- }
- int DeleteDate(const Date& date) {
- if (Event.count(date) > 0) {
- int n = Event.at(date).size();
- Event.erase(date);
- return n;
- }
- else return 0;
- }
- void Find(const Date& date) const {
- for (const auto& item : Event.at(date)) {
- cout << item << endl;
- }
- }
- void Print() const {
- for (auto item_map : Event) {
- for (auto item_set : item_map.second) {
- cout << setw(4) << setfill('0') << item_map.first.GetYear() << '-' << setw(2) << setfill('0') << item_map.first.GetMonth() << '-' << setw(2) << setfill('0') << item_map.first.GetDay() << ' ' << item_set << endl;
- }
- }
- };
- private:
- map<Date, set<string>> Event;
- };
- int main() {
- Database db;
- Date date;
- string command;
- while (getline(cin, command)) {
- if (command == " " || command == "" || command == "/n") {
- continue;
- }
- istringstream streamer(command);
- string command2, input_date, input_event;
- streamer >> command2 >> input_date >> input_event;
- if (command2 == "Add") {
- try {
- if (input_event != "") {
- db.AddEvent(DateParse(input_date), input_event);
- }
- }
- catch (runtime_error& er) {
- cout << er.what() << endl;
- }
- }
- else if (command2 == "Print") {
- db.Print();
- }
- else if (command2 == "Find") {
- db.Find(DateParse(input_date));
- }
- else if (command2 == "Del") {
- try {
- if (input_event.empty()) {
- int n = db.DeleteDate(DateParse(input_date));
- cout << "Deleted " << n << " events" << endl;
- }
- else {
- if (db.DeleteEvent(DateParse(input_date), input_event)) {
- cout << "Deleted successfully" << endl;
- }
- else cout << "Event not found" << endl;
- }
- }
- catch (runtime_error& er) {
- cout << er.what() << endl;
- }
- }
- else {
- cout << "Unknown command : " << command2 << endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement