//Jim Stroud //CS 201 //Section 6 //Extra Credit: Problem 3, page 383 //____________________________________________________________ //Program calendar.cpp prints a calendar for one year, given //the year and the day of the week that January 1 falls on. #include #include //Prints the calendar for a month. void PrintMonthlyCalendar( int, int& ); //Gets the days in the month and prints the months name. void GetMonthData( int, int, int& ); int main() { int year; //the year for the calendar int firstDay; //first day of the year and each month int monthCount; //counts the months of the year int daysInMonth; //the number of days in a given month //Prompt for input cout << endl << endl << endl << endl << endl; cout << "This program prints a calendar for one year, " << endl << "given the year and the day of the week that " << endl << "January 1 falls on. " << endl << endl; cout << "Enter year( a four digit number): "; cin >> year; cout << "Enter day( a digit, 0-6, for Sunday thru Saturday): "; cin >> firstDay; //Print year heading cout << endl << endl << endl << endl << setw( 12 ) << year; //Print monthly calendar for( monthCount = 1; monthCount <= 12; monthCount++ ) { GetMonthData( year, monthCount, daysInMonth ); PrintMonthlyCalendar( daysInMonth, firstDay ); } }//end main(). //*********************************************************** //Gets the days in the month and prints the months name. // void GetMonthData( /*IN*/ int year, //the calendar year /*IN*/ int month, //the calendar month /*OUT*/ int& numDays )//# days in month // //Pre: year, month have been assigned //Post: year == year@entry // month == month@entry // numDays == the number of days in the month@entry { cout << endl << endl << endl; switch( month ) { case 1: numDays = 31; cout << setw ( 14 ) << "January"; break; case 2: if( year % 4 ) numDays = 28; else numDays = 29; //leap year cout << setw ( 14 ) << "February"; break; case 3: numDays = 31; cout << setw ( 13 ) << "March"; break; case 4: numDays = 30; cout << setw ( 13 ) << "April"; break; case 5: numDays = 31; cout << setw ( 12 ) << "May"; break; case 6: numDays = 30; cout << setw ( 12 ) << "June"; break; case 7: numDays = 31; cout << setw ( 12 ) << "July"; break; case 8: numDays = 31; cout << setw ( 13 ) << "August"; break; case 9: numDays = 30; cout << setw ( 15 ) << "September"; break; case 10: numDays = 31; cout << setw ( 14 ) << "October"; break; case 11: numDays = 30; cout << setw ( 14 ) << "November"; break; case 12: numDays = 31; cout << setw ( 14 ) << "December"; break; }//endswitch. cout << endl << endl; }//end GetMonthData(). //*********************************************************** //Prints the calendar for a month // void PrintMonthlyCalendar( /*IN*/ int numDays, //# days in month /*IN/OUT*/int& dayOne )//day of the week //for 1st day //of the month // //Pre: numDays has been assigned the number of days in the month // && dayOne has been assigned the number for the day of the // week for the first day in the month //Post: numDays == numDays@entry // && dayOne == the number for the day of the week for the // first day of the next month { int dayCount; //Counts the num days printed int widthParam; //Parameter to the setw() function //Heading for days of the week cout << setw( 3 ) << 'S' << setw( 3 ) << 'M' << setw( 3 ) << 'T' << setw( 3 ) << 'W' << setw( 3 ) << 'T' << setw( 3 ) << 'F' << setw( 3 ) << 'S' << endl << " ____________________" << endl; for( dayCount = 1; dayCount <= numDays; dayCount++ ) { //The first day of the month must go under proper heading. if( dayCount == 1 ) widthParam = 3 * dayOne + 3; else widthParam = 3; cout << setw( widthParam ) << dayCount; //If the last day of the week has been reached... if( dayOne == 6 ) { dayOne = 0; //..reset as first weekday.. if( dayCount < numDays ) //...go to a newline for a cout << endl; //new week } else dayOne++; } }//end printMonthlyCalendar().