#include <iostream>
#include<stdlib.h>
using namespace std;
class Date
{
friend ostream& operator<<(ostream& out, const Date& d);
public:
// 全缺省的构造函数
Date(int year = 0, int month = 0, int day = 0) :m_year(year), m_month(month), m_day(day)
{
}
// 拷贝构造函数
Date(const Date& d)
{
m_year = d.m_year;
m_month = d.m_month;
m_day = d.m_day;
}
// 赋值运算符重载
Date& operator=(const Date& d)
{
if (this != &d)
{
m_year = d.m_year;
m_month = d.m_month;
m_day = d.m_day;
}
}
//获取天数
int GetDayByYM(int year, int month)
{
//保存一到十二月的天数
int days[] = { 29,31,28,31,30,31,30,31,31,30,31,30,31 };
if (month == 2)
{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
month = 0;
}
return days[month];
}
// 日期+=天数
Date& operator+=(int n)
{
int days = GetDayByYM(m_year,m_month);
int day = n + m_day;
while (day > days)
{
m_month++;
if (m_month > 12)
{
m_year++;
m_month = 1;
}
n -= days;
days = GetDayByYM(m_year,m_month);
day = n + m_day;
}
m_day += n;
return *this;
}
// 日期+天数
Date operator+(int n)
{
Date tmp = *this;
int days = GetDayByYM(tmp.m_year, tmp.m_month);
int day = n + tmp.m_day;
while (day > days)
{
tmp.m_month++;
if (tmp.m_month > 12)
{
tmp.m_year++;
tmp.m_month = 1;
}
n -= days;
days = GetDayByYM(tmp.m_year, tmp.m_month);
day = n + tmp.m_day;
}
tmp.m_day += n;
return tmp;
}
// 日期-天数
Date operator-(int day)
{
Date tmp = *this;
if (day < 0)
return tmp + abs(day);
tmp.m_day -= day;
while (tmp.m_day <= 0)
{
if (tmp.m_month == 1)
{
tmp.m_year--;
tmp.m_month = 12;
}
else
{
tmp.m_month--;
}
tmp.m_day += GetDayByYM(tmp.m_year, tmp.m_month);
}
return tmp;
}
// 日期-=天数
Date& operator-=(int day)
{
m_day -= day;
while (m_day <= 0)
{
if (m_month == 1)
{
m_year--;
m_month = 12;
}
else
{
m_month--;
}
m_day += GetDayByYM(m_year, m_month);
}
return *this;
}
// 前置++
Date& operator++()
{
*this = *this + 1;
return *this;
}
// 后置++
Date operator++(int)
{
Date tmp = *this;
++*this;
return tmp;
}
// 后置--
Date operator--(int)
{
Date tmp = *this;
--* this;
return tmp;
}
// 前置--
Date& operator--()
{
*this = *this - 1;
return *this;
}
// >运算符重载
bool operator>(const Date& d)
{
return (m_year > d.m_year || m_year == d.m_year && m_month > d.m_month || m_year == d.m_year && m_month == d.m_month && m_day > d.m_day);
}
// ==运算符重载
bool operator==(const Date& d)
{
return m_year == d.m_year && m_month == d.m_month && m_day == d.m_day;
}
// >=运算符重载
inline bool operator <= (const Date& d)
{
return !(*this < d);
}
// <运算符重载
bool operator < (const Date& d)
{
return (m_year < d.m_year || m_year == d.m_year && m_month < d.m_month || m_year == d.m_year && m_month == d.m_month && m_day < d.m_day);
}
// <=运算符重载
bool operator >= (const Date& d)
{
return !(*this < d);
}
// !=运算符重载
bool operator != (const Date& d)
{
return !(*this == d);
}
// 日期-日期 返回天数
int operator-(const Date& d)
{
int flag = 1;
Date Max=*this;
Date Min=d;
if ((*this) < d)
{
std::swap(Max, Min);
flag = -1;
}
int days = 0;
while (Max != Min)
{
--Max;
++days;
}
return days * flag;
}
private:
int m_year;
int m_month;
int m_day;
};
ostream& operator<<(ostream& out, const Date &d)
{
cout << d.m_year << "年" << d.m_month << "月" << d.m_day << "日" << endl;
return out;
}