Advertisement
kompilainenn

Untitled

Jun 6th, 2022
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. class Date
  8. {
  9. public:
  10.  
  11.     Date (const string & date)
  12.     {
  13. //        if (stoi(date.substr(0,2)) > 31 || stoi(date.substr(3,2)) > 12 ||
  14. //               (stoi(date.substr(0,2)) > 29 && stoi (date.substr(3,2)) == 2))
  15. //             throw "Error! A non valid date!";
  16.  
  17.         this->day = stoi(date.substr(0,2));
  18.         this->month = stoi(date.substr(3,2));
  19.         this->year = stoi(date.substr(6,4));
  20.     }
  21.  
  22.     string DateAfter (string& date, int days);
  23.  
  24.     string DateBefore (string& date, int days);
  25.  
  26.     bool LeapYear (string& date)
  27.     {
  28.         return this->year % 4 == 0;
  29.     }
  30.  
  31.     void SetDay (unsigned day)
  32.     {
  33.         this->day = day;
  34.     }
  35.  
  36.     void SetMonth (unsigned month)
  37.     {
  38.         this->month = month;
  39.     }
  40.  
  41.     void SetYear (unsigned year)
  42.     {
  43.         this->year = year;
  44.     }
  45.  
  46.     unsigned GetDay ()
  47.     {
  48.         return this->day;
  49.     }
  50.  
  51.     unsigned GetMonth ()
  52.     {
  53.         return this->month;
  54.     }
  55.  
  56.     unsigned GetYear ()
  57.     {
  58.         return this->year;
  59.     }
  60.  
  61.     //compare dates
  62.  
  63.     bool operator == (Date & other)
  64.     {
  65.         return this->day == other.day && this->month == other.month && this->year == other.year;
  66.     }
  67.  
  68.     bool operator != (Date & other)
  69.     {
  70.         return this->day != other.day || this->month != other.month || this->year == other.year;
  71.     }
  72.  
  73.     unsigned DaysBetween ();
  74.  
  75. private:
  76.  
  77.     unsigned day;
  78.     unsigned month;
  79.     unsigned year;
  80. //    const static enum daysInMonth = {31;28;31;30;31;30;31;31;30;31;30;31};
  81.  
  82. };
  83.  
  84. int main()
  85. {
  86.     cout << "Enter the date in dd.mm.yyyy format: ";
  87.     string new_date;
  88.     getline(cin, new_date);
  89.     try
  90.     {
  91.     Date first_date(new_date);
  92.     }
  93.     catch (const std::exception &ex)
  94.     {
  95.         cout << ex.what() << endl;
  96.     }
  97.     cout << "The year of the first date is " << first_date.GetYear();
  98.  
  99.     return 0;
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement