#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <assert.h>
using namespace std;
class Date
{
public:
//构造函数
Date(int year = 1995, int month = 2, int day = 12)
:_year(year)
, _month(month)
, _day(day)
{
if (IsInvalid()) //判断日期合法性
{
_year = year;
_month = month;
_day = day;
}
}
//检验日期的合法性
bool IsInvalid()
{
if (_year > 0 && _month > 0 && _month<13 && _day>0 && _day <= GetMonthDay(_year, _month))
{
return true;
}
else
{
return false;
}
}
//检测本年份是否为闰年
bool IsLaerYear(int year)
{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
return true;
}
else
return false;
}
//获得某个月份的天数
int GetMonthDay(int year, int month)
{
int day = 0;
if (month > 0 && month < 13)
{
static int days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
day = days[month];
if (month == 2 && IsLaerYear(year))
{
day = 29; //闰年2月29天
}
}
return day;
}
//拷贝构造函数
Date(const Date& d1)
{
_year = d1._year;
_month = d1._month;
_day = d1._day;
}
//赋值运算符的重载
Date& operator=(const Date& d)
{
if (_year == d._year)
{
if (_month == d._month)
{
if (_day == d._day)
{
return *this;
}
}
}
}
//X天之后的日期
Date operator+(int days) //10000
{
Date tmp = *this;
int MonthDay = 0;
while (tmp._day += days)
{
MonthDay = GetMonthDay(tmp._year, tmp._month);
if (tmp._day <= MonthDay)
{
break;
}
days = tmp._day - MonthDay;
tmp._month++;
tmp._day = 0;
if (tmp._month == 12)
{
tmp._year++;
tmp._month = 1;
}
}
return tmp;
}
//X天之前的日期
Date operator-(int days)
{
Date tmp = *this;
int MonthDay = 0;
if (tmp._day == days)
{
tmp._month--;
if (tmp._month < 1)
{
tmp._month = 12;
tmp._year--;
}
tmp._day = GetMonthDay(tmp._year, tmp._month);
days = 0;
}
if (tmp._day > days)
{
tmp._day -= days;
return tmp;
}
days -= tmp._day;
tmp._month--;
if (tmp._month < 1)
{
tmp._month = 12;
tmp._year--;
}
MonthDay = GetMonthDay(tmp._year, tmp._month);
while (days)
{
if(days < MonthDay)
{
tmp._day = MonthDay - days;
days = 0; break;
}
else
{
days -= MonthDay;
tmp._month--;
if (tmp._month < 1)
{
tmp._month = 12;
tmp._year--;
}
MonthDay = GetMonthDay(tmp._year, tmp._month);
}
}
return tmp;
}
//X天之前的日期的优化
//Date operator -(int day)
//{
// Date tmp = *this;
// while (day >= tmp._day)
// {
// tmp._month--;
// day -= tmp._day;
// if (day == tmp._day)
// {
// day = 0;
// }
// if (tmp._month < 1)
// {
// tmp._year--;
// tmp._month = 12;
// }
// tmp._day = GetMonthDay(tmp._year, tmp._month);
// }
// tmp._day -= day;
// return tmp;
//}
//两个日期相减的到的天数,即两个日期中间相隔多少天
int operator-(const Date& d)
{
int day = 0;
int MonthDay = 0;
while ((_year != d._year) || (_month != d._month) || (_day != d._day))
{
day += _day;
_month--;
if (_month < 1)
{
_month = 12;
_year--;
}
_day = GetMonthDay(_year, _month);
if ((_year == d._year) && (_month == d._month) && (_day !=d._day))
{
day += GetMonthDay(_year, _month) - d._day;
_day = d._day;
}
}
return day;
}
//天数的前置加加
Date& operator++()
{
_day++;
if (_day > GetMonthDay(_year, _month))
{
_month++;
_day = 1;
if (_month > 12)
{
_year++;
_month = 1;
}
}
return *this;
}
//天数的后置加加
Date operator++(int)
{
Date tmp = *this;
_day++;
if (tmp._day > GetMonthDay(tmp._year, tmp._month))
{
tmp._month++;
tmp._day = 1;
if (tmp._month > 12)
{
tmp._year++;
tmp._month = 1;
}
}
return tmp;
}
//天数的前置减减
Date& operator--()
{
_day--;
if (_day < 1)
{
_month--;
if (_month < 1)
{
_year--;
_month = 12;
}
_day = GetMonthDay(_year, _month);
}
return *this;
}
//天数的后置减减
Date operator--(int)
{
Date tmp = *this;
_day--;
if (tmp._day < 1)
{
tmp._month--;
if (tmp._month < 1)
{
tmp._year--;
tmp._month = 12;
}
tmp._day = GetMonthDay(tmp._year, tmp._month);
}
return tmp;
}
//输出运算的重载声明
friend ostream& operator<<(ostream& _cout, const Date& d);
private:
int _year;
int _month;
int _day;
};
//输出运算的重载定义
ostream& operator<<(ostream& os, const Date& d)
{
os << d._year << "-" << d._month << "-" << d._day;
return os;
}
int main()
{
Date d1(1970, 5, 14);
Date d2(1970, 11, 4);
Date d3(1995, 2, 11);
//cout << d1 + 100 << endl;
//cout << d1 - 11 << endl;
cout << "d1 = " << d1 << endl;
cout << "d2 = " << d2 << endl;
cout << "d3 = " << d3 << endl;
cout << "d2 - d1 = " << d2 - d1 << endl;
//cout << "--d3 =" << --d3 << endl;
//cout << "d3-- =" << d3-- << endl;
//cout << "++d3 =" << ++d3 << endl;
cout << "d3++ =" << d3++ << endl;
system("pause");
return 0;
}