#include<iostream>
#include<stdlib.h>
using namespace std;
class Date
{
friend ostream& operator<<(ostream& os,const Date& d);
friend istream& operator>>(istream& is,Date& d);
public:
Date(int year=0,int month=0,int day=0);
Date(const Date& d);
Date& operator=(const Date& d);
~Date();
public:
Date& operator++();//因为可能会遇到++(++a);所以引用返回。
Date operator++(int);
Date& operator--();//同上--(--b);
Date operator--(int);
Date operator+(int);
Date operator-(int);
Date& operator+=(int day);
Date& operator-=(int);
int operator-(const Date& d);
public:
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);
bool operator==(const Date& d);
void Display();
private:
int Leapyear(int year)
{
if((year%4==0&&year0!=0)||(year@0==0))
return 1;
else
return -1;
}
int Getday(int year,int month)
{
int D[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int ret=Leapyear(year);
if(ret==1)
{
D[2]=29;
}
return D[month];
}
int _year;
int _month;
int _day;
};Date::Date(int year,int month,int day)
:_year(year),_month(month),_day(day)
{}
Date::Date(const Date& d)
:_year(d._year),_month(d._month),_day(d._day)
{}
Date& Date::operator=(const Date& d)
{
if(this!=&d)
{
_year=d._year;
_month=d._month;
_day=d._day;
}
return *this;
}
Date::~Date()
{}
Date &Date::operator++()
{
_day++;
if(_day>Getday(_year,_month))
{
if(_month>12)
{
_year++;
_month=1;
_day=1;
}
else
{
_month++;
_day=1;
}
}
return*this;
}
Date Date::operator++(int)
{
Date tmp=*this;
if(tmp._day>Getday(_year,_month))
{
if(tmp._month>12)
{
tmp._year++;
tmp._month=1;
tmp._day=1;
}
else
{
tmp._month++;
tmp._day=1;
}
}
tmp._day++;
return tmp;
}
Date &Date::operator--()
{
_day--;
if(_day==0)
{
_month--;
if(_month==0)
{
_year--;
_month=12;
}
_day=Getday(_year,_month);
}
return *this;
}
Date Date::operator--(int)
{
Date tmp=*this;
if(tmp._day==1&&tmp._month==1)
{
tmp._year--;
tmp._month=12;
tmp._day=Getday(tmp._year,tmp._month);
}
else if(tmp._day==1&&tmp._month!=1)
{
tmp._month--;
tmp._day=Getday(tmp._year,tmp._month);
}
else
{
tmp._day--;
}
return tmp;
}
Date Date::operator+(int day)
{
Date tmp=*this;
if(day<0)
{
return tmp-day;
}
tmp._day=tmp._day+day;
while(tmp._day>Getday(tmp._year,tmp._month))
{
tmp._day=tmp._day-Getday(tmp._year,tmp._month);
if(tmp._month==12)
{
tmp._year++;
tmp._month=1;
}
tmp._month++;
}
return tmp;
}
Date Date::operator-(int day)
{
Date tmp=*this;
if(day>0)
{
return tmp+day;
}
tmp._day=tmp._day-day;
while(tmp._day>0&&tmp._day<Getday(tmp._year,tmp._month))
{
tmp._day=tmp._day+Getday(tmp._year,tmp._month);
if(tmp._month==1)
{
tmp._year--;
tmp._month=12;
}
tmp._month--;
}
return tmp;
}
Date& Date::operator+=(int day)
{
_day+=day;
while(_day>Getday(_year,_month))
{
_day-=Getday(_year,_month);
if(_month=12)
{
_year++;
_month=1;
}
else
_month++;
}
return *this;
}
Date& Date::operator-=(int day)
{
_day-=day;
while(_day<0)
{
_day=_day+Getday(_year,_month);
if(_month==1)
{
_year--;
_month=12;
}
else
_month--;
}
return *this;
}
int Date::operator-(const Date& d)
{
Date Maxd=*this;
Date Mind=d;
if(Maxd < Mind)
{
Maxd= d;
Mind=*this;
}
int day=0;
while(1)
{
if(Maxd==Mind+day)
{
break;
}
day++;
}
return day;
}
bool Date::operator>(const Date& d)
{
if(_year>d._year||(_year==_year)&&(_month>d._month)||(_month==d._month)&&(_day>_day))
{
return true;
}
else
{
return false;
}
}
bool Date::operator>=(const Date&d)
{
if(*this<d)
{
return false;
}
else
{
return true;
}
}
bool Date::operator<(const Date& d)
{
if(_year<d._year||(_year==_year)&&(_month<d._month)||(_year==d._year)&&(_month==d._month)&&(_day<_day))
{
return true;
}
else
{
return false;
}
}
bool Date::operator<=(const Date& d)
{
if((*this)>d)
{
return false;
}
else
return true;
}
bool Date::operator==(const Date& d)
{
return (_year==d._year)&&(_month==d._month)&&(_day==d._day);
}
bool Date::operator!=(const Date& d)
{
return (_year!=d._year)||(_month!=d._month)||(_day!=d._day);
}
void Date::Display()
{
cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
}
ostream& operator<<(ostream& os,const Date& d)
{
os<<d._year<<"-"<<d._month<<"-"<<d._day<<endl;
return os;
}
istream& operator>>(istream& is, Date& d)
{
is>>d._year>>d._month>>d._day;
return is;
}
int main()
{
int day=0;
Date d1(2017,1,1);
Date d2(2016,1,1);
Date d3(d1);
/*day=d2-d1;*/
/*d1=d1+100;
d2=d2-100;*/
/*++d1;*/
/*--d1;*/
cout<<day<<endl;
cout<<"d1:"<<d1<<endl;
cout<<"d2:"<<d2<<endl;
cout<<"d3:"<<d3<<endl;
system("pause");
}