Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Создайте класс с именем Date для хранения даты (или
- используйте ранее созданный).
- В классе должна быть функция-член, которая увеличивает день на 1.
- Напишите соответствующие конструкторы и функции-члены.
- В классе должны быть перегружены операциии ++, -- ,
- !=, ==, >, <, >>, <<, =, +=, -=, ().
- Используйте обычную и дружественную перегрузку.
- #include <iostream>
- class Date
- {
- uint16_t year;
- uint8_t month;
- uint8_t day;
- uint8_t checkMonth(uint8_t monthP)
- {
- if (monthP < 1) return 1;
- if (monthP > 12) return 12;
- return monthP;
- }
- uint8_t checkDay(uint8_t dayP, uint8_t monthP, uint16_t yearP)
- {
- const uint8_t maxDays{ Date::daysInMonth(monthP, yearP) };
- if (dayP < 1) return 1;
- if (dayP > maxDays) return maxDays;
- return dayP;
- }
- public:
- static uint8_t daysInMonth(uint8_t month, uint16_t year)
- {
- return 30 + (((month < 8) and (month % 2 != 0)) or ((month >= 8) and (month % 2 == 0))) + ((month == 2) * (-2) + isLeapYear(year));
- }
- static bool isLeapYear(uint16_t year)
- {
- return year % 400 == 0 or year % 4 == 0 and year % 100 != 0;
- }
- static const uint8_t maxMonth{ 12 };
- Date() : Date(1, 1, 1970) {}
- Date(uint8_t dayP, uint8_t monthP, uint16_t yearP)
- : year{ yearP }, month{ checkMonth(monthP) }, day{ checkDay(dayP,month,yearP) } {}
- Date& setDay(uint8_t dayP) { day = checkDay(dayP, month, year); return *this; }
- Date& setMonth(uint8_t monthP) { month = checkMonth(monthP); day = checkDay(day, month, year); return *this; }
- Date& setYear(uint16_t yearP) { year = yearP; return *this; }
- uint8_t getDay() const { return day; }
- uint8_t getMonth() const { return month; }
- uint16_t getYear() const { return year; }
- Date& init(uint8_t dayP, uint8_t monthP, uint16_t yearP);
- Date& operator=(const Date& object);
- Date& operator++();
- Date& operator--();
- Date operator++(int);
- Date operator--(int);
- Date& operator+=(int days);
- Date& operator-=(int days);
- friend std::ostream& operator<<(std::ostream& out, const Date& object);
- friend std::istream& operator>>(std::istream& in, Date& object);
- friend bool operator==(const Date& left, const Date& right);
- friend bool operator!=(const Date& left, const Date& right);
- friend bool operator>(const Date& left, const Date& right);
- friend bool operator<(const Date& left, const Date& right);
- int daysTillMonth(const Date& object);
- int operator()(const Date& object); // вертає різницю між датами
- };
- Date& Date::init(uint8_t dayP, uint8_t monthP, uint16_t yearP)
- {
- setDay(dayP); setMonth(monthP); setYear(yearP); return *this;
- }
- std::ostream& operator<<(std::ostream& out, const Date& object)
- {
- out << (int)object.day << '.' << (int)object.month << '.' << object.year; return out;
- }
- std::istream& operator>>(std::istream& in, Date& object)
- {
- in >> object.day >> object.month >> object.year;
- return in;
- }
- bool operator==(const Date& left, const Date& right)
- {
- return (left.day == right.day and left.month == right.month and left.year == right.year);
- }
- bool operator!=(const Date& left, const Date& right)
- {
- return !(left == right);
- }
- bool operator>(const Date& left, const Date& right)
- {
- if(left.year == right.year)
- {
- if (left.month == right.month) { return left.day > right.day; }
- return left.month > right.month;
- }
- return left.year > right.year;
- }
- bool operator<(const Date& left, const Date& right)
- {
- return !(left > right);
- }
- Date& Date::operator=(const Date& object)
- {
- day = object.day;
- month = object.month;
- year = object.year;
- return *this;
- }
- Date& Date::operator++()
- {
- const uint8_t maxDays{ Date::daysInMonth(month, year) };
- if (day == maxDays)
- {
- if (month == 12) { day = 1; month = 1; year += 1; }
- else { day = 1; month += 1; }
- }
- else { day += 1; }
- return *this;
- }
- Date Date::operator++(int)
- {
- Date old{ *this };
- ++(*this);
- return old;
- }
- Date& Date::operator--()
- {
- if (day == 1 and month == 1) { day = 31; month = 12; year -= 1; }
- else if (day == 1 and month != 1)
- {
- month -= 1;
- day = Date::daysInMonth(month, year);
- }
- else { day -= 1; }
- return *this;
- }
- Date Date::operator--(int)
- {
- Date old{ *this };
- --(*this);
- return old;
- }
- Date& Date::operator+=(int addDays)
- {
- const int monthInYear{ 12 };
- int daysInMonth[monthInYear]{ 31,28 + isLeapYear(year),31,30,31,30,31,31,30,31,30,31 };
- if (addDays <= (daysInMonth[month - 1] - day)) { day += addDays; }
- else
- {
- addDays -= (daysInMonth[month - 1] - day);
- ++month;
- for (int curMonth{ month - 1 }; curMonth < monthInYear; ++curMonth)
- {
- if (addDays > daysInMonth[curMonth]) { addDays -= daysInMonth[curMonth]; }
- else if (addDays <= daysInMonth[curMonth])
- {
- day = addDays;
- month = curMonth + 1;
- break;
- }
- if (curMonth == (monthInYear - 1))
- {
- ++year;
- daysInMonth[1] = 28 + isLeapYear(year);
- curMonth = -1;
- }
- }
- }
- return *this;
- }
- Date& Date::operator-=(int subDays)
- {
- const int monthInYear{ 12 };
- int daysInMonth[monthInYear]{ 31,28 + isLeapYear(year),31,30,31,30,31,31,30,31,30,31 };
- if (subDays <= day) { day -= subDays; }
- else
- {
- subDays -= day;
- --month;
- for (int curMonth{ month - 1 }; curMonth >= 0; --curMonth)
- {
- if (subDays > daysInMonth[curMonth]) { subDays -= daysInMonth[curMonth]; }
- else if (subDays <= daysInMonth[curMonth])
- {
- if (subDays < daysInMonth[curMonth]) { day = daysInMonth[curMonth] - subDays; month = curMonth + 1;}
- else
- {
- day = daysInMonth[curMonth - 1];
- month = curMonth;
- }
- break;
- }
- if (curMonth == 0 )
- {
- --year;
- daysInMonth[1] = 28 + isLeapYear(year);
- curMonth = 12;
- }
- }
- }
- return *this;
- }
- int Date::daysTillMonth(const Date& object)
- {
- const int monthInYear{ 12 };
- const int daysInMonth[monthInYear]{ 31,28 + isLeapYear(object.year),31,30,31,30,31,31,30,31,30,31 };
- int days{ 0 };
- for (int curMonth{ 0 }; curMonth < object.month - 1; days += daysInMonth[curMonth++]);
- return days;
- }
- int Date::operator()(const Date& object)
- {
- bool max{ *this > object };
- const int daysInYear{ 365 };
- int days{ (max ? year - object.year : object.year - year) * daysInYear };
- for (int yearP{ max ? object.year : year }; yearP <= (max ? year : object.year); days += isLeapYear(yearP++));
- int dayBegin{ max ? object.day : day };
- int dayEnd{ max ? day : object.day };
- days -= (daysTillMonth(max ? object : *this) + dayBegin);
- days += (daysTillMonth(max ? *this : object) + dayEnd);
- return days;
- }
- int main()
- {
- Date date_1{ 31,12,2019 };
- Date date_2{ 1,1,2020 };
- Date date_3{ 28,2,2020 };
- Date date_4{ 28,2,2021 };
- Date date_5 = date_1;
- std::cout << "date_1 = " << date_1 << '\n' << "date_2 = " << date_2 << '\n' << "date_3 = " << date_3
- << '\n' << "date_4 = " << date_4 << '\n' << "date_5 = " << date_5 << "\n\n";
- std::cout << "(++date_1) = " << ++date_1 << "\n\n";
- std::cout << std::boolalpha << date_1 << " == " << date_2 << " ? " << (date_1 == date_2) << "\n\n";
- std::cout << "(date_2--) = " << date_2-- << " -> date_2 = " << date_2 << "\n\n";
- std::cout << std::boolalpha << date_1 << " != " << date_2 << " ? " << (date_1 != date_2) << "\n\n";
- std::cout << "(++date_3) = " << ++date_3 << "\n\n";
- std::cout << "(++date_4) = " << ++date_4 << "\n\n";
- std::cout << "(--date_5) = " << --date_5 << "\n\n";
- std::cout << (date_3 += 100) << "\n\n";
- std::cout << (date_3 += 3) << "\n\n";
- std::cout << (date_3 += 20) << "\n\n";
- std::cout << (date_3 += 365) << "\n\n";
- std::cout << (date_3 -= 365) << "\n\n";
- std::cout << (date_3 -= 20) << "\n\n";
- std::cout << (date_3 -= 3) << "\n\n";
- std::cout << (date_3 -= 100) << "\n\n";
- std::cout << "Between " << date_1 << " and " << date_4 << ' ' << date_1(date_4) << " days\n\n";
- std::cout << "Between " << date_4 << " and " << date_1 << ' ' << date_4(date_1) << " days\n";
- return 0;
- }
Add Comment
Please, Sign In to add comment