//------------------------------------------------------------------- // season.cpp // // This program prints the calendar for the first three months // of the year 1999. Its purpose is to show what mathematical // calculations are needed when writing your calendar program. // // compile-and-link using: $ g++ season.cpp -o season // // programmer: ALLAN CRUSE // written on: 05 MAR 2006 //------------------------------------------------------------------- #include // for printf(), perror() struct month_struct { char month_name[ 4 ]; int days_in_month; int first_weekday; int day_name[ 42 ]; }; struct month_struct month[ 12 ] = { {"JAN",31}, {"FEB",28}, {"MAR",31}, {"APR",30}, {"MAY",31}, {"JUN",30}, {"JUL",31}, {"AUG",31}, {"SEP",30}, {"OCT",31}, {"NOV",30}, {"DEC",31} }; int main( int argc, char **argv ) { int year = 1999; // specifies what year-number int start_day = 5; // year 1999 starts on Friday // adjustment of February-days for leap-years int century = year / 100, leapyear; if ( (century % 4) == 0 ) leapyear = 1; else if ( (year % 4) == 0 ) leapyear = 1; else leapyear = 0; month[ 1 ].days_in_month += leapyear; // compute the starting weekday for each month for (int mm = 0; mm < 12; mm++) { month[ mm ].first_weekday = start_day; start_day += month[ mm ].days_in_month; start_day %= 7; } // compute the nonzero day-numbers for each month for (int mm = 0; mm < 12; mm++) { int start_day = month[ mm ].first_weekday; for (int dd = 0; dd < month[ mm ].days_in_month; dd++) { int jj = start_day + dd; month[ mm ].day_name[ jj ] = 1 + dd; } } // show a calendar for the winter season of that year printf( "\n\n WINTER %d \n\n", year ); for (int mm = 0; mm < 3; mm++) { printf( "\n----------------------" ); printf( "\n %s ", month[mm].month_name ); printf( "\n----------------------" ); for (int i = 0; i < 42; i++) { if ( ( i % 7 ) == 0 ) printf( "\n" ); if ( month[ mm ].day_name[ i ] == 0 ) printf( " " ); else printf( " %2d", month[ mm ].day_name[ i ] ); } printf( "\n\n" ); } }