avr39ripe

cppDateClassAdvancedGittAndryiVersion

Aug 1st, 2021
106
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Создайте класс с именем Date для хранения даты (или
  2. используйте ранее созданный).
  3. В классе должна быть функция-член, которая увеличивает день на 1.
  4. Напишите соответствующие конструкторы и функции-члены.
  5. В классе должны быть перегружены операциии ++, -- ,
  6. !=, ==, >, <, >>, <<, =, +=, -=, ().
  7. Используйте обычную и дружественную перегрузку.
  8.  
  9. #include <iostream>
  10.  
  11. class Date
  12. {
  13.     uint16_t year;
  14.     uint8_t month;
  15.     uint8_t day;
  16.  
  17.     uint8_t checkMonth(uint8_t monthP)
  18.     {
  19.         if (monthP < 1) return 1;
  20.         if (monthP > 12) return 12;
  21.         return monthP;
  22.     }
  23.     uint8_t checkDay(uint8_t dayP, uint8_t monthP, uint16_t yearP)
  24.     {
  25.         const uint8_t maxDays{ Date::daysInMonth(monthP, yearP) };
  26.         if (dayP < 1) return 1;
  27.         if (dayP > maxDays) return maxDays;
  28.         return dayP;
  29.     }
  30. public:
  31.     static uint8_t daysInMonth(uint8_t month, uint16_t year)
  32.     {
  33.         return 30 + (((month < 8) and (month % 2 != 0)) or ((month >= 8) and (month % 2 == 0))) + ((month == 2) * (-2) + isLeapYear(year));
  34.     }
  35.  
  36.     static bool isLeapYear(uint16_t year)
  37.     {
  38.         return year % 400 == 0 or year % 4 == 0 and year % 100 != 0;
  39.     }
  40.     static const uint8_t maxMonth{ 12 };
  41.  
  42.     Date() : Date(1, 1, 1970) {}
  43.     Date(uint8_t dayP, uint8_t monthP, uint16_t yearP)
  44.         : year{ yearP }, month{ checkMonth(monthP) }, day{ checkDay(dayP,month,yearP) } {}
  45.  
  46.     Date& setDay(uint8_t dayP) { day = checkDay(dayP, month, year); return *this; }
  47.     Date& setMonth(uint8_t monthP) { month = checkMonth(monthP); day = checkDay(day, month, year); return *this; }
  48.     Date& setYear(uint16_t yearP) { year = yearP; return *this; }
  49.  
  50.     uint8_t getDay() const { return day; }
  51.     uint8_t getMonth() const { return month; }
  52.     uint16_t getYear() const { return year; }
  53.  
  54.     Date& init(uint8_t dayP, uint8_t monthP, uint16_t yearP);
  55.  
  56.     Date& operator=(const Date& object);
  57.     Date& operator++();
  58.     Date& operator--();
  59.     Date operator++(int);
  60.     Date operator--(int);
  61.     Date& operator+=(int days);
  62.     Date& operator-=(int days);
  63.  
  64.     friend std::ostream& operator<<(std::ostream& out, const Date& object);
  65.     friend std::istream& operator>>(std::istream& in, Date& object);
  66.  
  67.     friend bool operator==(const Date& left, const Date& right);
  68.     friend bool operator!=(const Date& left, const Date& right);
  69.     friend bool operator>(const Date& left, const Date& right);
  70.     friend bool operator<(const Date& left, const Date& right);
  71.    
  72.     int daysTillMonth(const Date& object);
  73.  
  74.     int operator()(const Date& object); // вертає різницю між датами
  75. };
  76.  
  77. Date& Date::init(uint8_t dayP, uint8_t monthP, uint16_t yearP)
  78. {
  79.     setDay(dayP); setMonth(monthP); setYear(yearP); return *this;
  80. }
  81.  
  82. std::ostream& operator<<(std::ostream& out, const Date& object)
  83. {
  84.     out << (int)object.day << '.' << (int)object.month << '.' << object.year; return out;
  85. }
  86.  
  87. std::istream& operator>>(std::istream& in, Date& object)
  88. {
  89.     in >> object.day >> object.month >> object.year;
  90.     return in;
  91. }
  92.  
  93. bool operator==(const Date& left, const Date& right)
  94. {
  95.     return (left.day == right.day and left.month == right.month and left.year == right.year);
  96. }
  97.  
  98. bool operator!=(const Date& left, const Date& right)
  99. {
  100.     return !(left == right);
  101. }
  102.  
  103. bool operator>(const Date& left, const Date& right)
  104. {
  105.     if(left.year == right.year)
  106.     {
  107.         if (left.month == right.month) { return left.day > right.day; }
  108.         return left.month > right.month;
  109.     }
  110.     return left.year > right.year;
  111. }
  112. bool operator<(const Date& left, const Date& right)
  113. {
  114.     return !(left > right);
  115. }
  116.  
  117. Date& Date::operator=(const Date& object)
  118. {
  119.     day = object.day;
  120.     month = object.month;
  121.     year = object.year;
  122.     return *this;
  123. }
  124.  
  125. Date& Date::operator++()
  126. {
  127.     const uint8_t maxDays{ Date::daysInMonth(month, year) };
  128.     if (day == maxDays)
  129.     {
  130.         if (month == 12) { day = 1; month = 1; year += 1; }
  131.         else { day = 1; month += 1; }
  132.     }
  133.     else { day += 1; }
  134.  
  135.     return *this;
  136. }
  137.  
  138. Date Date::operator++(int)
  139. {
  140.     Date old{ *this };
  141.     ++(*this);
  142.     return old;
  143. }
  144.  
  145. Date& Date::operator--()
  146. {
  147.     if (day == 1 and month == 1) { day = 31; month = 12; year -= 1; }
  148.     else if (day == 1 and month != 1)
  149.     {
  150.         month -= 1;
  151.         day = Date::daysInMonth(month, year);
  152.     }
  153.     else { day -= 1; }
  154.  
  155.     return *this;
  156. }
  157.  
  158. Date Date::operator--(int)
  159. {
  160.     Date old{ *this };
  161.     --(*this);
  162.     return old;
  163. }
  164.  
  165. Date& Date::operator+=(int addDays)
  166. {
  167.     const int monthInYear{ 12 };
  168.     int daysInMonth[monthInYear]{ 31,28 + isLeapYear(year),31,30,31,30,31,31,30,31,30,31 };
  169.     if (addDays <= (daysInMonth[month - 1] - day)) { day += addDays; }
  170.     else
  171.     {
  172.         addDays -= (daysInMonth[month - 1] - day);
  173.         ++month;
  174.         for (int curMonth{ month - 1 }; curMonth < monthInYear; ++curMonth)
  175.         {
  176.             if (addDays > daysInMonth[curMonth]) { addDays -= daysInMonth[curMonth]; }
  177.             else if (addDays <= daysInMonth[curMonth])
  178.             {
  179.                 day = addDays;
  180.                 month = curMonth + 1;
  181.                 break;
  182.             }
  183.             if (curMonth == (monthInYear - 1))
  184.             {
  185.                 ++year;
  186.                 daysInMonth[1] = 28 + isLeapYear(year);
  187.                 curMonth = -1;
  188.             }
  189.         }
  190.     }
  191.     return *this;
  192. }
  193.  
  194. Date& Date::operator-=(int subDays)
  195. {
  196.     const int monthInYear{ 12 };
  197.     int daysInMonth[monthInYear]{ 31,28 + isLeapYear(year),31,30,31,30,31,31,30,31,30,31 };
  198.     if (subDays <= day) { day -= subDays; }
  199.     else
  200.     {
  201.         subDays -= day;
  202.         --month;
  203.         for (int curMonth{ month - 1 }; curMonth >= 0; --curMonth)
  204.         {
  205.             if (subDays > daysInMonth[curMonth]) { subDays -= daysInMonth[curMonth]; }
  206.             else if (subDays <= daysInMonth[curMonth])
  207.             {
  208.                 if (subDays < daysInMonth[curMonth]) { day = daysInMonth[curMonth] - subDays; month = curMonth + 1;}
  209.                 else
  210.                 {
  211.                     day = daysInMonth[curMonth - 1];
  212.                     month = curMonth;
  213.                 }
  214.                 break;
  215.             }
  216.             if (curMonth == 0 )
  217.             {
  218.                 --year;
  219.                 daysInMonth[1] = 28 + isLeapYear(year);
  220.                 curMonth = 12;
  221.             }
  222.         }
  223.     }
  224.     return *this;
  225. }
  226.  
  227. int Date::daysTillMonth(const Date& object)
  228. {
  229.     const int monthInYear{ 12 };
  230.     const int daysInMonth[monthInYear]{ 31,28 + isLeapYear(object.year),31,30,31,30,31,31,30,31,30,31 };
  231.     int days{ 0 };
  232.  
  233.     for (int curMonth{ 0 }; curMonth < object.month - 1; days += daysInMonth[curMonth++]);
  234.  
  235.     return days;
  236. }
  237.  
  238. int Date::operator()(const Date& object)
  239. {
  240.     bool max{ *this > object };
  241.     const int daysInYear{ 365 };
  242.     int days{ (max ? year - object.year : object.year - year) * daysInYear };
  243.  
  244.     for (int yearP{ max ? object.year : year }; yearP <= (max ? year : object.year); days += isLeapYear(yearP++));
  245.  
  246.     int dayBegin{ max ? object.day : day };
  247.     int dayEnd{ max ? day : object.day };
  248.     days -= (daysTillMonth(max ? object : *this) + dayBegin);
  249.     days += (daysTillMonth(max ? *this : object) + dayEnd);
  250.     return days;
  251. }
  252.  
  253.  
  254.  
  255. int main()
  256. {
  257.     Date date_1{ 31,12,2019 };
  258.     Date date_2{ 1,1,2020 };
  259.     Date date_3{ 28,2,2020 };
  260.     Date date_4{ 28,2,2021 };
  261.     Date date_5 = date_1;
  262.    
  263.     std::cout << "date_1 = " << date_1 << '\n' << "date_2 = " << date_2 << '\n' << "date_3 = " << date_3
  264.         << '\n' << "date_4 = " << date_4 << '\n' << "date_5 = " << date_5 << "\n\n";
  265.     std::cout << "(++date_1) = " << ++date_1 << "\n\n";
  266.     std::cout << std::boolalpha << date_1 << " == " << date_2 << " ? " << (date_1 == date_2) << "\n\n";
  267.     std::cout << "(date_2--) = " << date_2-- << " -> date_2 = " << date_2 << "\n\n";
  268.     std::cout << std::boolalpha << date_1 << " != " << date_2 << " ? " << (date_1 != date_2) << "\n\n";
  269.     std::cout << "(++date_3) = " << ++date_3 << "\n\n";
  270.     std::cout << "(++date_4) = " << ++date_4 << "\n\n";
  271.     std::cout << "(--date_5) = " << --date_5 << "\n\n";
  272.  
  273.     std::cout << (date_3 += 100) << "\n\n";
  274.     std::cout << (date_3 += 3) << "\n\n";
  275.     std::cout << (date_3 += 20) << "\n\n";
  276.     std::cout << (date_3 += 365) << "\n\n";
  277.  
  278.     std::cout << (date_3 -= 365) << "\n\n";
  279.     std::cout << (date_3 -= 20) << "\n\n";
  280.     std::cout << (date_3 -= 3) << "\n\n";
  281.     std::cout << (date_3 -= 100) << "\n\n";
  282.  
  283.     std::cout << "Between " << date_1 << " and " << date_4 << ' ' << date_1(date_4) << " days\n\n";
  284.     std::cout << "Between " << date_4 << " and " << date_1 << ' ' << date_4(date_1) << " days\n";
  285.  
  286.     return 0;
  287. }
  288.  
RAW Paste Data