--------------------------------------------------------------------------
++++
----
---------------------------------------------------------------------------
#include<iostream>
using namespace std;
class Date{
public:
Date(int year = 1900,int month = 1,int day = 1)
:_year(year)
,_month(month)
,_day(day)
{
if(!isInvalidDate(_year,_month,_day))
{
cout<<"日期不正确,已自动修正"<<endl;
_year = 1900;
_month = 1;
_day = 1;
}
}
Date(const Date& d)
{
if(this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
}
~Date()
{}
int GetMonthDay(int year,int month)
{
int days[13]= {-1,31,28,31,30,31,30,31,31,30,31,30,31};
if((year%4 == 0 && year%100!=0) || (year%400 == 0))
{
days[2] = 29;
}
return days[month];
}
bool isInvalidDate(int year,int month,int day)
{
if( year >=0 &&
(month > 0 && month <13)
&&(day > 0 && day <= GetMonthDay(year,month)))
return true;
else
return false;
}
void Display()
{
cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
}
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);
}
bool operator<=(const Date& d)
{
return (*this < d)||(*this == d);
}
bool operator==(const Date& d)
{
return (_year == d._year && _month == d._month && _day == d._day);
}
bool operator!= (const Date& d)
{
return !(*this == d);
}
Date& operator=(const Date& d)
{
if(*this != d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
Date operator+(int day)
{
if(day < 0)
{
return operator-(-day);
}
Date tmp = *this;
if(isInvalidDate(tmp._year,tmp._month,tmp._day+day))
{
tmp._day += day ;
return tmp;
}
int Sumday = tmp._day + day;
while(Sumday > GetMonthDay(tmp._year,tmp._month))
{
Sumday = Sumday-GetMonthDay(tmp._year,tmp._month);
tmp._month++;
if(tmp._month ==13)
{
tmp._year++;
tmp._month = 1;
}else
{
tmp._day = Sumday;
}
}
return tmp;
}
Date& operator+=(int day)
{
*this = *this + day;
return *this;
}
Date operator-(int day)
{
if(day < 0)
{
return operator+(-day);
}
Date tmp = *this;
while(day >= tmp._day)
{
day = day -tmp._day;
if(tmp._month == 1)
{
tmp._year--;
tmp._month = 12;
}
else
{
tmp._month--;
}
tmp._day = GetMonthDay(tmp._year,tmp._month);
}
tmp._day = tmp._day - day;
return tmp;
}
Date& operator-=(int day)
{
*this = *this - day;
return *this;
}
Date& operator++()
{
_day = _day+1;
if(_day > GetMonthDay(_year,_month))
{
_day = _day - GetMonthDay(_year,_month);
_month++;
if(_month == 13)
{
_month = 1;
_year++;
}
}
return *this;
}
Date operator++(int)
{
Date tmp = *this;
operator++();
return tmp;
}
Date& operator--()
{
if(_day > 1)
{
_day --;
}
else
{
if(_month == 1)
{
_month = 12;
_year --;
_day = GetMonthDay(_year,_month);
}else
{
--_month;
_day = GetMonthDay(_year,_month);
}
}
return *this;
}
Date operator--(int)
{
Date tmp = *this;
operator--();
return tmp;
}
int operator-( Date& d)
{
if(_year < d._year)
{
Date tmp = *this;
*this = d;
d = tmp;
}
else if(_year == d._year && _month < d._month)
{
Date tmp = *this;
*this = d;
d = tmp;
}
else if(_year == d._year && _month == d._month && _day < d._day)
{
Date tmp = *this;
*this = d;
d = tmp;
}
Date tmp1(*this);
Date tmp2(d);
int ret = 0;
while(tmp1 != tmp2)
{
tmp2++;
ret++;
}
return ret;
}
private:
int _year;
int _month;
int _day;
};
void test1()
{
Date d1(2018, 3,20);
d1.Display();
Date d2(2019, 1, 1);
d2.Display();
int ret = d1 - d2;
cout <<d1 - d2 << endl;
}
int main()
{
test1();
return 0;
}