Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<map>
- #include<string>
- #include<iomanip>
- #include<sstream>
- #include<set>
- #include<exception>
- using namespace std;
- class Date {
- public:
- int GetYear() const {
- return y;
- }
- int GetMonth() const {
- return m;
- }
- int GetDay() const {
- return d;
- }
- Date(){}
- Date(int a, int b, int c) {
- y = a;
- m = b;
- d = c;
- }
- private:
- int y;
- int m;
- int d;
- };
- bool operator<(const Date& lhs, const Date& rhs) {
- string left, right;
- stringstream sl, sr;
- sl.fill('0');
- sr.fill('0');
- sl << setw(4) << lhs.GetYear() << setw(2) << lhs.GetMonth() << setw(2) << lhs.GetDay();
- sr << setw(4) << rhs.GetYear() << setw(2) << rhs.GetMonth() << setw(2) << rhs.GetDay();
- getline(sl, left);
- getline(sr, right);
- return (left < right);
- }
- class Database {
- public:
- void AddEvent(const Date& date, const string& event) {
- db[date].insert(event);
- }
- bool DeleteEvent(const Date& date, const string& event) {
- if (db.at(date).count(event) == 1) {
- db[date].erase(event);
- return true;
- }
- else return false;
- }
- int DeleteDate(const Date& date) {
- int q = db.at(date).size();
- db.erase(date);
- return q;
- }
- void Find(const Date& date) const {
- for ( const auto& i : db.at(date)) {
- cout << i << endl;
- }
- }
- void Print() const {
- cout.fill('0');
- for (const auto& i : db) {
- for (const auto& j : i.second) {
- cout << setw(4) << i.first.GetYear() <<'-'<< setw(2) << i.first.GetMonth() <<'-'<< setw(2) << i.first.GetDay() << " " << j << endl;
- }
- }
- }
- private:
- map<Date, set<string> > db;
- };
- Date rd(string& inp) {
- stringstream in(inp);
- int a=0, b=0, c=0;
- string io="Wrong date format: ";
- char s, s1=0, s2=0;
- if (!isdigit(in.peek()) && in.peek()!=' ') {
- throw invalid_argument(io+inp);
- }
- else {
- while (isdigit(in.peek())) {
- s = in.peek();
- a = a * 10 + (s - 48);
- in.ignore(1);
- }
- if (in.peek() != '-') {
- cout << in.peek();
- throw invalid_argument(io+inp);
- }
- in >> s1;
- if (!isdigit(in.peek()) && in.peek() != '-') {
- throw invalid_argument(io + inp);
- }
- char z=in.peek();
- if(z=='-')
- in.ignore(1);
- while (isdigit(in.peek())) {
- s = in.peek();
- b = b * 10 + (s - 48);
- in.ignore(1);
- }
- if (z == '-') b *= -1;
- if (in.peek() != '-') {
- throw invalid_argument(io + inp);
- }
- in >> s2;
- if (!isdigit(in.peek()) && in.peek()!='-' ) {
- throw invalid_argument(io + inp);
- }
- char z1 = in.peek();
- if(z1 == '-')
- in.ignore(1);
- while (isdigit(in.peek())) {
- s = in.peek();
- c = c * 10 + (s- 48);
- in.ignore(1);
- }
- if (z1 == '-') c *= -1;
- if (in.peek() != ' ' && !in.eof()) {
- throw invalid_argument(io + inp);
- }
- }
- if (a > 9999 || a < 0) throw exception();
- if (b > 12 || b<=0) throw invalid_argument("Month value is invalid: " + to_string(b));
- else if (c > 31 || c <= 0) throw invalid_argument("Day value is invalid:" + to_string(c));
- Date d(a, b, c);
- return d;
- }
- int main() {
- Database db;
- string command;
- while (getline(cin, command)) {
- try {
- string com, name,date;
- stringstream in(command);
- Date d;
- in >> com;
- if (com == "Add") {
- try {
- try {
- in >>date >> name;
- d = rd(date);
- if (name != "") {
- db.AddEvent(d, name);
- }
- }
- catch (exception &e) {
- cout << e.what() << endl;
- break;
- }
- }
- catch (exception& e) {
- }
- }
- else if (com == "Del") {
- try { in >> date;
- d = rd(date); }
- catch (exception &e) {
- cout << e.what() << endl;
- break;
- }
- if (!in.eof()) {
- try {
- in >> name;
- if (db.DeleteEvent(d, name) == true) {
- cout << "Deleted successfully" << endl;
- }
- else cout << "Event not found" << endl;
- }
- catch (exception& e) {
- cout << "Event not found" << endl;
- }
- }
- else {
- try {
- cout << "Deleted " << db.DeleteDate(d) << " events" << endl;
- }
- catch (exception& e) {
- cout << "Deleted 0 events" << endl;
- }
- }
- }
- else if (com == "Find") {
- try {
- try {
- in >> date;
- d = rd(date); }
- catch (exception &e) {
- cout << e.what() << endl;
- break;
- }
- db.Find(d);
- }
- catch (exception& e) {
- }
- }
- else if (com == "Print") {
- db.Print();
- }
- else if (command != "") {
- try {
- throw invalid_argument("Unknown command: " + com);
- }
- catch (exception &e) {
- cout << e.what() << endl;
- break;
- }
- }
- }
- catch (exception& e) {
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment