Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <set>
- #include <map>
- #include <exception>
- #include <sstream>
- #include <string>
- #include <iomanip>
- using namespace std;
- //вспомогательные функции
- int CountWords(const string& s) {
- stringstream is(s);
- int number_of_words = 0;
- string word;
- while (is >> word) {number_of_words++;}
- return number_of_words;
- }
- void WrongDateFormat(const stringstream& flow, const string& date) {
- string error = "Wrong date format: " + date;
- throw runtime_error(error);
- }
- void EnsureNextSymbolAndSkip(stringstream& stream, const string& input_date) {
- if (stream.peek() != '-') {
- WrongDateFormat(stream, input_date);
- }
- stream.ignore(1);
- }
- void EnsureCorrectFinal(stringstream& stream, const string& input_date) {
- if (stream.peek() != EOF) {
- WrongDateFormat(stream, input_date);
- }
- }
- //структура Дейт, функции и операторы для нее
- struct Date {
- int year = 0;
- int month = 0;
- int day = 0;
- };
- void operator<<(ostream& output, const Date& date) {
- output << setfill('0');
- output << setw(4) << date.year << '-'
- << setw(2) << date.month << '-' << setw(2) << date.day;
- }
- bool operator<(const Date& lhs, const Date& rhs) {
- if (lhs.year == rhs.year) {
- if (lhs.month == lhs.month) {
- return lhs.day < rhs.day;
- }
- return lhs.month < rhs.month;
- }
- return lhs.year < rhs.year;
- }
- Date ParseDate(const string& s){
- Date date;
- string save_the_date = s;
- stringstream stream(s);
- stream >> date.year;
- EnsureNextSymbolAndSkip(stream, save_the_date);
- stream >> date.month;
- EnsureNextSymbolAndSkip(stream, save_the_date);
- stream >> date.day;
- EnsureCorrectFinal(stream, save_the_date);
- return date;
- }
- void CheckDate (const Date& new_date) {
- int new_month = new_date.month;
- if (new_month > 12 || new_month < 1) {
- string ss = "Month value is invalid: " + to_string(new_month);
- throw runtime_error(ss);
- }
- int new_day = new_date.day;
- if (new_day > 31 || new_day < 1) {
- string ss = "Day value is invalid: " + to_string(new_day);
- throw runtime_error(ss);
- }
- }
- //КЛАСС
- class Database {
- private:
- map<Date, set<string>> db;
- public:
- void AddEvent(const Date& date, const string& some_event) {
- db[date].insert(some_event);
- }
- bool DeleteEvent(const Date& date, const string& some_event) {
- if (db.count(date) == 0 || db[date].count(some_event) == 0) {
- return false;
- //cout << "Event not found" << endl;
- }
- else {
- set<string> day = db[date];
- day.erase(some_event);
- db[date] = day;
- return true;
- //cout << "Deleted successfully" << endl;
- }
- }
- int DeleteDate(Date& date) {
- int res = db.at(date).size();
- db.erase(date);
- return res;
- }
- void FindDate(const Date& date) const{
- if (db.count(date) > 0) {
- for (auto s: db.at(date)) {
- cout << s << endl;
- }
- }
- }
- void PrintAll() const{
- for (auto x:db) {
- set<string> events = x.second;
- for (string s: events) {
- cout << x.first;
- cout << " " << s << endl;
- }
- }
- }
- };
- //Проверка команды из потока ввода
- void CheckCommand(const string& some_command, Database& database) {
- set<string> commands = {"Del", "Find", "Add"};
- string action;
- string input_date;
- string new_event;
- int words = CountWords(some_command);
- stringstream stream(some_command);
- if (words == 1) {
- if (some_command == "Print") {
- database.PrintAll();
- }
- else {
- string error = "Unknown command: " + some_command;
- throw runtime_error(error);
- }
- }
- else if (words > 1) {
- getline(stream, action, ' ');
- if (commands.count(action) == 0 ) {
- string error = "Unknown command: " + action;
- throw runtime_error(error);
- }
- else if (words == 2) {
- getline(stream, input_date);
- Date new_date = ParseDate(input_date);
- CheckDate(new_date);
- if (action == "Del") {
- cout << "Deleted " << database.DeleteDate(new_date) << " events" << endl;
- }
- else if (action == "Find") {
- database.FindDate(new_date);
- }
- }
- else if (words == 3){
- getline(stream, input_date, ' ');
- Date new_date = ParseDate(input_date);
- CheckDate(new_date);
- getline(stream, new_event);
- if (action == "Del") {
- if (database.DeleteEvent(new_date, new_event)) {
- cout << "Deleted successfully" << endl;
- }
- else {
- cout << "Event not found" << endl;
- }
- }
- else if (action == "Add") {
- database.AddEvent(new_date, new_event);
- }
- }
- }
- }
- //мэйн
- int main() {
- Database yandex;
- string command;
- while (getline(cin, command)) {
- try {
- if (command > "") {
- CheckCommand(command, yandex);
- }
- } catch (exception& ex) {
- cout << ex.what() << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement