Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <map>
- #include <vector>
- #include <set>
- #include <iomanip>
- using namespace std;
- class Date {
- public:
- int GetYear() const {
- return year;
- }
- int GetMonth() const{
- return month;
- }
- int GetDay() const {
- return day;
- }
- void PutDay(const int& d){
- day = d;
- //return day;
- }
- void PutMonth (const int& m){
- month = m;
- //return month;
- }
- void PutYear(const int& y){
- year = y;
- //return year;
- }
- private:
- int year;
- int month;
- int day;
- };
- bool operator<(const Date& lhs, const Date& rhs){
- if(lhs.GetYear() == rhs.GetYear() && lhs.GetMonth() == rhs.GetMonth()){
- return lhs.GetDay() < rhs.GetDay();
- }
- else if (lhs.GetYear() == rhs.GetYear()){
- return lhs.GetMonth() < rhs.GetMonth();
- }
- else return lhs.GetYear() < rhs.GetYear();
- }
- bool operator==(const Date& lhs, const Date& rhs){
- return (lhs.GetYear() == rhs.GetYear() &&
- lhs.GetMonth() == rhs.GetMonth() &&
- lhs.GetDay() == rhs.GetDay());
- }
- class Database {
- public:
- void AddEvent(const Date& date, const string& event){
- Map_DE [date].insert(event);
- }
- bool DeleteEvent(const Date& date, const string& event){
- bool r = 0;
- if (Map_DE.count(date)!= 0){
- for (const auto& i:Map_DE.at(date)){
- if (i == event) {
- Map_DE.at(date).erase(event);
- r = 1;
- cout << "Deleted successfully" << endl;
- return r;
- }
- }
- }
- cout << "Event not found" << endl;
- return r;
- }
- int DeleteDate(const Date& date){
- int n = 0;
- if ( Map_DE.count(date)!= 0){
- set <string> nt;
- nt = Map_DE.at(date);
- n = nt.size();
- Map_DE.erase(date);
- return n;
- }
- return n;
- }
- void Find(const Date& date) const{
- if ( Map_DE.count(date)!= 0){
- for (const auto& i:Map_DE.at(date)){
- cout << i << endl;
- }
- }
- }
- void Print() const {
- for(const auto& m: Map_DE){
- for(const auto& a: m.second){
- cout << setw(4) << setfill('0') << m.first.GetYear() << "-" <<
- setw(2) << m.first.GetMonth() << "-" << setw(2) << m.first.GetDay() <<" "<< a << endl;
- }
- }
- }
- private:
- map <Date, set<string>> Map_DE;
- };
- void Ensure (stringstream& stream) {
- if (stream.peek() != '-') {
- throw exception();
- }
- stream.ignore(1);
- }
- void Ensure_end (stringstream& stream) {
- if (stream.peek() == EOF) {
- throw exception();
- }
- }
- Date ParseDate (const string& _date){
- Date newdate;
- stringstream input (_date);
- int yy, dd, mm;
- input >> yy;
- if(!input >> yy){
- throw exception();
- }
- Ensure(input);
- input >> mm;
- if(!input >> mm) {
- throw exception();
- }
- Ensure(input);
- input >> dd;
- if(!input >> dd) {
- throw exception();
- }
- Ensure_end(input);
- if (mm < 1 || mm > 12 ){
- throw invalid_argument("Month value is invalid: " + to_string(mm));
- }
- else if (dd < 1 || dd > 31){
- throw domain_error("Day value is invalid: " + to_string(dd));
- }
- else if (mm > 0 && mm < 13 && dd > 0 && dd < 32){
- newdate.PutDay(dd);
- newdate.PutMonth(mm);
- newdate.PutYear(yy);
- return newdate;
- }
- }
- int main() {
- Database db;
- string command;
- while (getline(cin, command)) {
- stringstream stream (command);
- string comm, _date, event;
- stream >> comm;
- stream >> _date;
- stream >> event;
- Date date;
- try {
- if(comm == "Add" ){
- date = ParseDate (_date);
- db.AddEvent(date, event);
- }
- else if(comm == "Del"){
- date = ParseDate (_date);
- if(event == ""){
- cout << "Deleted " << db.DeleteDate(date) << " events" << endl;
- }
- else {
- db.DeleteEvent(date, event);
- }
- }
- else if(comm == "Find"){
- date = ParseDate (_date);
- db.Find(date);
- }
- else if(comm == "Print"){
- db.Print();
- }
- else if(comm == ""){
- }
- else cout << "Unknown command: " << comm << endl;
- }
- catch (invalid_argument& ii){
- cout << ii.what();
- return 0;
- }
- catch (domain_error& ex3){
- cout << ex3.what();
- return 0;
- }
- catch (exception& ex1){
- cout << "Wrong date format: " << _date << endl;
- return 0;
- }
- }
- return 0;
- }
- /*
- Add 0-1-2 event1
- Add 1-2-3 event2
- Find 0-1-2
- Del 0-1-2
- Print
- Del 1-2-3 event2
- Del 1-2-3 event2
- вывод
- event1
- Deleted 1 events
- 0001-02-03 event2
- Deleted successfully
- Event not found
- Month value is invalid: MONTH
- Add 1--1-32 task
- Add 1-0-32 task
- Add 1-13-32 task
- Add 1-+0-32 task
- Day value is invalid: DAY
- Add 1-1--1 task
- Add 1-1-0 task
- Add 1-1-+0 task
- Add 1-1-32 task
- Wrong date format: DATE
- Add f8-1-1 task
- Add 9f-1-1 task
- Add 1-11f-1 task
- Add 1-f12-1 task
- Add 1-1-f13 task!
- Add 1-1-14f task
- Add --15-1-1 task
- Add 1---2-1 task
- Add 1-1---17 task
- Add 1-1- task
- Add 1--1 task
- Add --111 task
- Add 1---+++1 task
- Add --++1 task
- Add 1-1-f task
- Del foo
- Add 1-0- event1
- OK:
- Add 1-1-1 task
- Add -2-1-1 task
- Add 03-01-01 task
- Add +4-1-1 task
- Add 1-+6-1 task
- Add 1-1-+7 task
- Sorting:
- Input:
- Add 1-1-1 C
- Add 1-1-1 A
- Add 1-1-1 B
- Add 1-2-1 task2
- Add 1-1-2 task3
- Add 2-1-1 task1
- Output:
- 0001-01-01 A
- 0001-01-01 B
- 0001-01-01 C
- 0001-01-02 task3
- 0001-02-01 task2
- 0002-01-01 task1
- Input:
- Find 1-1-1
- Output:
- A
- B
- C
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement