Advertisement
in_chainz

Untitled

Feb 19th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdexcept>
  3. #include <array>
  4. #include <tuple>
  5.  
  6. class Date {
  7. private:
  8.     int day, month, year;
  9.  
  10. public:
  11.     bool IsLeap() {
  12.         return (!(year % 4) && year % 100) || !(year % 400);
  13.     }
  14.  
  15.     int DayByMonth() {
  16.         std::array<int, 12> d = {31, 28 + IsLeap(), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  17.         return d[month - 1];
  18.     }
  19.  
  20.     bool Correct() {
  21.         return day > 0 && day <= DayByMonth() &&
  22.                month > 0 && month < 13;
  23.     }
  24.  
  25.     Date(int day_, int month_, int year_)
  26.             : day(day_),
  27.               month(month_),
  28.               year(year_) {
  29.         if (!Correct())
  30.             throw std::invalid_argument("Idet medved po lesu, "
  31.                                         "vidit - tank gorit, sel v nego "
  32.                                         "i pogib kak geroi");
  33.     }
  34.  
  35.     int GetDay() const {
  36.         return day;
  37.     }
  38.  
  39.     int GetMonth() const {
  40.         return month;
  41.     }
  42.  
  43.     int GetYear() const {
  44.         return year;
  45.     }
  46.  
  47.     void Increment() {
  48.         day++;
  49.         if (day > DayByMonth()) {
  50.             day = 1;
  51.             ++month;
  52.         }
  53.  
  54.         if (month > 12) {
  55.             year++;
  56.             month = 1;
  57.         }
  58.     }
  59.  
  60.     void Decrement() {
  61.         day--;
  62.         if (day < 1) {
  63.             --month;
  64.             if (month < 1) {
  65.                 month = 12;
  66.                 --year;
  67.             }
  68.             day = DayByMonth();
  69.         }
  70.     }
  71.  
  72.     Date &operator++() {
  73.         Increment();
  74.         return *this;
  75.     }
  76.  
  77.     Date &operator--() {
  78.         Decrement();
  79.         return *this;
  80.     }
  81. };
  82.  
  83. Date operator+(Date &cur, int ds) {
  84.     for (int i = 0; i < ds; ++i)
  85.         cur.Increment();
  86.     return cur;
  87. }
  88.  
  89. Date operator-(Date &cur, int ds) {
  90.     for (int i = 0; i < ds; ++i)
  91.         cur.Decrement();
  92.     return cur;
  93. }
  94.  
  95. bool operator<(const Date &one, const Date &two) {
  96.     int year_one = one.GetYear(),
  97.             month_one = one.GetMonth(),
  98.             day_one = one.GetDay(),
  99.             year_two = two.GetYear(),
  100.             month_two = two.GetMonth(),
  101.             day_two = two.GetDay();
  102.     return std::tie(year_one, month_one, day_one)
  103.            < std::tie(year_two, month_two, day_two);
  104. }
  105.  
  106. bool operator==(const Date &one, const Date &two) {
  107.     int year_one = one.GetYear(),
  108.             month_one = one.GetMonth(),
  109.             day_one = one.GetDay(),
  110.             year_two = two.GetYear(),
  111.             month_two = two.GetMonth(),
  112.             day_two = two.GetDay();
  113.     return std::tie(year_one, month_one, day_one)
  114.            == std::tie(year_two, month_two, day_two);
  115. }
  116.  
  117. bool operator!=(const Date &one, const Date &two) {
  118.     int year_one = one.GetYear(),
  119.             month_one = one.GetMonth(),
  120.             day_one = one.GetDay(),
  121.             year_two = two.GetYear(),
  122.             month_two = two.GetMonth(),
  123.             day_two = two.GetDay();
  124.     return !(std::tie(year_one, month_one, day_one)
  125.              == std::tie(year_two, month_two, day_two));
  126. }
  127.  
  128. int operator-(const Date &one, const Date &two) {
  129.     Date tmp = one;
  130.     int counter = 0;
  131.     if (tmp == two) {
  132.         return 0;
  133.     } else if (tmp < two) {
  134.         for (; tmp != two; ++tmp, ++counter) {}
  135.     } else {
  136.         for (; tmp != two; --tmp, --counter) {}
  137.     }
  138.     return -counter;
  139. }
  140.  
  141. std::ostream &operator<<(std::ostream &out, const Date &d) {
  142.     out << d.GetDay() << '.' << d.GetMonth() << '.' << d.GetYear();
  143.     return out;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement