#define _CRT_SECURE_NO_WARNINGS#include #include using namespace std;class Date{public:Date(int _year, int _month, int _day); //构造函数void ShowDate(){cout << year << "-" << month << "-" << day << endl;}bool operator<(const Date& d); //小于运算符的重载bool operator==(const Date& d); //等于运算符的重载bool operator>(const Date& d); //大于运算符的重载bool operator<=(const Date& d); //小于等于运算符的重载bool operator>=(const Date& d); //大于等于运算符的重载Date operator+(int day); //某个日期+天数Date &operator+=(int day); //某个日期+=天数Date operator-(int day); //某个日期-天数Date &operator-=(int day); //某个日期-=天数Date& operator++(); //某个日期++(前置)Date operator++(int); //某个日期++(后置)Date& operator--(); //某个日期--(前置)Date operator--(int); //某个日期--(后置)int operator-( Date& d); //两个日期相减friend bool IsTrueDate(Date &d); //友元函数,判断某个日期是否是合法日期private:int year;int month;int day;};Date::Date(int _year, int _month, int _day) : // 用初始化列表初始化构造函数year(_year),month(_month),day(_day){}bool Date::operator<(const Date& d) //小于运算符的重载{return this->year < d.year || this->year == d.year && this->month year == d.year && this->month == d.month && this->day year == d.year && this->month == d.month && this->day == d.day;}bool Date::operator>(const Date& d) //大于运算符的重载{return this->year > d.year || this->year == d.year && this->month>d.month ||this->year == d.year && this->month == d.month && this->day>d.day;}bool Date::operator<=(const Date& d) //小于等于运算符的重载{return !(*this > d);}//bool Date::operator<=(const Date& d) //小于等于运算符的重载//{//return *this < d && *this == d;//}bool Date::operator>=(const Date& d) //大于等于运算符的重载{return !(*this < d);}//bool Date::operator>=(const Date& d) //大于等于运算符的重载//{//return *this > d && *this == d;//}bool IsLeapYear(int year) //判断某年是否是闰年{if ((year % 400 == 0) || ((year % 4 == 0) && (year % 1001 != 0))){return true;}elsereturn false;}int GetMonthDay(int year, int 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)){day += 1;}return day;}//日期计算器Date& Date:: operator += (int day){*this=*this + day;return *this;}Date Date::operator+ (int day){if (day<0){*this -(-day);}Date tmp(*this);if (IsTrueDate(*this)){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++;}}}else{cout << "The date is eror!" << endl;tmp.year = 1900;tmp.month = 1;tmp.day = 1;}return tmp;}Date &Date::operator -= (int day){*this = *this - day;return *this;}Date Date::operator - (int day){if (day<0){*this + (-day);}Date tmp(*this);if (IsTrueDate(*this)){tmp.day -= day;while (tmp.day <= 0){if (tmp.month == 1){tmp.month = 12;tmp.year--;}else{tmp.month--;}tmp.day += GetMonthDay(tmp.year, tmp.month);}}return tmp;}Date Date::operator--(int) //后置--{Date tmp(*this);*this -= 1;return tmp;}Date Date::operator++(int) //后置++{Date tmp(*this);*this += 1;return tmp;}Date& Date::operator--() //前置--{*this -= 1;return *this;}Date& Date::operator++() //前置++{*this += 1;return *this;}bool IsTrueDate(Date &d){if (d.year > 1900 && d.month > 0 && d.month<13 && d.day>0 &&d.day <= GetMonthDay(d.year, d.month)){return true;}else{cout << "The date is eror!" << endl;d.year = 1900;d.month = 1;d.day = 1;return false;}}int Date::operator - ( Date& d) {int days=0;int flag = 1;if (IsTrueDate(*this) && IsTrueDate(d)){while (!(*this == d)){if (*this > d){d.day++;days++;}else{this->day++;days++;flag = -1;}}}return flag * days;}