Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <sstream>
- #include <iomanip>
- #include <map>
- #include <set>
- using namespace std;
- class Date{
- public:
- int GetYear() const{
- return year;
- }
- int GetMounth() const{
- return mounth;
- }
- int GetDay() const{
- return day;
- }
- Date(const int& new_year, const int& new_mounth, const int& new_day){
- day = new_day;
- mounth = new_mounth;
- year = new_year;
- }
- private:
- int day;
- int mounth;
- int year;
- };
- //Определяет количество цыфр в числе
- int CountNumbers(const int& number){
- int a = number, counter = 0
- ;
- while (a > 0){
- a = a / 10;
- counter ++;
- }
- return counter;
- }
- ostream& operator<< (ostream& stream, Date date){
- int count = CountNumbers(date.GetYear());
- if (count < 4){
- stream << setfill('0');
- stream << setw(4);
- }
- stream << date.GetYear() << "-";
- count = CountNumbers(date.GetMounth());
- if (count < 2){
- stream << setfill('0');
- stream << setw(2);
- }
- stream << date.GetMounth() << "-";
- count = CountNumbers(date.GetDay());
- if (count < 2){
- stream << setfill('0');
- stream << setw(2);
- }
- stream << date.GetDay();
- return stream;
- }
- bool operator< (const Date& lhs, const Date& rhs){
- if (lhs.GetYear() < rhs.GetYear()) {
- return true;
- }
- else if (lhs.GetYear() == rhs.GetYear()) {
- if (lhs.GetMounth() < rhs.GetMounth()) {
- return true;
- }
- else if (lhs.GetMounth() == rhs.GetMounth()) {
- if (lhs.GetDay() < rhs.GetDay()) {
- return true;
- }
- else {
- return false;
- }
- }
- else {
- return false;
- }
- }
- else {
- return false;
- }
- }
- //Проверяет, является ли введенная дата целым числом
- void CheckInteger(const string& date){
- for (auto iter : date){
- if (iter == '.' || iter == ','){
- throw runtime_error("Wrong date format " + date);
- }
- }
- }
- //Проверка количества дефисов
- void CheckValidDate(const string& date){
- if (date[0] == '-' && date[1] == '-'){
- throw runtime_error("Wrong date format: " + date);
- }
- for (int i = 2; i < date.size(); i++){
- if (date[i - 1] == '-' && date[i] == '-' && date[i + 1] == '-'){
- throw runtime_error("Wrong date format: " + date);
- }
- }
- }
- void CheckValidMounthAndDay(const string& date){
- stringstream ss(date);
- int year, day, month;
- ss >> year;
- ss.ignore(1);
- ss >> month;
- ss.ignore(1);
- ss >> day;
- if (month < 1 || month > 12){
- throw runtime_error("Month value is invalid: " + to_string(month));
- }
- if (day < 1 || day > 32){
- throw runtime_error("Day value is invalid: " + to_string(day));
- }
- }
- //Проверяет наличие введенной команды
- void FindCommand(const string& st){
- bool fl = false;
- vector<string> tmp = {"Add", "Del", "Find", "Print"};
- for (auto iter : tmp){
- if (st == iter){
- fl = true;
- }
- }
- if (fl == false){
- string error = "Unknown command: " + st;
- throw runtime_error(error);
- }
- }
- //Запись введенной даты в класс
- Date GetDate(const string& date){
- stringstream ss(date);
- int k;
- vector<int> tmp;
- for (int i = 0; i < 3; i++){
- ss >> k;
- ss.ignore(1);
- tmp.push_back(k);
- k = 0;
- }
- Date d = {tmp[0], tmp[1], tmp[2]};
- return d;
- }
- bool operator==(const Date& lhs, const Date& rhs){
- if (lhs.GetYear() == rhs.GetYear() && lhs.GetMounth() == rhs.GetMounth() &&
- lhs.GetDay() == rhs.GetDay()){
- return true;
- } else {
- return false;
- }
- }
- class Database{
- public:
- void AddEvent(const Date& date, const string& event){
- database[date].insert(event);
- }
- void Print() const{
- for (auto iter : database){
- cout << iter.first << " ";
- for (auto jter : iter.second){
- cout << jter << " ";
- }
- cout << endl;
- }
- }
- void Find (const Date& date) const{
- for (auto iter : database.at(date)){
- cout << iter << endl;
- }
- }
- int DeleteDate(const Date& date) {
- if (database.count(date) > 0){
- int counter = database.at(date).size();
- database.erase(date);
- return counter;
- }
- return 0;
- }
- bool DeleteEvent(const Date& date, const string& event){
- if (database.count(date) == 0 || database.at(date).count(event) == 0){
- return false;
- } else {
- set<string> tmp = database.at(date);
- tmp.erase(event);
- database[date] = tmp;
- return true;
- }
- }
- private:
- map<Date, set<string>> database;
- };
- int main(){
- Database db;
- string line;
- while (getline(cin, line)){
- stringstream ss(line);
- string command, date, event;
- getline(ss,command,' ');
- try {
- FindCommand(command);
- if (command == "Print"){
- db.Print();
- } else {
- //т.к. для Print не нужна ни дата ни событие, то
- //далее выполняем операции не для Print
- getline(ss, date, ' ');
- //Проверка даты
- CheckInteger(date);
- CheckValidDate(date);
- CheckValidMounthAndDay(date);
- //Записываем введенную дату
- Date new_date = GetDate(date);
- if (command == "Add"){
- getline(ss, event, '\0');
- db.AddEvent(new_date,event);
- }
- if (command == "Find"){
- db.Find(new_date);
- }
- if (command == "Del"){
- if (ss.rdbuf()->in_avail() == 0){
- //т.е. поток пустой, в нем не осталось события
- cout << "Deleted " << db.DeleteDate(new_date) << " events" << endl;
- } else {
- getline(ss, event, '\0');
- bool fl = db.DeleteEvent(new_date, event);
- if (fl == true){
- cout << "Deleted successfully" << endl;
- } else {
- cout << "Event not found";
- }
- }
- }
- }
- }catch (exception& ex){
- cout << ex.what() << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement