#include<iostream>
using namespace std;
int Year[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
class Date
{
public:
Date(int year1 = 1900, int month1 = 1, int day1 = 1) // 全缺省的构造函数
{
if (year1 >= 0 && month >= 1 && month1 <= 12 && day1 >= 1 && day1 <= GetMonthDay(year1, month1))
{
year = year1;
month = month1;
day = day1;
}
else
{
cout << "非法日期" << endl;
cout << year << "年" << month << "月" << day << "日" << endl;
}
}
inline int GetMonthDay(int year1, int month1) // 获取某年某月的天数
{
if ((year1 % 4 == 0 && year1 % 100 != 0) || year1 % 400 == 0)
{
if (month1 == 2)
return 29;
else
return Year[month1];
}
else
return Year[month1];
}
Date(const Date& d) //拷贝构造函数
{
year = d.year;
month = d.month;
day = d.day;
}
Date& operator=(const Date& d)
{
year = d.year;
month = d.month;
day = d.day;
}
~Date();
Date& operator+=(int day1)
{
if (day1 < 0)
{
//复用operator-=
*this -= -day1;
}
else
{
day += day1;
/*
天数加上去后,有可能不合法,需要调整,调整过程中月份可能不合法,因此月份也要进行调整,月份调整到了13,年数就要加1
举个例子
2020.12.1 + 128
2020.12.129 129>31
2020.13.98 13>12
2021.1.98 98>31
2021.2.67 67>28
2021.3.39 39>31
2021.4.8 1<8<31 合法
*/
while (day>GetMonthDay(year, month))
{
day -= GetMonthDay(year, month);
month++;
if (month > 12)
{
month = 1;
year++;
}
}
}
return *this;
}
Date operator+(int day1) const //上面的+=函数和这个+函数的区别是,上面的对象的参数发生了改变,而这个对象本身并不变
{
Date tmp(*this); //拷贝构造tmp用于返回,因为this本身是不会改变的,这就是+=和+的区别
tmp += day1;
return tmp;
}
Date operator-(int day1)
{
Date tmp(*this);
tmp -= day1;
return tmp;
}
Date& operator-=(int day1)
{
if (day1 < 0)
{
*this += -day1;
}
else
{
day -= day1;
/*
2020.2.10 - 80 day每减一次,month就跟着减一次
2020.1.-70 -70<=0 -70+31
2020.0.-39 0<1
2019.12.-39 -39<=0 -39+31
2019.11.-8 -8<=0 -8+30
2019.11.22
*/
while (day <= 0)
{
month--;
if (month < 1)
{
month = 12;
year--;
}
day += GetMonthDay(year, month);
}
}
return *this;
}
Date& operator++() //前置++
{
*this += 1;
return *this;
}
Date operator++(int) //后置++
{
Date tmp(*this);
*this += 1;
return tmp;
}
Date& operator--() // 前置--
{
*this -= 1;
return *this;
}
Date operator--(int) // 后置--
{
Date tmp(*this);
*this -= 1;
return tmp;
}
bool operator>(const Date& d)
{
if (year > d.year)
return true;
else if (year == d.year && month > d.month)
return true;
else if (year == d.year && month == d.month && day > d.day)
return true;
return false;
}
bool operator==(const Date& d)
{
return year == d.year && month == d.month && day == d.day;
}
inline bool operator >= (const Date& d)
{
//满足大于或者等于其中之一即可
return *this > d || *this == d;
}
bool operator < (const Date& d)
{
if (year < d.year)
return true;
else if (year == d.year && month < d.month)
return true;
else if (year == d.year && month == d.month && day < d.day)
return true;
return false;
}
bool operator <= (const Date& d)
{
return *this < d || *this == d;
}
bool operator != (const Date& d)
{
return !(*this == d);
}
int operator-(const Date& d) //两日期相减所差天数
{
Date max = *this;
Date min = d;
int flag = 1; //若*this>d,相减结果为正,flag=1,反之为负,flag=-1
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (min < max)
{
min++;
n++;
}
return n*flag;
}
void display()
{
cout << year << "年" << month << "月" << day << "日" << endl;
}
private:
int year;
int month;
int day;
};
int main()
{
return 0;
}