// // Program to test "date_convert" function (alternative version). // #include #include // "error_exit" function. // Post: "Bad input" printed; program exited. // (Yes, this is extremely crude.) void error_exit(void) { cout << "Bad input\n"; exit(EXIT_FAILURE); } // "print_date" function. // Pre: "d", "m", "y" represent a date (day, month, year). // Post: Prints the date, in month/day/year form. void print_date(int d, int m, int y) { cout << "date in month/day/year format: " << m << "/" << d << "/" << y << endl; return; } // "date_convert" function. // Pre: "day" and "year" represent a date, where "day" ranges from // 1 to 365 (366 for leap years). // Post: Prints the date, in month/day/year form. void date_convert(int day, int year) { // check year for acceptable value. if (year < 1) error_exit(); int days_in_feb; // calculate days days in February. if ( ((year % 400) == 0) || (((year % 4) == 0) && ((year % 100) != 0))) days_in_feb = 29; else days_in_feb = 28; // check day for acceptable value (too small -- check for too big // later). if (day < 1) error_exit(); int month = 1; // "initial guess" of January int day_in_month = day; // calculate month and day_in_month: // go through months, one at a time, until we find the one for // this day. // January? if (day_in_month <= 31) { print_date(day_in_month, month, year); return; } // no, guess February. month = 2; day_in_month = day_in_month - 31; // February? if (day_in_month <= days_in_feb) { print_date(day_in_month, month, year); return; } // no, guess March. month = 3; day_in_month = day_in_month - days_in_feb; // March? if (day_in_month <= 31) { print_date(day_in_month, month, year); return; } // no, guess April. month = 4; day_in_month = day_in_month - 31; // April? if (day_in_month <= 30) { print_date(day_in_month, month, year); return; } // no, guess May. month = 5; day_in_month = day_in_month - 30; // May? if (day_in_month <= 31) { print_date(day_in_month, month, year); return; } // no, guess June. month = 6; day_in_month = day_in_month - 31; // June? if (day_in_month <= 30) { print_date(day_in_month, month, year); return; } // no, guess July. month = 7; day_in_month = day_in_month - 30; // July? if (day_in_month <= 31) { print_date(day_in_month, month, year); return; } // no, guess August. month = 8; day_in_month = day_in_month - 31; // August? if (day_in_month <= 31) { print_date(day_in_month, month, year); return; } // no, guess September. month = 9; day_in_month = day_in_month - 31; // September? if (day_in_month <= 30) { print_date(day_in_month, month, year); return; } // no, guess October. month = 10; day_in_month = day_in_month - 30; // October? if (day_in_month <= 31) { print_date(day_in_month, month, year); return; } // no, guess November. month = 11; day_in_month = day_in_month - 31; // November? if (day_in_month <= 30) { print_date(day_in_month, month, year); return; } // no, guess December. month = 12; day_in_month = day_in_month - 30; // December? if (day_in_month <= 31) { print_date(day_in_month, month, year); return; } // none of the above -- error! error_exit(); } int main(void) { int d, y; cout << "Enter integers for day (1 to days in year), year:\n"; cin >> d >> y; date_convert(d, y); return EXIT_SUCCESS; }