Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <map>
- #include <string>
- #include <vector>
- #include <algorithm>
- #include <exception>
- #include <set>
- #include <iostream>
- #include <sstream>
- #include <iomanip>
- using namespace std;
- int CountDash(istream& f) {
- int c = 0;
- while (f.peek() == '-') {
- char h;
- f >> h;
- ++c;
- }
- for (int i = 0; i<c; i++) {
- f.unget();
- }
- return c;
- }
- class Date {
- public:
- Date(int& i, int& k, int& m) {
- year = i;
- month = k;
- day = m;
- if ((m > 31) || (m <= 0)) {
- if ((k > 12) || (k <= 0)) throw invalid_argument("Month value is invalid: " + to_string(k));
- else throw invalid_argument("Day value is invalid: " + to_string(m));
- }
- else if ((k > 12) || (k <= 0)) throw invalid_argument("Month value is invalid: " + to_string(k));
- }
- int GetYear() const {
- return year;
- };
- int GetMonth() const {
- return month;
- };
- int GetDay() const {
- return day;
- };
- private:
- int year, month, day;
- };
- bool operator<(const Date& lhs, const Date& rhs) {
- if (lhs.GetYear() < rhs.GetYear()) return true;
- else if ((lhs.GetYear() == rhs.GetYear()) && (lhs.GetMonth() < rhs.GetMonth())) return true;
- else if ((lhs.GetYear() == rhs.GetYear()) && (lhs.GetMonth() == rhs.GetMonth()) && (lhs.GetDay() < rhs.GetDay())) return true;
- else return false;
- }
- bool operator==(const Date& lhs, const Date& rhs) {
- if ((lhs.GetYear() == rhs.GetYear()) && (lhs.GetMonth() == rhs.GetMonth()) && (lhs.GetDay() == rhs.GetDay())) return true;
- else return false;
- }
- void PrintDate(Date D) {
- cout << setfill('0');
- cout << setw(4) << D.GetYear();
- cout << setfill('0');
- cout << "-";
- cout << setw(2) << D.GetMonth();
- cout << setfill('0');
- cout << "-";
- cout << setw(2) << D.GetDay();
- cout << setfill('0');
- cout << " ";
- }
- class Database {
- public:
- void AddEvent(const Date& date, const string& event) {
- data[date].insert(event);
- };
- void DeleteEvent(const Date& date, const string& event) {
- if (data[date].count(event) == 1) {
- data[date].erase(event);
- cout << "Deleted successfully" << endl;;
- }
- else {
- cout << "Event not found" << endl;
- }
- };
- int DeleteDate(const Date& date) {
- set <string> s = data.at(date);
- int n = s.size();
- data.erase(date);
- cout << "Deleted " + to_string(n) + " events"<< endl;
- return n;
- };
- void Find(const Date& date) const {
- set <string> s = data.at(date);
- for (auto& i : s) {
- cout << i << " ";
- }
- cout << endl;
- };
- void Print() const {
- for (auto& t : data) {
- PrintDate(t.first);
- set <string> s = data.at(t.first);
- for (auto& i : s) {
- cout << i << " ";
- }
- cout << endl;
- }
- };
- private:
- map<Date, set <string>> data;
- };
- Date CheckDate(string& q) {
- stringstream ss(q);
- int y, m, d;
- if (isdigit(ss.peek())) {
- ss >> y;
- if (CountDash(ss) == 1) {
- while (ss.peek() == '-') {
- char c;
- ss >> c;
- }
- if (isdigit(ss.peek())) {
- ss >> m;
- }
- }
- else if (CountDash(ss) == 2) {
- while (ss.peek() == '-') {
- char c;
- ss >> c;
- }
- if (isdigit(ss.peek())) {
- ss >> m;
- m = m*(-1);
- }
- }
- else throw invalid_argument("Wrong date format: " + q);
- if (CountDash(ss) == 1) {
- while (ss.peek() == '-') {
- char c;
- ss >> c;
- }
- if (isdigit(ss.peek())) {
- ss >> d;
- }
- }
- else if (CountDash(ss) == 2) {
- while (ss.peek() == '-') {
- char c;
- ss >> c;
- }
- if (isdigit(ss.peek())) {
- ss >> d;
- d = d*(-1);
- }
- }
- else invalid_argument("Wrong date format: " + q);
- if (ss.peek() != EOF) invalid_argument("Wrong date format: " + q);
- }
- else if (ss.peek() == '-') {
- char c;
- ss >> c;
- if (isdigit(ss.peek())) {
- ss >> y;
- y = y*(-1);
- if (CountDash(ss) == 1) {
- while (ss.peek() == '-') {
- char c;
- ss >> c;
- }
- if (isdigit(ss.peek())) {
- ss >> m;
- }
- }
- else if (CountDash(ss) == 2) {
- while (ss.peek() == '-') {
- char c;
- ss >> c;
- }
- if (isdigit(ss.peek())) {
- ss >> m;
- m = m*(-1);
- cout << m << endl;
- }
- }
- else throw invalid_argument("Wrong date format: " + q);
- if (CountDash(ss) == 1) {
- while (ss.peek() == '-') {
- char c;
- ss >> c;
- }
- if (isdigit(ss.peek())) {
- ss >> d;
- }
- }
- else if (CountDash(ss) == 2) {
- while (ss.peek() == '-') {
- char c;
- ss >> c;
- }
- if (isdigit(ss.peek())) {
- ss >> d;
- d = d*(-1);
- }
- }
- else throw invalid_argument("Wrong date format: " + q);
- if (ss.peek() != EOF) throw invalid_argument("Wrong date format: " + q);
- }
- }
- else invalid_argument("Wrong date format: " + q);
- return{ y, m, d };
- }
- int main() {
- Database db;
- string command;
- while (getline(cin, command)) {
- stringstream ss(command);
- string f;
- ss >> f;
- if (f.size() != 0) {
- if (f == "Add") {
- try {
- string g, h;
- ss >> g >> h;
- Date date = CheckDate(g);
- db.AddEvent(date, h);
- }
- catch (invalid_argument& ex) {
- cout << ex.what();
- }
- }
- else if (f == "Del") {
- string g;
- ss >> g;
- Date date = CheckDate(g);
- if (!ss.eof()) {
- string h;
- ss >> h;
- db.DeleteEvent(date, h);
- }
- else db.DeleteDate(date);
- }
- else if (f == "Find") {
- string g;
- ss >> g;
- Date date = CheckDate(g);
- db.Find(date);
- }
- else if (f == "Print") db.Print();
- else cout << "Unknown command: " << command << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement