Advertisement
Art_Uspen

Untitled

Feb 28th, 2021
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdexcept>
  3. #include <unordered_map>
  4. #include <tuple>
  5.  
  6. const std::unordered_map<int, int> month_day = {{1,  31},
  7.                                                 {2,  28},
  8.                                                 {3,  31},
  9.                                                 {4,  30},
  10.                                                 {5,  31},
  11.                                                 {6,  30},
  12.                                                 {7,  31},
  13.                                                 {8,  31},
  14.                                                 {9,  30},
  15.                                                 {10, 31},
  16.                                                 {11, 30},
  17.                                                 {12, 31}};
  18.  
  19. class Date {
  20. private:
  21.     int day_;
  22.     int month_;
  23.     int year_;
  24.  
  25. public:
  26.     Date(int day, int month, int year) {
  27.         if ((year % 400 == 0 && month == 2) || (year % 100 != 0 && year % 4 == 0 && month == 2)) {
  28.             if (day < 1 || day > 29) {
  29.                 throw std::invalid_argument("invalid data");
  30.             } else {
  31.                 day_ = day;
  32.                 month_ = month;
  33.                 year_ = year;
  34.             }
  35.         } else {
  36.             if (month > 12 || day < 1 || day > month_day.at(month)) {
  37.                 throw std::invalid_argument("invalid data");
  38.             } else {
  39.                 day_ = day;
  40.                 month_ = month;
  41.                 year_ = year;
  42.             }
  43.         }
  44.     }
  45.  
  46.     [[nodiscard]] int GetDay() const {
  47.         return day_;
  48.     }
  49.  
  50.     [[nodiscard]] int GetMonth() const {
  51.         return month_;
  52.     }
  53.  
  54.     [[nodiscard]] int GetYear() const {
  55.         return year_;
  56.     }
  57.  
  58.     Date &operator++() {
  59.         if ((year_ % 400 == 0 && month_ == 2) ||
  60.             (year_ % 100 != 0 && year_ % 4 == 0 && month_ == 2)) {
  61.             if (day_ == 28) {
  62.                 day_ = 29;
  63.                 return *this;
  64.             } else if (day_ == 29) {
  65.                 day_ = 1;
  66.                 month_ = 3;
  67.                 return *this;
  68.             } else {
  69.                 ++day_;
  70.                 return *this;
  71.             }
  72.         } else if (day_ == month_day.at(month_)) {
  73.             if (month_ == 12) {
  74.                 day_ = 1;
  75.                 month_ = 1;
  76.                 ++year_;
  77.                 return *this;
  78.             } else {
  79.                 day_ = 1;
  80.                 ++month_;
  81.                 return *this;
  82.             }
  83.         } else {
  84.             ++day_;
  85.             return *this;
  86.         }
  87.     }
  88.  
  89.     Date operator--() {
  90.         if ((year_ % 400 == 0 && month_ == 3) ||
  91.             (year_ % 100 != 0 && year_ % 4 == 0 && month_ == 3)) {
  92.             if (day_ == 1) {
  93.                 day_ = 29;
  94.                 month_ = 2;
  95.                 return *this;
  96.             } else {
  97.                 --day_;
  98.                 return *this;
  99.             }
  100.         } else {
  101.             if (day_ == 1) {
  102.                 if (month_ == 1) {
  103.                     day_ = 31;
  104.                     month_ = 12;
  105.                     --year_;
  106.                     return *this;
  107.                 } else {
  108.                     // std::cout << day_ << " " << month_ <<" "<< year_ << '\n';
  109.                     day_ = month_day.at(month_ - 1);
  110.                     --month_;
  111.                     return *this;
  112.                 }
  113.             } else {
  114.                 --day_;
  115.                 return *this;
  116.             }
  117.         }
  118.     }
  119. };
  120.  
  121. Date operator+(const Date &first, int number) {
  122.     Date new_date = first;
  123.     int i = 0;
  124.     while (i != number) {
  125.         ++new_date;
  126.         ++i;
  127.     }
  128.     return new_date;
  129. }
  130.  
  131. Date operator-(const Date &first, int number) {
  132.     Date new_date = first;
  133.     int i = 0;
  134.     while (i != number) {
  135.         --new_date;
  136.         ++i;
  137.     }
  138.     return new_date;
  139. }
  140.  
  141. bool operator<(const Date &first, const Date &second) {
  142.     auto y_first = first.GetYear();
  143.     auto m_first = first.GetMonth();
  144.     auto d_first = first.GetDay();
  145.     auto y_second = second.GetYear();
  146.     auto m_second = second.GetMonth();
  147.     auto d_second = second.GetDay();
  148.     return std::tie(y_first, m_first, d_first) <
  149.            std::tie(y_second, m_second, d_second);
  150. }
  151.  
  152. int operator-(const Date &second, const Date &first) {
  153.     Date new_first = std::min(first, second);
  154.     Date new_second = std::max(first, second);
  155.     int count = 0;
  156.     while ((new_first < new_second)) {
  157.         --new_second;
  158.         ++count;
  159.     }
  160.     return count;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement