//------------------------------------------------------------------- // xmas.cpp // // This program shows which day-of-the-week is Christmas for a // specified year which is entered as a command-line argument. // // programmer: ALLAN CRUSE // date begun: 28 SEP 2003 //------------------------------------------------------------------- #include // for printf(), perror() #include // for atoi(), exit() #include // for strncpy() char daylst[] = "TueWedThuFriSatSunMon"; int main( int argc, char **argv ) { // get the command-line argument int param = ( argc > 1 ) ? atoi( argv[1] ) : 2003; // check for invalid year if ( param < 1900 ) { printf( "year cannot be before 1900\n" ); exit(1); } // compute years since 1900 int years = param - 1900; // compute leap-years since 1900 int leaps = years / 4; // compute days since Christmas 1900 int days = 365*years + leaps; // compute the day-of-the-week int theday = days % 7; // report the day on which Christmas falls char report[] = "xxx, Dec 25\n"; strncpy( report, &daylst[ 3*theday ], 3 ); printf( report ); }