//Excerpts from date.C with three int data members, year, month, day. //Move count days into the future. If count is negative, move into the past. void date::next(int count) { div_t divide = div(count, 365); if (divide.rem < 0) { divide.rem += 365; --divide.quot; } year += divide.quot; day += divide.rem; while (day > date_length[month]) { day -= date_length[month]; if (++month > 12) { month = 1; ++year; } } } //Return the Julian date. int date::julian() const { int j = day; for (int m = 1; m < month; ++m) { j += date_length[m]; } return j; } //Header file for class date with one data member #ifndef DATEH #define DATEH class date { int day; //number of days before or after January 1, 0 AD public: date(int initial_month, int initial_day, int initial_year); void next(int count = 1) {day += count;} int julian() const; void print() const; }; #endif //date.C file for class date with one data member #include #include //for div, div_t, exit, EXIT_FAILURE #include "date.h" using namespace std; date::date(int initial_month, int initial_day, int initial_year) { if (initial_month < 1 || initial_month > 12) { cerr << "bad month " << initial_month << "/" << initial_day << "/" << initial_year << "\n"; exit(EXIT_FAILURE); } if (initial_day < 1 || initial_day > date_length[initial_month]) { cerr << "bad day " << initial_month << "/" << initial_day << "/" << initial_year << "\n"; exit(EXIT_FAILURE); } day = 365 * initial_year + initial_day - 1; for (int m = 1; m < initial_month; ++m) { day += date_length[m]; } } int date::julian() const { int j = day % 365; if (j < 0) { j += 365; } return j + 1; } void date::print() const { div_t divide = div(day, 365); if (divide.rem < 0) { divide.rem += 365; --divide.quot; } int d = divide.rem + 1; int m; //uninitialized variable for (m = 1; d > date_length[m]; ++m) { d -= date_length[m]; } cout << m << "/" << d << "/" << divide.quot; }