Server IP : 172.16.15.8 / Your IP : 18.191.103.144 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/cathomas/ |
[ Home ] | [ C0mmand ] | [ Upload File ] |
---|
//------------------------------------------------------------- // Example to illustrate user-defined functions //------------------------------------------------------------- #include <iostream> #include <iomanip> using namespace std; //------------------------------------------------------------- // LeapYear calculates for leap years. //------------------------------------------------------------- // Returns: true if it is a leap year and false if it is not. //------------------------------------------------------------- bool LeapYear (int year) { // calculating if there is a leap year or not if (year % 4 != 0) return false; else if (year % 100 == 0 && year % 400 != 0) return false; else return true; } //------------------------------------------------------------- // DaysInMonth determines the number of days of the month // Note: ignores leap year //------------------------------------------------------------- // 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) // modify this case so that february has numdays = 29; // the right amount of days with or else // without a leapyear 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; } } //--------------------------------------------------- // Display first line displays the first line of the // week in the calendar by finding the startdayofweek //--------------------------------------------------- void DisplayFirstLine (int startdayofweek) { int i; cout << endl; for (i=0; i < startdayofweek; i++) cout << " "; for (i=1; i<=(7-startdayofweek); i++) cout << setw(6) << i; } //-------------------------------------------------- // Display full week calculates what the next couple // of weeks should start on by looking at the last // number displayed //-------------------------------------------------- void DisplayFullWeek (int startdateofweek) { int i; cout << "\n"; for (i=startdateofweek; i<= (startdateofweek+6) ; i++) cout << setw(6) << i; } //------------------------------------------------- // Display last week has to calculate what the week // should start on and also calculate where it // should stop based on how many days are in the // month. //------------------------------------------------- void DisplayLastWeek (int startdateofweek, int numdaysinmonth) { int i; cout << "\n"; for (i=startdateofweek; i<= numdaysinmonth; i++) cout << setw(6) << i; } //------------------------------------------------ // Calculate day of week calculates the day of the // week of the date passed in via the parameters // month, day, and year. //------------------------------------------------ // Accepts: calls the leap year function // Returns: an integer that indicates the day of // the week. //----------------------------------------------- int CalculateDayofWeek (int month, int day, int year) { int code, lasttwo, // last two digits total, // add day, monthcode, and lasttwodigits of the year quarterofyear, // last two digits of the year + 1/4 of the last two digits centuries; // assignment statement for centuries // switch statment to declare the month code for each month. switch (month) { case 1: code = 6; break; case 2: code = 2; break; case 3: code = 2; break; case 4: code = 5; break; case 5: code = 0; break; case 6: code = 3; break; case 7: code = 5; break; case 8: code = 1; break; case 9: code = 4; break; case 10: code = 6; break; case 11: code = 2; break; case 12: code = 4; break; } lasttwo = year % 100; // calculate the last 2 digits of the year quarterofyear = (lasttwo/4) + lasttwo; // adding a quarter of the year onto itself total = quarterofyear + code + day; // adding the total of all three numbers together // calculating for centuries centuries = year / 100; if (centuries == 17 || centuries == 21) total = total + 5; else if (centuries == 18 || centuries == 22) total = total + 3; else if (centuries == 19) total = total + 1; // calculating the leap year for january and february to add 1 to the total if (LeapYear (year) == true) if (month == 1 || month == 2) total = total - 1; // add a mod so that the number correspnds to the day of the week total = total % 7; return total; } //-------------------------------------------------- // Display title and headings displays the month // name and the days of the week. //-------------------------------------------------- void DisplayTitleAndHeadings (int month) { cout << " "; DisplayMonthName (month); cout << "\n" << setw(6) <<" Sun" << setw(6) << "Mon" << setw(6) << "Tue" << setw(6) << "Wed" << setw(6) << "Thu" << setw(6) << "Fri"; cout << setw(6) << "Sat"; } //------------------------------------------------------------- // Main Function //------------------------------------------------------------- int main ( ) { // --- Variables int monthnum, // month number (1 - 12) entered by user begdayofmonth, // begining day of the month. begdateofnextweek,// begining date of the next week year, // the year the user entered numdays; // number of days in month // --- Get month number from user cout << "\nEnter a month number (1-12): "; cin >> monthnum; cout << "\n Enter the Year "; cin >> year; begdayofmonth = CalculateDayofWeek (monthnum, 1, year); // --- Validate month number and 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; } // add a while to validate the year while (year < 1700 || year > 2299) { cout << "\n\n!!! Year must be in range 1700 - 2299 !!!"; cout << "\n\n Please re-enter year: "; cin >> year; } // --- Get number of days in month LeapYear(year); numdays = DaysInMonth(monthnum, year); // --- Display output sentence DisplayTitleAndHeadings(monthnum); cout << "\n"; 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); cout << "\n"; return 0; }