Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdexcept>
- #include <unordered_map>
- #include <tuple>
- const std::unordered_map<int, int> month_day = {{1, 31},
- {2, 28},
- {3, 31},
- {4, 30},
- {5, 31},
- {6, 30},
- {7, 31},
- {8, 31},
- {9, 30},
- {10, 31},
- {11, 30},
- {12, 31}};
- class Date {
- private:
- int day_;
- int month_;
- int year_;
- public:
- Date(int day, int month, int year) {
- if ((year % 400 == 0 && month == 2) || (year % 100 != 0 && year % 4 == 0 && month == 2)) {
- if (day < 1 || day > 29) {
- throw std::invalid_argument("invalid data");
- } else {
- day_ = day;
- month_ = month;
- year_ = year;
- }
- } else {
- if (month > 12 || day < 1 || day > month_day.at(month)) {
- throw std::invalid_argument("invalid data");
- } else {
- day_ = day;
- month_ = month;
- year_ = year;
- }
- }
- }
- [[nodiscard]] int GetDay() const {
- return day_;
- }
- [[nodiscard]] int GetMonth() const {
- return month_;
- }
- [[nodiscard]] int GetYear() const {
- return year_;
- }
- Date &operator++() {
- if ((year_ % 400 == 0 && month_ == 2) ||
- (year_ % 100 != 0 && year_ % 4 == 0 && month_ == 2)) {
- if (day_ == 28) {
- day_ = 29;
- return *this;
- } else if (day_ == 29) {
- day_ = 1;
- month_ = 3;
- return *this;
- } else {
- ++day_;
- return *this;
- }
- } else if (day_ == month_day.at(month_)) {
- if (month_ == 12) {
- day_ = 1;
- month_ = 1;
- ++year_;
- return *this;
- } else {
- day_ = 1;
- ++month_;
- return *this;
- }
- } else {
- ++day_;
- return *this;
- }
- }
- Date operator--() {
- if ((year_ % 400 == 0 && month_ == 3) ||
- (year_ % 100 != 0 && year_ % 4 == 0 && month_ == 3)) {
- if (day_ == 1) {
- day_ = 29;
- month_ = 2;
- return *this;
- } else {
- --day_;
- return *this;
- }
- } else {
- if (day_ == 1) {
- if (month_ == 1) {
- day_ = 31;
- month_ = 12;
- --year_;
- return *this;
- } else {
- // std::cout << day_ << " " << month_ <<" "<< year_ << '\n';
- day_ = month_day.at(month_ - 1);
- --month_;
- return *this;
- }
- } else {
- --day_;
- return *this;
- }
- }
- }
- };
- Date operator+(const Date &first, int number) {
- Date new_date = first;
- int i = 0;
- while (i != number) {
- ++new_date;
- ++i;
- }
- return new_date;
- }
- Date operator-(const Date &first, int number) {
- Date new_date = first;
- int i = 0;
- while (i != number) {
- --new_date;
- ++i;
- }
- return new_date;
- }
- bool operator<(const Date &first, const Date &second) {
- auto y_first = first.GetYear();
- auto m_first = first.GetMonth();
- auto d_first = first.GetDay();
- auto y_second = second.GetYear();
- auto m_second = second.GetMonth();
- auto d_second = second.GetDay();
- return std::tie(y_first, m_first, d_first) <
- std::tie(y_second, m_second, d_second);
- }
- int operator-(const Date &second, const Date &first) {
- Date new_first = std::min(first, second);
- Date new_second = std::max(first, second);
- int count = 0;
- while ((new_first < new_second)) {
- --new_second;
- ++count;
- }
- return count;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement