//This file is leapdate.C #include #include #include "leapdate.h" using namespace std; int leapdate::length() const { static const int len[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; const int y = get_year(); bool is_leap; //true if this is a leap year if (y % 400 == 0) { //1600 and 2000 are leap years is_leap = true; } else if (y % 100 == 0) { //1700, 1800, and 1900 are not leap years is_leap = false; } else if (y % 4 == 0) { is_leap == true; } else { is_leap = false; } const int m = get_month(); return is_leap && m == february ? 29 : len[m]; } void leapdate::install(int m, int d, int y) { year = y; if (m < january || m > december) { cerr << "bad month " << m << "\n"; exit(EXIT_FAILURE); } month = m; if (d < 1 || d > length()) { cerr << "bad day " << d << "\n"; exit(EXIT_FAILURE); } day = d; } leapdate& leapdate::operator++() //prefix { if (++day > length()) { day = 1; if (++month > december) { month = january; ++year; } } return *this; } leapdate& leapdate::operator--() //prefix { if (--day < 1) { if (--month < january) { month = december; --year; } day = length(); } return *this; }