Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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& print() { std::cout << +day << '.' << +month << '.' << year << '\n'; return *this; }
- };
- int main()
- {
- std::cout << std::boolalpha << (Date::isLeapYear(2020) == true) << '\n';
- std::cout << std::boolalpha << (Date::isLeapYear(2021) == false) << '\n';
- std::cout << std::boolalpha << (Date::isLeapYear(1800) == false) << '\n';
- std::cout << std::boolalpha << (Date::isLeapYear(1600) == true) << '\n';
- std::cout << std::boolalpha << (Date::daysInMonth(12, 2021) == 31) << '\n';
- std::cout << std::boolalpha << (Date::daysInMonth(1, 2021) == 31) << '\n';
- std::cout << std::boolalpha << (Date::daysInMonth(4, 2021) == 30) << '\n';
- std::cout << std::boolalpha << (Date::daysInMonth(2, 2021) == 28) << '\n';
- std::cout << std::boolalpha << (Date::daysInMonth(2, 2020) == 29) << '\n';
- Date date1;
- std::cout << std::boolalpha << (date1.getDay() == 1) << '\n';
- std::cout << std::boolalpha << (date1.getMonth() == 1) << '\n';
- std::cout << std::boolalpha << (date1.getYear() == 1970) << '\n';
- Date date2{ 26,7,1981 };
- std::cout << std::boolalpha << (date2.getDay() == 26) << '\n';
- std::cout << std::boolalpha << (date2.getMonth() == 7) << '\n';
- std::cout << std::boolalpha << (date2.getYear() == 1981) << '\n';
- date2.setDay(32);
- std::cout << std::boolalpha << (date2.getDay() == 31) << '\n';
- date2.setDay(0);
- std::cout << std::boolalpha << (date2.getDay() == 1) << '\n';
- date2.setDay(31);
- date2.setMonth(2);
- std::cout << std::boolalpha << (date2.getDay() == 28) << '\n';
- std::cout << std::boolalpha << (date2.getMonth() == 2) << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement