#include #include using namespace std; /* want to be able to say, for example: date today(2023, 2, 2); today.print(); today.increment(); today.print(); today.set(1987, 12, 31); today.increment(); today.print(); */ class date { protected: // only methods of this class can access these variables int day, month, year, dow; public: date(int y, int m, int d); void set(int y, int m, int dy); void print(); void increment(); }; string mname[] = { "", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "Decemeber" }; string dname[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; string stndrdth(int n) { if (n == 1 || n == 21 || n == 31) return "st"; if (n == 2 || n == 22) return "nd"; if (n == 3 || n == 23) return "rd"; return "th"; } bool is_leap_year(int y) { if (y % 400 == 0) return true; if (y % 100 == 0) return false; if (y % 4 == 0) return true; return false; } int days_in(int y, int m) { if (m == 4 || m == 6 || m == 9 || m == 11) return 30; if (m != 2) return 31; if (is_leap_year(y)) return 29; return 28; } int day_number(int y, int m, int d) { int t = y * 365; t += (y + 3) / 4; t -= (y + 99) / 100; t += (y + 399) / 400; for (int i = 1; i < m; i += 1) t += days_in(y, i); t += d; return t; } date::date(int y, int m, int d) { set(y, m, d); } void date::set(int y, int m, int dy) { year = y; month = m; day = dy; int d = day_number(year, month, day); dow = (d + 5) % 7; } void date::print() { cout << dname[dow] << " " << day << stndrdth(day) << " " << mname[month] << " " << year << "\n"; } void date::increment() { dow = (dow + 1) % 7; day += 1; if (day > days_in(year, month)) { day = 1; month += 1; if (month == 13) { month = 1; year += 1; } } } int main() { cout << "2nd February 2023 is day number " << day_number(2023, 2, 2) << " A.D.\n"; date today(2023, 2, 2); today.print(); today.increment(); today.print(); today.set(1987, 12, 31); today.increment(); today.print(); }