/* File enumWeek.cpp shinnerl@ucla.edu Enumerated weekdays. Using an enumerated type for weekdays improves the style and enhances modularity, at some cost in increased coding. */ #include #include #include using namespace std; enum weekday { UNKNOWN = -1, SUN, MON, TUE, WED, THU, FRI, SAT, }; // Note: SUN == 0. string dayToString( weekday day ){ string answer; switch (day) { case SUN: answer = "SUN"; break; case MON: answer = "MON"; break; case TUE: answer = "TUE"; break; case WED: answer = "WED"; break; case THU: answer = "THU"; break; case FRI: answer = "FRI"; break; case SAT: answer = "SAT"; break; default: answer = "UNKNOWN"; } return answer; } weekday stringToDay( string s ){ weekday answer = UNKNOWN; if (s == "sun" || s == "SUN" ) answer = SUN; else if (s == "mon" || s == "MON" ) answer = MON; else if (s == "tue" || s == "TUE" ) answer = TUE; else if (s == "wed" || s == "WED" ) answer = WED; else if (s == "thu" || s == "THU" ) answer = THU; else if (s == "fri" || s == "FRI" ) answer = FRI; else if (s == "sat" || s == "SAT" ) answer = SAT; return answer; } void fail( const char* msg ){ cout << msg << endl; exit (EXIT_FAILURE); } void checkDate( int date, const char* functionName ){ if ( date < 1 || date > 31 ){ string s = string("\nError in function ") + string(functionName) + string(": invalid date. \n"); fail (s.c_str()); } } void checkDay( weekday day, const char* functionName ){ if ( day < SUN || day > SAT ){ string s = string("\nError in function ") + string(functionName) + string(": invalid weekday. \n"); fail (s.c_str()); } } weekday getDay(){ const int MAXTRY = 3; int count = 0; weekday answer; string s; do { cout << " Enter one of the following days: " << " sun mon tue wed thu fri sat : "; cin >> s; answer = stringToDay( s ); } while (answer == UNKNOWN && count++ < MAXTRY ); return answer; } int getDate(){ const int MAXTRY = 3; int count = 0; int answer; do { cout << " Enter an integer between 1 and 31: "; cin >> answer; } while ( (answer < 1 || answer > 31) && count++ < MAXTRY ); return answer; } int weekNumber( int dayNum, weekday firstDay ){ checkDate( dayNum, "weekNumber()"); // Enforce preconditions. checkDay( firstDay, "weekNumber()"); int idealDate = dayNum + firstDay; // Ideally, the 1st is a Sunday. return (idealDate - 1) / 7 + 1; } weekday dateToDay( int dayNum, weekday firstDay ){ checkDate( dayNum, "dateToDay()"); // Enforce preconditions. checkDay( firstDay, "dateToDay()"); int idealDate = dayNum + firstDay; // Ideally, the 1st is a Sunday. return weekday((idealDate - 1) % 7) ; // Note the explicit cast. } int main () { // ------------------------------------------------ // Input the first weekday of the month and a date: // ------------------------------------------------ cout << "\n Enter the first weekday of the month. \n"; weekday firstDay = getDay(); checkDay( firstDay, "getDay()"); cout << " Enter a month date. \n"; int dayNum = getDate(); checkDate( dayNum, "getDate()"); // ------------------------------------- // Output the day and week for the date: // ------------------------------------- cout << "\n The week number for the date is " << weekNumber( dayNum, firstDay ) << ". " << endl; cout << " The weekday for the date is " << dayToString( dateToDay(dayNum, firstDay) ) << ". \n" << endl; return EXIT_SUCCESS; } /* Sample I/O Script started on Thu Feb 28 12:16:45 2002 whale.1> weekday Enter the first weekday of the month. Enter one of the following days: sun mon tue wed thu fri sat : banana Enter one of the following days: sun mon tue wed thu fri sat : mon Enter a month date. Enter an integer between 1 and 31: 437 Enter an integer between 1 and 31: -17 Enter an integer between 1 and 31: 12 The week number for the date is 2. The weekday for the date is FRI. whale.2> exit exit whale.3> cal 4 2002 April 2002 S M Tu W Th F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 script done on Thu Feb 28 12:17:24 2002 */