260 likes | 382 Views
CSE 20232 Lecture 12 – Enumerations & Functions (A Calendar Example). Enumerations User defined functions Calendar examples Calculating the centroid of a block Next time Questions. Enumerations. An enumerated type Has named values
E N D
CSE 20232Lecture 12 – Enumerations & Functions (A Calendar Example) • Enumerations • User defined functions • Calendar examples • Calculating the centroid of a block • Next time • Questions
Enumerations • An enumerated type • Has named values • All values are mapped to (or replaced by) a sequence of integral values starting at 0 unless otherwise indicated • Has no external representation • The bool type is an example of a predefined enumeration with values {false,true}
Enumerations • Example declaration & use: enum color {red,green,blue,cyan,magenta,yellow}; color thisColor, thatColor; thisColor = blue; thatColor = thisColor; cout << thatColor; // outputs # value cin >> thisColor; // NOT ALLOWED
Enumerations • Enumeration may also be used to define a name for a constant value • Enum { Base = 10, Limit = 100};
Calendars • Let’s create a program … • to read a date in the 20th or 21st century and • print out the day of the week and • a message indicating whether or not the date is in a leap year
Simple Calendar int main ( ) { int year, month, day; char dummy; cout << "Enter a date (mm/dd/yyyy): "; cin >> month >> dummy >> day >> dummy >> year; // produce output like this ... // Monday, January 1st, 1900, is the 1st day of 1900 // (NOT a leap year) outputMessage(month,day,year); return 0; }
Simple Calendar void outputMessage (int month, int day, int year) { int yearDay; cout << endl; WriteDayName(DayOfWeek(month,day,year)); cout << ", "; WriteMonthName(month); cout << " " << day; WriteSuffix(day); cout << ", " << year << ", is the "; yearDay = DayOfYear(month,day,year); cout << yearDay; WriteSuffix(yearDay % 10); cout << " day of " << year; if (IsLeapYear(year)) cout << " (a LEAP year)"; else cout << " (NOT a leap year)"; cout << endl; }
Simple Calendar (HINTS) • Y is a leap year if Y is evenly divisible by 4, but not evenly divisible by 100, except those also evenly divisible by 400. • DayOfYear(m,d,y) is the sum of the total days in all months preceding m in y plus d. • DaysInMonth(m,y) is a constant except for m==2 (Feb) which depends on the year. • DaysInYear(y) is 365 or 366 depending on year(leap?) • DaysSince_1_1_1900(m,d,y) is the sum of all the days in all the years from 1900 till the beginning of this year (we assume y >= 1900) plus the DayOfYear(m,d,y) minus one (since we do not count 1/1/1900 itself). • DayOfWeek(m,d,y) is based on DaysSince_1_1_1900(m,d,y) % 7. This remainder is the number of the day of the week relative to 1/1/1900 which was a MONDAY. Remainder 0 means Monday, 1 means Tuesday, 2 means Wednesday, etc. • WriteSuffix(int num) writes the appropriate d character numerical suffix of num to cout. These are ... 1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 0th. • WriteMonthName(m) and WriteDayName(wkday) should be obvious.
Simple Calendar // Create some symbolic names for the days of the week enum DayName {Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday};
Simple Calendar // Y is a leap year if Y is evenly divisible by 4, but not // evenly divisible by 100, except those also evenly divisible // by 400. bool IsLeapYear (int y) { }
Simple Calendar // Y is a leap year if Y is evenly divisible by 4, but not // evenly divisible by 100, except those also evenly divisible // by 400. bool IsLeapYear (int y) { if (y % 400 == 0) return true; if (y % 100 == 0) return false; if (y % 4 == 0) return true; return false; }
Simple Calendar // DaysInMonth(m,y) is a constant except for m==2 (Feb) // which depends on the year. int DaysInMonth (int m, int y) { }
Simple Calendar // DaysInMonth(m,y) is a constant except for m==2 (Feb) // which depends on the year. int DaysInMonth (int m, int y) { if ((m == 9) || (m == 4) || (m == 6) || (m == 11)) return 30; else if (m == 2) if (IsLeapYear(y)) return 29; else return 28; else return 31; return -1; // this is an error indicating m < 1 or m > 12 }
Simple Calendar // DaysInYear(y) is 365 or 366 depending on year(leap?) int DaysInYear (int y) { }
Simple Calendar // DaysInYear(y) is 365 or 366 depending on year(leap?) int DaysInYear (int y) { if (IsLeapYear(y)) return 366; return 365; }
Simple Calendar // DayOfYear(m,d,y) is the sum of the total days in all months // preceding m in y plus d. int DayOfYear (int m, int d, int y) { }
Simple Calendar // DayOfYear(m,d,y) is the sum of the total days in all months // preceding m in y plus d. int DayOfYear (int m, int d, int y) { int days = 0; for (int month = 1; month < m; month++) days = days + DaysInMonth(month,y); days = days + d; return days; }
Simple Calendar // DaysSince_1_1_1900(m,d,y) is the sum of all the days in all // the years from 1900 till the beginning of this year (we // assume y >= 1900) plus the DayOfYear(m,d,y) minus one // (since we do not count 1/1/1900 itself). long int DaysSince_1_1_1900 (int m, int d, int y) { }
Simple Calendar // DaysSince_1_1_1900(m,d,y) is the sum of all the days in all // the years from 1900 till the beginning of this year (we // assume y >= 1900) plus the DayOfYear(m,d,y) minus one // (since we do not count 1/1/1900 itself). long int DaysSince_1_1_1900 (int m, int d, int y) { long int days = 0; for (int year = 1900; year < y; year++) days = days + DaysInYear(year); days = days + DayOfYear(m,d,y); days = days – 1; // do not count first day of century return days; }
Simple Calendar // DayOfWeek(m,d,y) is based on DaysSince_1_1_1900(m,d,y) % 7. // This remainder is the number of the day of the week relative // to 1/1/1900 which was a MONDAY. Remainder 0 means Monday, 1 // means Tuesday, 2 means Wednesday, etc. DayName DayOfWeek (int m, int d, int y) { }
Simple Calendar // DayOfWeek(m,d,y) is based on DaysSince_1_1_1900(m,d,y) % 7. // This remainder is the number of the day of the week relative // to 1/1/1900 which was a MONDAY. Remainder 0 means Monday, 1 // means Tuesday, 2 means Wednesday, etc. DayName DayOfWeek (int m, int d, int y) { switch (DaysSince_1_1_1900(m,d,y) % 7) { case 0 : return Monday; case 1 : return Tuesday; case 2 : return Wednesday; case 3 : return Thursday; case 4 : return Friday; case 5 : return Saturday; case 6 : return Sunday; default: return Monday; } }
Simple Calendar // WriteSuffix(int num) writes the appropriate d character // numerical suffix of num to cout. These are ... 1st, 2nd, 3rd, // 4th, 5th, 6th, 7th, 8th, 9th, 0th. void WriteSuffix (int num) { }
Simple Calendar // WriteSuffix(int num) writes the appropriate d character // numerical suffix of num to cout. These are ... 1st, 2nd, 3rd, // 4th, 5th, 6th, 7th, 8th, 9th, 0th. void WriteSuffix (int num) { switch (num % 10) // last digit { case 1 : cout << “st”; break; case 2 : cout << “nd”; break; case 3 : cout << “rd”; break; default: cout << “th”; } }
Simple Calendar // WriteMonthName(m) and WriteDayName(wkday) should be obvious. void WriteDayName (DayName dnom) { } void WriteMonthName(int m) { }
Simple Calendar // WriteMonthName(m) and WriteDayName(wkday) should be obvious. void WriteDayName (DayName dnom) { switch (dnom) { Monday : cout << “Monday”; break; Tuesday : cout << “Tuesday”; break; Wednesday : cout << “Wednesday”; break; Thursday : cout << “Thursday”; break; Friday : cout << “Friday”; break; Saturday : cout << “Saturday”; break; Sunday : cout << “Sunday”; } }
Simple Calendar // WriteMonthName(m) and WriteDayName(wkday) should be obvious. void WriteMonthName(int m) { if (m == 1) cout << “January”; else if (m == 2) cout << “February”; else if (m == 3) cout << “March”; else if (m == 4) cout << “April”; else if (m == 5) cout << “May”; else if (m == 6) cout << “June”; else if (m == 7) cout << “July”; else if (m == 8) cout << “August”; else if (m == 9) cout << “September”; else if (m == 10) cout << “October”; else if (m == 11) cout << “November”; else if (m == 12) cout << “December”; }