Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdexcept>
- #include <array>
- #include <tuple>
- class Date {
- private:
- int day, month, year;
- public:
- bool IsLeap() {
- return (!(year % 4) && year % 100) || !(year % 400);
- }
- int DayByMonth() {
- std::array<int, 12> d = {31, 28 + IsLeap(), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- return d[month - 1];
- }
- bool Correct() {
- return day > 0 && day <= DayByMonth() &&
- month > 0 && month < 13;
- }
- Date(int day_, int month_, int year_)
- : day(day_),
- month(month_),
- year(year_) {
- if (!Correct())
- throw std::invalid_argument("Idet medved po lesu, "
- "vidit - tank gorit, sel v nego "
- "i pogib kak geroi");
- }
- int GetDay() const {
- return day;
- }
- int GetMonth() const {
- return month;
- }
- int GetYear() const {
- return year;
- }
- void Increment() {
- day++;
- if (day > DayByMonth()) {
- day = 1;
- ++month;
- }
- if (month > 12) {
- year++;
- month = 1;
- }
- }
- void Decrement() {
- day--;
- if (day < 1) {
- --month;
- if (month < 1) {
- month = 12;
- --year;
- }
- day = DayByMonth();
- }
- }
- Date &operator++() {
- Increment();
- return *this;
- }
- Date &operator--() {
- Decrement();
- return *this;
- }
- };
- Date operator+(Date &cur, int ds) {
- for (int i = 0; i < ds; ++i)
- cur.Increment();
- return cur;
- }
- Date operator-(Date &cur, int ds) {
- for (int i = 0; i < ds; ++i)
- cur.Decrement();
- return cur;
- }
- bool operator<(const Date &one, const Date &two) {
- int year_one = one.GetYear(),
- month_one = one.GetMonth(),
- day_one = one.GetDay(),
- year_two = two.GetYear(),
- month_two = two.GetMonth(),
- day_two = two.GetDay();
- return std::tie(year_one, month_one, day_one)
- < std::tie(year_two, month_two, day_two);
- }
- bool operator==(const Date &one, const Date &two) {
- int year_one = one.GetYear(),
- month_one = one.GetMonth(),
- day_one = one.GetDay(),
- year_two = two.GetYear(),
- month_two = two.GetMonth(),
- day_two = two.GetDay();
- return std::tie(year_one, month_one, day_one)
- == std::tie(year_two, month_two, day_two);
- }
- bool operator!=(const Date &one, const Date &two) {
- int year_one = one.GetYear(),
- month_one = one.GetMonth(),
- day_one = one.GetDay(),
- year_two = two.GetYear(),
- month_two = two.GetMonth(),
- day_two = two.GetDay();
- return !(std::tie(year_one, month_one, day_one)
- == std::tie(year_two, month_two, day_two));
- }
- int operator-(const Date &one, const Date &two) {
- Date tmp = one;
- int counter = 0;
- if (tmp == two) {
- return 0;
- } else if (tmp < two) {
- for (; tmp != two; ++tmp, ++counter) {}
- } else {
- for (; tmp != two; --tmp, --counter) {}
- }
- return -counter;
- }
- std::ostream &operator<<(std::ostream &out, const Date &d) {
- out << d.GetDay() << '.' << d.GetMonth() << '.' << d.GetYear();
- return out;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement