bool Plan::isOK() { // return if plan appears ok if ( numDays() > 0 && numCalendars() > 0 && index( calendar()) >= 0 ) return true; return false; }
Date::Date(int month, int day, int year) { this->year = year; this->month = month; this->day = day; if (numDays(this->month,this->year) == -1) { this->year = (this->year < 1) ? 1 : (this->year > 2014 ? 2014 : this-> year); this->month = (this->month < 1) ? 1 : (this->month > 12 ? 12: this->month); if (this->month != month) this->day = Date::monthNumDays[this->month-1]; } }
int main() { int day_abs = 0; int day_rel = 0; int week_abs = 0; int week_rel = 0; int mon_abs = 0; int mon_rel = 0; int yr_abs = 1900; bool done = false; int count = 0; while (!done) { // Check if Sunday and first of month if (yr_abs > 1900 && day_abs % 7 == 6 && day_rel == 0) { count++; printf("%d / %d / %d\n", day_rel + 1, mon_rel + 1, yr_abs); } // Update first day_abs++; day_rel++; if (day_rel == numDays(mon_rel, yr_abs)) { day_rel = 0; mon_rel++; mon_abs++; } if (mon_rel == 12) { yr_abs++; mon_rel = 0; } if (yr_abs == 2001) done = true; } printf("%d\n", count); return 0; }
void Date::readFromUser() { cout << "Enter month: "; cin >> month; cout << "Enter day: "; cin >> day; cout << "Enter year: "; cin >> year; int tempMonth = month; int tempYear = year; if (numDays(this->month,this->year) == -1) { this->year = (this->year < 1) ? 1 : (this->year > 2014 ? 2014 : this-> year); this->month = (this->month < 1) ? 1 : (this->month > 12 ? 12: this->month); if ((this->month != tempMonth) || (this->year != tempYear)) this->day = Date::monthNumDays[this->month-1]; } }
//sorry i had to use goto here, but i really couldn't think of a way to make the program break out of the catch block once it had gone in int main() { start: try { cout << "Please enter a date below, in the format mm/dd/yyyy or using any other seperator in place of '/':" << endl; string input; cin >> input; date val(input); //values for use in the operations date copy = val; date other; cout << "The date you have chosen is " << val << '.' << endl; //loop while (cin) { cout << endl << "Please enter the corresponding number of the function that you would like to perform:" << endl << "1. Is this year a leap year?" << endl << "2. What are the number of days in this year?" << endl << "3. What are the number of days till another date?" << endl << "4. What date is Thanksgiving on this year?" << endl << "5. If this date is the start of the semester, when will it end?" << endl << "6. What's tomorrow's date?" << endl << "7. Enter a new date." << endl << "8. Quit" << endl; int choice; cin >> choice; switch(choice) { case 1: if (isLeap(val.getYear())) cout << "Yes, " << val.getYear() << " is a leap year." << endl; else cout << "No, " << val.getYear() << " is not a leap year." << endl; break; case 2: cout << "The number of days in the year " << val.getYear() << " is " << numDays(val.getYear()) << '.' << endl; break; case 3: cout << "Please enter the date that you would like to calculate the days till: "; cin >> input; other = date(input); cout << "There is/are " << daysTill(val, other) << " day(s) till " << other << '.' << endl; break; case 4: cout << "Thanksgiving is on " << thanksgiving(date(1,11,val.getYear())) << " this year." << endl; break; case 5: cout << "The semester will end on " << endOfSem(val) << '.' << endl; break; case 6: ++copy; cout << "The date tomorrow is " << copy << endl; break; case 7: goto start; break; case 8: cout << "Goodbye!" << endl; goto end; break; } } } catch(badDate) { cout << "You have entered an invalid date. Please start again." << endl; goto start; } end: return 0; }