// // Original author: Gordon Griesel // Date: Fall 2024 // Purpose: Calculate days between dates // // Days between dates is calculated by incrementing the earlier date // one day at a time until it reaches the later date, and counting // the increments. // // To be a leap year, the year number must be divisible by four – except for // end-of-century years, which must be divisible by 400. This means that the // year 2000 was a leap year, although 1900 was not. 2024, 2028, 2032 and // 2036 are all leap years. // // #include #include #include using namespace std; const char mname[][16] = {"Jan","Feb","Mar","Apr","May","Jun", "Jul","Aug","Sep","Oct","Nov","Dec"}; const int days[] = {31,28,31,30,31,30,31,31,30,31,30,31}; class Date { private: int month; int day; int year; public: Date() { //The default constructor is not used. } Date(const char *date) { //date format: "mm-dd-yyyy" //Use it like this: Date d1("11-05-2024"); month = (date[0]-'0')*10 + date[1]-'0'; day = (date[3]-'0')*10 + date[4]-'0'; year = (date[6]-'0')*1000 + (date[7]-'0')*100 + (date[8]-'0')*10 + (date[9]-'0'); } Date operator++() { //prefix (pre increment) day += 1; return *this; } Date operator++(int) { //postfix (post increment) Date tmp; day += 1; return tmp; } bool operator==(const Date &d) { return (day == d.day && month == d.month && year == d.year); } friend ostream &operator<<(ostream &, const Date &); }; ostream &operator<<(ostream &strm, const Date &obj) { strm << mname[obj.month-1] << " " << setw(2) << obj.day << ", " << obj.year; return strm; } //function prototypes int twoDates(const char *d1, const char *d2); int daysBetweenDates(Date d1, Date d2); //some test data char testdates[][16] = { "11-05-2024", "11-05-2024", //from today until today "11-05-2024", "11-06-2024", //from today until tomorrow "11-01-2024", "11-30-2024", //from nov-1 to nov-30 "11-01-2024", "12-01-2024", //from nov-1 to dec-1 "01-01-2024", "01-01-2025", //a whole year "01-01-2025", "01-01-2026", //another whole year "", "" }; // main function is here //======================================================== int main() { cout << "\nCalculate days between dates.\n" << endl; char d1[100], d2[100]; int noverflows = 0; int i = 0; while (strlen(testdates[i*2]) > 0) { strcpy(d1, testdates[i*2+0]); strcpy(d2, testdates[i*2+1]); cout << "Days between " << (Date)d1 << " and " << (Date)d2 << " is: "; //cout << flush; int count = twoDates(d1, d2); cout << count << endl; if (count >= 4000000) ++noverflows; ++i; } if (noverflows) cout << noverflows << " errors occured." << endl; cout << endl; return 0; } // Do not modify this function. int twoDates(const char *d1, const char *d2) { Date date1(d1); Date date2(d2); //cout << "Days between " << date1 << " and " << date2 << " is: " << flush; int count = daysBetweenDates(date1, date2); //cout << count << endl; return count; } // Do not modify this function. int daysBetweenDates(Date d1, Date d2) { // Start the count at 0. // Example: The days between Thursday and Friday // will be one day not two days. int count = 0; while (!(d1 == d2)) { ++d1; ++count; //safety exit if (count >= 40000000) break; } return count; }