Server IP : 172.16.15.8 / Your IP : 3.137.164.229 Web Server : Apache System : Linux zeus.vwu.edu 4.18.0-553.27.1.el8_10.x86_64 #1 SMP Wed Nov 6 14:29:02 UTC 2024 x86_64 User : apache ( 48) PHP Version : 7.2.24 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON Directory (0705) : /home/cchansen/ |
[ Home ] | [ C0mmand ] | [ Upload File ] |
---|
//------------------------------------------------------------- // Program #5: Calculating Calendars // // Written by: Caitlin Hansen // November 2008 // // Purpose: to calculate calendars produced by the user's input // that includes leap year option, starting days of the week, // and an entire month of an user designated year. //------------------------------------------------------------- #include <iostream> #include <iomanip> #include <cmath> using namespace std; // ------------------------------------------------------------ // LeapYear determines whether a year is or is not a leap year // ------------------------------------------------------------ // Accepts: number representing a year // Returns: a true or false calculation // ------------------------------------------------------------ bool LeapYear ( int year ) { int rem; rem = year % 4; if ( rem == 0 ) return true; else return false; } //------------------------------------------------------------- // DaysInMonth determines the number of days of the month //------------------------------------------------------------- // Accepts: number ( 1 - 12 ) representing month // Returns: number of days in that month //------------------------------------------------------------- int DaysInMonth ( int monthnum, int year ) { int numdays; // variable for the days in the month switch ( monthnum ) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: numdays = 31; break; case 2: if ( LeapYear(year) == true ) numdays = 29; else numdays = 28; break; case 4: case 6: case 9: case 11: numdays = 30; break; } return numdays; } int CalculateDayofWeek ( int month, int day, int year ) { int dayofweek, step1, // calculates the last two digits of the year step2, // calculates a quarter of the digits step3, // calculates the total of the last 2 digits and the quarter code, // the code for the months last; // the total of the code, its corresponding int, and the sum of step3 switch( month ) { case 5: code = 0; break; case 8: code = 1; break; case 2: case 3: case 11: code = 2; break; case 6: code = 3; break; case 9: case 12: code = 4; break; case 4: case 7: code = 5; break; case 1: case 10: code = 6; break; } step1 = year % 100; step2 = step1 / 4; step3 = step1 + step2; if ( year >= 1700 ) last = code + step3 + 5; else if ( year >= 1800 ) last = code + step3 + 3; else if ( year >= 1900 ) last = code + step3 + 1; else if ( year >= 2100 ) last = code + step3 + 5; else if ( year >= 2200 ) last = code + step3 + 3; dayofweek = ( last + 3 ) % 7; if ( LeapYear(year) == true && month == 1 ) last -= 1; else if ( LeapYear(year) == true && month == 2 ) last -= 1; return dayofweek; } //------------------------------------------------------------- // DisplayMonthName displays the month name to standard output //------------------------------------------------------------- // Accepts: number ( 1 - 12 ) representing month // Returns: nothing //------------------------------------------------------------- void DisplayMonthName ( int monthnum ) { switch ( monthnum ) { case 1: cout << "January"; break; case 2: cout << "February"; break; case 3: cout << "March"; break; case 4: cout << "April"; break; case 5: cout << "May"; break; case 6: cout << "June"; break; case 7: cout << "July"; break; case 8: cout << "August"; break; case 9: cout << "September"; break; case 10: cout << "October"; break; case 11: cout << "November"; break; case 12: cout << "December"; break; } } //------------------------------------------------------------- // DisplayFirstLine displays the first week in the output //------------------------------------------------------------- // Accepts: number representing days in first week of output // Returns: nothing //------------------------------------------------------------- void DisplayFirstLine ( int startdayofweek ) { int i; cout << "\n"; for( i = 0; i < startdayofweek; i ++ ) cout << " "; for( i = 1; i <= ( 7 - startdayofweek ); i ++ ) cout << setw(5) << i; } //------------------------------------------------------------- // DisplayFullWeek displays the full week of output //------------------------------------------------------------- // Accepts: number ( resulting start date of week ) // Returns: nothing //------------------------------------------------------------- void DisplayFullWeek ( int startdateofweek ) { int i; cout << "\n"; for ( i = startdateofweek; i <= ( startdateofweek + 6 ); i ++ ) cout << setw(5) << i; } //------------------------------------------------------------- // DisplayLastWeek displays the last week of output //------------------------------------------------------------- // Accepts: numbers ( start date of week and number of days in the month ) // Returns: nothing //------------------------------------------------------------- void DisplayLastWeek ( int startdateofweek, int numdaysinmonth ) { int i; cout << "\n"; for (i = startdateofweek; i <= numdaysinmonth; i ++) cout << setw(5) << i; } //------------------------------------------------------------- // DisplayLastWeek displays the last week of output //------------------------------------------------------------- // Accepts: numbers ( start date of week and number of days in the month ) // Returns: nothing //------------------------------------------------------------- void leapYear( int year ) { int startYear, endYear, leapCount = 4; startYear = year; startYear /= 100; switch( startYear ) { case 17: leapCount = 5; break; case 18: leapCount = 3; break; case 19: leapCount = 1; break; case 21: leapCount = 5; break; case 22: leapCount = 3; break; } } void DisplayTitleAndHeadings ( int month ) { cout << endl; cout << "\t\t"; DisplayMonthName ( month ); cout << endl; cout << " ---------------------------------"; cout << endl; cout << " Sun Mon Tue Wed Thur Fri Sat"; cout << endl; cout << " ---------------------------------"; } //------------------------------------------------------------- // Main Function //------------------------------------------------------------- int main ( ) { // --- Variables int monthnum, // month number ( 1 - 12 ) entered by user startdateofweek, // start date of the week begdateofnextweek, // beginning date of the next week year, // year number ( 1700 - 2299 ) entered by user begdayofmonth, // beginning day of the month numdays; // number of days in month // --- Validating year amd month numbers cout << "\nPlease, Enter the desired year ( 1700-2299 ): "; cin >> year; while ( year < 1700 || year > 2299 ) { cout << "\n\n!!! Year number must be within the range of 1700 to 2299!!!"; cout << "\n\nPlease, re-enter year number: "; cin >> year; } cout << "\nPlease, Enter the desired month ( 1 - 12 ): "; cin >> monthnum; while ( monthnum < 1 || monthnum > 12 ) { cout << "\n\n!!! Month number must be in range 1 - 12 !!!"; cout << "\n\nPlease re-enter month number: "; cin >> monthnum; } // --- Get number of days in month numdays = DaysInMonth( monthnum, year ); begdayofmonth = CalculateDayofWeek ( monthnum, 1, year ); leapYear( year ); // --- Displaying output sentence DisplayTitleAndHeadings ( monthnum ); DisplayFirstLine( begdayofmonth ); begdateofnextweek = 8 - begdayofmonth; DisplayFullWeek( begdateofnextweek ); begdateofnextweek += 7; DisplayFullWeek( begdateofnextweek ); begdateofnextweek += 7; DisplayFullWeek( begdateofnextweek ); begdateofnextweek += 7; if( numdays - begdateofnextweek >= 7 ) { DisplayFullWeek( begdateofnextweek ); begdateofnextweek += 7; } DisplayLastWeek( begdateofnextweek, numdays ); begdateofnextweek += 7; cout << endl; cout << endl; return 0; }