class Date //日期类
{
public:
Date(int year = 2017, int month = 1, int day = 1) //构造函数
:_year(year)
,_month(month)
,_day(day)
{
if( !(_year>0 && (_month>0 && month<13) &&(_day>0 && _day<= GetDaysInMonth(_year,_month)) ))
//如果输入的日期不合法,则默认日期为2017年1月1日
{
_year = 2017;
_month = 1;
_day = 1;
}
}
//////////////////////////////////////////////////////////////////
bool IsLeap(int _year) //判断是否是闰年
{
if((_year%4 == 0 && _year%100 != 0)|| _year%400 == 0 )
{
return true;
}
return false;
}
//////////////////////////////////////////////////////////////////
Date operator+(int days) //days天之后的日期
{
Date temp(*this);
temp._day += days;
while(temp._day < 1)
{
if(temp._month == 1) //当一月份的日期为负数时,需要回到去年的12月
{
temp._year -= 1;
temp._month =12;
}
else
{
temp._month -= 1;
}
temp._day += GetDaysInMonth(temp._year, temp._month);
}
int daysInMonth = 0;
while(temp._day > (daysInMonth = GetDaysInMonth(temp._year,temp._month)))
{
//
temp._day -= daysInMonth;
if(temp._month == 12)
{
temp._year += 1;
temp._month += 1;
}
temp._month += 1;
}
return temp;
}
/////////////////////////////////////////////////////////////
Date operator-(int days) //days天之前的日期
{
if( days < 0)
{
return *this+(0-days); //调用上面days天之后的日期的函数
}
Date temp(*this);
temp._day -= days;
while(temp._day <= 0)
{
if(1 == temp._month)
{
temp._year -= 1;
temp._month = 12;
}
else
temp._month -= 1;
temp._day += GetDaysInMonth(temp._year,temp._month);
}
return temp;
}
////////////////////////////////////////////////////////////////
Date(const Date& d) //拷贝构造函数
:_year(d._year)
,_month(d._month)
,_day(d._day)
{}
///////////////////////////////////////////////////////////////
Date& operator=(const Date& d) //赋值运算符重载
{
this->_year = d._year;
this->_month = d._month;
this->_day = d._day;
return *this;
}
////////////////////////////////////////////////////////////////
bool operator==(const Date& d) //前面的日期和后面的日期是同一天
{
return ((this->_year == d._year)
&& (this->_month == d._month)
&& (this->_day == d._day));
}
////////////////////////////////////////////////////////////////
bool operator!=(const Date& d) //前面的日期和后面的日期不是同一天
{
return !(*this==d);
}
/////////////////////////////////////////////////////////////////
bool operator<(const Date& d) //前面的日期在后面的日期之前
{
if(this->_year < d._year)
{
return true;
}
else if(this->_year > d._year)
{
return false;
}
if(this->_month < d._month)
{
return true;
}
else if(this->_month > d._month)
{
return false;
}
if(this->_day < d._day)
{
return true;
}
else
{
return false;
}
}
////////////////////////////////////////////////////////
bool operator>(const Date& d) //前面的日期在后面的日期之后
{
return !(*this<d || *this==d);
}
///////////////////////////////////////////////////////
bool operator<=(const Date& d)
{
return ((*this<d) || (*this==d));
}
//////////////////////////////////////////////////////
Date& operator++() //前置++
{
this->_day += 1;
int daysInMonth = GetDaysInMonth(this->_year,this->_month);
if(this->_day > daysInMonth)
{
if(this->_month == 12)
{
this->_year += 1;
this->_month =1;
}
else
{
this->_month += 1;
}
this->_day = 1;
}
return *this;
}
////////////////////////////////////////////////////
Date operator++(int) //后置++
{
Date temp(*this);
operator++();
return temp;
}
///////////////////////////////////////////////////
Date& operator--() //前置--
{
if(this->_day == 1)
{
if(this->_month == 1)
{
this->_year -= 1;
this->_month = 12;
this->_day = 31;
}
else
{
this->_month -= 1;
this->_day = GetDaysInMonth(this->_year,this->_month);
}
}
else
{
this->_day -= 1;
}
return *this;
}
///////////////////////////////////////////////////
Date operator--(int) //后置--
{
Date temp(*this);
operator--();
return temp;
}
//////////////////////////////////////////////////////
bool operator>=(const Date& d)
{
return ((*this>d) || (*this==d));
}
////////////////////////////////////////////////////
int operator-(const Date& d) //求两个日期之间相差多少天
{
Date later = (*this>d)?(*this):d;
Date former = (*this<d)?(*this):d;
int count = 0;
while(later != former)
{
former++;
count++;
}
return count;
}
//////////////////////////////////////////////////////
~Date() //析构函数
{}
private:
int GetDaysInMonth(int year,int month) //根据年份和月份判断本月有多少天
{
int days[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
//用数组存放每月天数时,为了使下标从1开始(与月份对应),所以加了一个0
if( month == 2 && IsLeap(year) ) //闰年2月是29天
{
days[month] += 1;
}
return days[month];
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1(2017,2,21);
Date d2(2016,2,21);
int ret = (d1-d2);
return 0;
}