Date Calculator
#include <iostream>using namespace std; #include <string>class Date{public:Date ( INT&NBSP;YEAR&NBSP;=&NBSP;2015,&NBSP;INT&NBSP;MONTH&NBSP;=&NBSP;11,&NBSP;INT&NBSP;DAY&NBSP;=&NBSP;15)//constructor: _ Year, _month (month) //initialization list, _day (day) {}date (const date& d)//copy constructor: _ Year (d._year), _month (D._month), _day (d._day) {}date& operator= (const date& d) {if (this!= &D) {_year=d._year;_month=d._month;_day=d._day;} Return *this;} Void display1 () {cout<< "Date:" <<_year<< "-" <<_month<< "-" <<_day<< Endl;} public:bool operator== (const date& d) {return _year == d._year&& _month == d._month&& _day == d._day;} Bool operator!= (const date& d) {return ! ( *this==d);} bool operator > (const date& d) {if (_year>d._year) {return true;} Else if (_year==d._year) {if (_month>d._month) {return true;} Else if (_month==d._month) {if (_day>d._day) {return true;}}} Return false;} Bool operator>= (const date& d) {return *this > d | | *this == d;} bool operator< (const date& d) {return ! ( *THIS&NBSP;>=&NBSP;D);} Bool operator<= (const date& d) {return ! ( *THIS&NBSP;>&NBSP;D);} BOOL&NBSP;JUDGE_YMD (int year=2015,int month =11,int day=15)//Judging validity {if (_year<1| | _month<0| | _month>12| | _day<1| | _day>judge_day (_year,_month)) return false;return true;} bool isleapyear (int year)//Leap year {if (year % 4 == 0 && year % 100 != 0) | | year % 400 == 0) Return true;return false;} Int yearmonth (DATE&&NBSP;D)//number of months ++ {++d._month;if (d._month==13) {d._month=1;++d._year;} Else{d._month;} REturn d._month;} Int judge_day (Int year,int month)//number of days per month {int monthday[12]={ 31,28,31,30,31,30,31,31,30,31,30,31};if (Isleapyear (year)) {return monthday[month-1]+1;} RETURN&NBSP;MONTHDAY[MONTH-1];} Public:void correctdate ()//convert date {while (_day<=0) {if (_month==1) {_year--;_month=12;} else{_month--;} _day+=judge_day (_year,_month);} while (_day>=judge_day (_year,_month)) {if (_month==12) {_year++;_month=1;} else{_month++;} _day-=judge_day (_year,_month);}} Calculation: Date before and after n days date operator+ (int day) {cout<<day<< "Days:" <<endl;date d (*this);d . _day+=day;d.correctdate (); return d;} date operator-(int day) {cout<<day<< "days ago:" <<endl;date d (*this);d. _day-=day;d. Correctdate (); return d;} date& operator+= (int day) {this->_day+=day;this->correctdate (); return *this;} date& operator-= (int day) {this->_day-=day;this->correctdate (); return *this;} date& operator++ ()//front + +{*this+=1;return *this;} date operator++ (int)//Post ++{date tmp (*this); *this+=1;return tmp;} Calculation: Days between two dates int operator-(date& d) {int days=0;date d1=*this;date d2=d;cout< < "Days of two days:" <<endl;if (D1<D2)//d2 smaller {d1=d;d2=*this;} while (D1&NBSP;!=&NBSP;D2) {days++;} Return days;} Friend istream& operator>> (istream& in, date& d);friend ostream& operator<< (ostream& out, date& d);p Rivate:int _year;int _month;int _day;}; Istream& operator>> (istream& in, date& d) {in>>d._year;in>>d. _month;in>>d._day;return in;} ostream& operator<< (ostream& out, date& d) {out<<d._year<< "-" <<d._month<< "-" <<d._day<<endl;return out;} Void displaymenu () {cout<< "*************************************" <<endl;cout<< "* Year calendar ************ "<<endl;cout<<" "<<endl;cout<<" **** 1 calculation date before/after N Days of date *** "<<endl;cout<<" **** 2 calculates the number of days between two dates ******* "<<endl;cout<<" **** 0 exit **************** * * "<<endl;cout<<" ************************************* "<<ENDL;} Int main () {int input=0,flag=1;date d1;date d2;while (flag) {DisplayMenu ();cout<< "Please select:" <<endl;cin>>input;switch (input) {case 1:{next1:int k=0;cout<< "Please enter the date:" <<endl; Cin>> d1;if (D1. Judge_ymd () ==false) {cout<< "Illegal date, please reenter" <<ENDL;GOTO&NBSP;NEXT1;} cout<< "Please enter the number of days after:" <<endl;cin>>k; (D1+k). Display1 (); break;} case 2:{next2:int days=0;cout<< "Please enter date 1:" <<endl;cin>> d1;iF (D1. Judge_ymd () ==false) {cout<< "Illegal date, please reenter" <<ENDL;GOTO&NBSP;NEXT2;} next3:cout<< "Please enter date 2:" <<endl;cin>> d2;if (D2. Judge_ymd () ==false) {cout<< "Illegal date, please reenter" <<ENDL;GOTO&NBSP;NEXT3;} days=d1-d2;cout<< "Days of Difference:" <<days<<endl;break;} case 0:{cout<< "Exit" <<endl;break;} default:{cout<< "Input error, please re-enter" <<endl;break;}} if (input==0) {flag=0;} Else{flag=1;}} return 0;}
This article is from the "Flower Open Shore" blog, please be sure to keep this source http://zxtong.blog.51cto.com/10697148/1752749
Day Calculator (date Class)