#include #include #include #include using namespace std; class Date { public: Date(int y, int m, int d) : year(y), month(m), day(d) { if ( month < 1 || month > 12 ) { throw std::domain_error{"Month " + to_string(m) + " doesn't exist!"}; } if ( day < 1 || day > days_in_month() ) { throw std::domain_error{"Day " + to_string(day) + " invalid"}; } } bool is_leap_year() const { if ( year % 400 == 0 ) return true; if ( year % 100 == 0 ) return false; return year % 4 == 0; } int days_in_month() const { static constexpr const array days {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (month < 1 || month > 12) return 0; if ( month == 2 && is_leap_year() ) return 29; return days.at(month); } void next_date() { ++day; if ( day > days_in_month() ) { day = 1; ++month; if (month > 12) { month = 1; ++year; } } } int year; int month; int day; }; ostream& operator<<(ostream & os, Date const& d) { return os << d.year << "-" << d.month << "-" << d.day; } const int DAYS_LATER{10000}; int main() { int y,m,d; char c; bool good_date{false}; cout << "Enter a date: "; while ( ! good_date ) try { cin >> y >> c >> m >> c >> d; Date date{y, m, d}; good_date = true; for ( int i{0}; i < DAYS_LATER; ++i ) { date.next_date(); } cout << DAYS_LATER << " days later: " << date; } catch (exception& e) { cerr << "Invalid date, enter another date: "; cin.clear(); cin.ignore(numeric_limits::max(), '\n'); } cout << endl; return 0; }