#pragmaonce#include<iostream>usingnamespace std;classDate{friend ostream&operator<<(ostream& out,const Date& d);friend istream&operator>>(istream& in, Date& d);public:Date(int year =1,int month =1,int day =1);voidPrint()const;intGetMonthDay(int year,int month)const;booloperator>(const Date& d)const;booloperator<(const Date& d)const;booloperator>=(const Date& d)const;booloperator<=(const Date& d)const;booloperator==(const Date& d)const;booloperator!=(const Date& d)const;//d1+=100
Date&operator+=(int day);//d1+100
Date operator+(int day)const;//d1-=100
Date&operator-=(int day);
Date operator-(int day)const;//++d1
Date&operator++();//d1++
Date operator++(int);
Date&operator--();
Date operator--(int);//两个日期之间的差intoperator-(const Date& d)const;voidPrintWeekDay()const;private:int _year;int _month;int _day;};
ostream&operator<<(ostream& out,const Date& d);
istream&operator>>(istream& in, Date& d);
源文件 Date.cpp
#include"Date.h"intDate::GetMonthDay(int year,int month)const{staticint monthDayArray[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};int day = monthDayArray[month];// 365天 5小时+if(month ==2&&((year %4==0&& year %100!=0)||(year %400==0))){
day +=1;}return day;}Date::Date(int year,int month,int day){
_year = year;
_month = month;
_day = day;if(!(_year >=0&&(month >0&& month <13)&&(day >0&& day <=GetMonthDay(year, month)))){
cout <<"非法日期->";// this->Print();Print();}}voidDate::Print()const{
cout << _year <<"-"<< _month <<"-"<< _day << endl;}bool Date::operator>(const Date& d)const{if(_year > d._year){returntrue;}elseif(_year == d._year && _month > d._month){returntrue;}elseif(_year == d._year && _month == d._month && _day > d._day){returntrue;}else{returnfalse;}}// d1 == d2bool Date::operator==(const Date& d)const{return _year == d._year
&& _month == d._month
&& _day == d._day;}// d1 < d2bool Date::operator<(const Date& d)const{return!(*this>= d);}// d1 >= d2bool Date::operator>=(const Date& d)const{return*this> d ||*this== d;}bool Date::operator<=(const Date& d)const{return!(*this> d);}bool Date::operator!=(const Date& d)const{return!(*this== d);}// d1 += 100
Date& Date::operator+=(int day){if(day <0){return*this-=-day;}
_day += day;while(_day >GetMonthDay(_year, _month)){
_day -=GetMonthDay(_year, _month);++_month;if(_month ==13){
_month =1;
_year++;}}return*this;}// d1 + 100
Date Date::operator+(int day)const{
Date ret(*this);//ret.operator+=(day);
ret += day;return ret;}// d1 -= 10
Date& Date::operator-=(int day){if(day <0){return*this+=-day;}
_day -= day;while(_day <=0){--_month;if(_month ==0){--_year;
_month =12;}
_day +=GetMonthDay(_year, _month);}return*this;}// d1 - 10
Date Date::operator-(int day)const{
Date ret(*this);
ret -= day;return ret;}// ++d1;
Date& Date::operator++(){*this+=1;return*this;}// d1++; 后置为了跟前置++,进行区分// 增加一下参数占位,跟前置++,构成函数重载
Date Date::operator++(int){
Date ret(*this);*this+=1;return ret;}// --d1;
Date& Date::operator--(){*this-=1;return*this;}
Date Date::operator--(int){
Date ret(*this);*this-=1;return ret;}// offerDay - todayint Date::operator-(const Date& d)const{
Date max =*this;
Date min = d;int flag =1;if(*this< d){
max = d;
min =*this;
flag =-1;}int count =0;while(min != max){++min;++count;}return count * flag;}voidDate::PrintWeekDay()const{constchar* arr[]={"星期一","星期二","星期三","星期四","星期五","星期六","星期天"};int count =*this-Date(1900,1,1);
cout << arr[count %7]<< endl;}
ostream&operator<<(ostream& out,const Date& d){
out << d._year <<"-"<< d._month <<"-"<< d._day << endl;return out;}
istream&operator>>(istream& in, Date& d){
in >> d._year;
in >> d._month;
in >> d._day;return in;}
源文件 Test.cpp
#include"Date.h"voidTestDate1(){
Date d1;
Date d2(2022,1,16);
d1.Print();
d2.Print();
Date d3(2022,13,15);
d3.Print();
Date d4(2022,2,29);
d4.Print();
Date d5(2000,2,29);
d4.Print();}voidTestDate2(){
Date d1(2022,1,16);
Date ret = d1 +100;
ret.Print();
d1 +=100;
d1.Print();}voidTestDate3(){
Date d1(2022,2,2);
Date ret1 = d1 -10;
ret1.Print();
Date ret2 = d1 -17;
ret2.Print();
Date ret3 = d1 -30;
ret3.Print();
Date ret4 = d1 -400;
ret4.Print();
Date ret5 = d1 -1500;
ret5.Print();
Date ret6 = d1 --100;
ret6.Print();}voidTestDate4(){
Date today(2022,1,17);
Date offerDay(2022,9,1);
cout <<(offerDay - today)<< endl;
cout <<(today - offerDay)<< endl;
today.PrintWeekDay();
Date today1(2022,1,17);
today1.PrintWeekDay();Date(2022,1,17).PrintWeekDay();}intmain(){/*Date d;
cin >> d;
cout << d << endl;*/TestDate3();return0;}