#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
public:
Date(int year, int month, int day) //构造函数
{
if (year >= 2000 && month > 0 && month<13 && day>0 < GetMonthDay(year, month)) //判断日期是否非法
{
_year = year;
_month = month;
_day = day;
}
else
{
cout << "日期非法" << endl;
assert(false);
}
}
Date(const Date& d) //拷贝构造
:_year(d._year)
, _month(d._month)
, _day(d._day)
{}
Date & operator=(const Date& d)//赋值运算符的重载
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
~Date()
{
}
bool IsInvalid(int year,int month,int day )
{
if ((year < 1) || (month<0 || month>12) || (day<0 || day>GetMonthDay(year, month)))
return false;
return true;
}
bool isLeapYear(int year)
{
if (year % 400==0 || (year % 4==0 && year % 100!=0))
return true;
return 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;
}
void Show()
{
cout << _year << "\t" << _month << "\t" << _day << endl;
}
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)
{
return!(*this<d);
}
bool operator<=(const Date& d)//重载<=(复用函数<和==)
{
return (*this<d||(*this==d));//当<或==满足一个的时候为真
}
bool operator>(const Date& d)//重载<=(复用函数<=)
{
return !(*this <= d);
}
bool operator<(const Date& d) //重载<
{
if (_year < d._year)//判断年
{
return true;
}
else if (_year == d._year)//年相等判断月
{
if (_month < d._month)
{
return true;
}
else if (_month == d._month)//月相等,判断天
{
if (_day < d._day)
{
return true;
}
}
}
else
return false;
}
Date operator+(int day)
{
int ret = GetMonthDay(_year, _month);//获得当年当月天数
Date tmp = *this;//用this创建一个临时对象
while ((_day + day) > ret)
{
if ((tmp._month+1)>12)
{
tmp._year++;//年份加一
tmp._month =0;//月数置零
}
else//否则,月份加一
{
tmp._month++;
}
day -= ret;
ret = GetMonthDay(_year, tmp._month);//获取下一个月的天数
}
tmp._day += day;
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 -= tmp._day;
if (tmp._month == 1)
{
tmp._year--;
tmp._month = 12;
}
else
{
tmp._month--;
}
tmp._day = GetMonthDay(tmp._year, tmp._month);
}
tmp._day = day;
return tmp;
}
Date& operator-=(int day)
{
*this = *this - day;
return *this;
}
Date& operator++()
{
++_day;
if (_day>GetMonthDay(_year,_month))
{
_day -= GetMonthDay(_year, _month);
++ _month;
if (_month>12)
{
++_year;
_month = 1;
}
}
return *this;
}
Date& operator++(int)// 后置
{
Date tmp = *this;
*this = operator++();
return tmp;
}
Date& operator--()
{
if (_day > 1)
{
--_day;
}
else
{
if (_month == 1)
{
--_year;
_month = 12;
_day = GetMonthDay(_year, _month);
}
else
{
--_month;
_day = GetMonthDay(_year, _month);
}
}
return *this;
}
Date& operator--(int)
{
Date tmp = *this;
*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 (!(tmp2 == tmp1))
{
tmp2.operator++();
ret++;
}
return ret;
}
private:
int _year;
int _month;
int _day;
};
void Test1()
{
/*Date d1(2018, 4, 15);
d1.Show();
Date d2 = d1;
d2.Show();
/*Date d3;
d3 = d1;
d2.Show()*/;
Date d1(2018, 4, 15);
Date d2(2018, 4, 16);
d1.Show();
d2.Show();
}
void Test2()
{
Date d1(2018, 9, 5);
d1.Show();
/* Date ret = d1+1;
ret.Show(); */
/* Date ret=d1+70;
ret.Show(); */
/* Date ret=d1-25;
ret.Show(); */
/*Date ret = d1 += 70;
ret.Show();
d1.Show(); */
/* Date ret = d1 -= 70;
ret.Show();
d1.Show();
d1++;
d1.Show();
++d1;
d1.Show();
--d1;
d1.Show();
d1--;
d1.Show(); */
Date d2(2018, 4, 16);
d2.Show();
int ret = d1 - d2;
cout << ret << endl;
}
int main()
{
//Test1();
Test2();
system("pause");
return 0;
}