#include <iostream> #include <assert.h>using namespace std;class date{ Public: date (int year = 1900, int month = 1, int day = &NBSP;1) { _year = year; _month = month ; _day = day; if (! Checkdate ()) { cout << "Enter date as illegal date" << Endl; assert (false); } } date (const date& d) { _year = d._year; _month = d._month; _day = d._day; } /*~date () { cout << "~date ()" << Endl; }*/public: bool checkdate () { return (_year >= 1900 && _month > 0 && _Month<13 && _day>0 && _day <= _getmonthday ( _year, _month)); } bool operator== (const date&d) { return ( this->_year == d._year && this->_month == d._month && this->_day == d._day); } bool operator!= (const Date &D) { return ! ( *THIS&NBSP;==&NBSP;D); } bool operator< (const date&d) { return (_year < d._year) | | (_year == d._year&&_month < d._month) | | (_year == d._year&&_month == d._month | | _day < d._day); } bool operator<= (const date&d) { return (*this == d) | | (*this < d); } bool operator>= (const date&d) { return (*this == d) | | (*this > d); } bool operator> (const date&d) { return ! (*this <= d); } //Date Calculator date operator+ (int day) { Date tmp (*this); if (day<0) { return (*this - ( -day)); //Direct Reduction } tmp._day += day; while (Tmp._day>_getmonthday (tmp._year, tmp._month)) { tmp._day -= _ Getmonthday (Tmp._year, tmp._month); if (tmp._month == 12) { tmp._year++; tmp._month = 1; } else { ++tmp._month; } } return tmp; } int operator-(const date &d) { int flag = 1; int days = 0; date max = *this; Date min = d; if (max < min) { Swap (max._year, min._year); swap (Max._month, min._month); swap (max . _day, min._day); flag = -1; } while (min != max) // Add a small date directly to a date that is equal to a large, counting the number of days to accumulate { ++min; ++ days; } return days*flag; } date& operator+= (Int day) { *this = *this + day; return *this; } date operator-(Int day) { if (day < 0) { return *this + (-day); } date tmp (*this); tmp._day -= day; while (tmp._day <= 0) { if (tmp._month == 1) //January Special { tmp._year--; tmp._month = 12; } else { --tmp._month; } tmp._day += _getmonthday (Tmp._year, tmp._month); } } date& operator-= (Int day) { *this = *this -day; return *this; } date & operator++ () { *this += 1; return *this; } date operator++ ( int) { date tmp (*this); *this += 1; return tmp; } Date & operator--() { *this -= 1; return *this; } Date operator--(int) { date tmp (*this); *this -= 1; return tmp; } void display () { cout << this->_year << "," << this->_month << "," << this-> _day << endl; } private: bool _isleapyear (int year) { if ((year % 4 == 0 && year % 100 != 0) | | (year % 400 == 0)) { return true; } else return false; } int _getmonthday (Int year, int month) //days per month { int montharray[13] = { 0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int day = monthArray[month]; if (Month == 2 && _isleapyear (year)) //leap year February 29 days { day += 1; } return day; } friend ostream & operator << (ostream &OUT,&NBSP;DATE&NBSP;&D); friend istream & operator>> (istream & IN,&NBSP;DATE&NBSP;&D); private: int _year; int _month; Int _day;};o stream & operator << (ostream &out, date &d) { out << d._year << "-" << d._month<< "-" << d._day; Return out;} Istream & operator>> (istream &in, date &d) { in >> d._year >> d._month >> d._day; return in;} Void test1 () { date d1 (2016, 12, 25); d1 += 5; d1.display (); D1 += 20; d1.display ();} Void test2 () { date d1 (2016, 12, 25); d1 -= 5; d1.display (); D1 -= 20; d1.display ();} Void test3 () { date d1 (2016, 12, 25); date d2 (2015, 1, 8); cout << d1 - d2 << endl; cout << d2 - d1 << endl; }void demo () { date d1, d2; int days; while (1) { cout << "1. Postponed x days;" << endl; cout << "2. Calculation date separated by days" << endl; cout << "0. Exit query" << endl; int in; cin >> in; if (in == 1) { cout << " Please enter a date for the year, month, and day " << endl; cin >> d1; while (!D1. Checkdate ()) { cout << "Illegal date, please re-enter" <<endl; cin >> d1; } cout << " Please enter the number of days to postpone " << endl; cin >> days; cout << d1 + days << endl; } else if (in ==&NBSP;2) { cout << "Please enter the year, month, and day of the first date" << endl; cin >> d1; while (!d1. Checkdate ()) { cout << "Illegal date, please re-enter" << endl; cin >> d1; } cout << "Please enter the year, month, and day of the second date" in turn << endl; cin >> d2; while (!D2. Checkdate ()) { cout << "Illegal date, please re-enter"; cin >> d2; } cout << "Days separated by:" << Endl; cout << d1 - d2<<endl; } else if (in == 0) { return; } else { cout << "Select Error, re-enter serial number" &NBSP;<<&NBSP;ENDL;&NBSP;&NBSP;}&NBSP;} }
This article is from the "printf Return Values" blog, so be sure to keep this source http://10741125.blog.51cto.com/10731125/1754511
C + + Class and Object date class operations (million calendar)