#include<iostream>
using namespace std;
class Date
{
public:
Date(int year = 2017, int month = 9, int day = 10)
: _year(year)
, _month(month)
, _day(day)
{
//这里是判断如果输入内容不符合日期的格式,就会默认为1990年的1月1日再输出。
if (!((_year > 0) && (_month > 0) && (_month < 13) && (day>0) && (day <= GetMonthDays(_month))))
{
_year = 1990;
_month = 1;
_day = 1;
}
}
//拷贝构造函数,使用类创建新类。
Date(const Date& d)
:_year(d._year)
,_month(d._month)
,_day(d._day)
{}
//输出符号的重载。
ostream& operator<<(ostream& _cout)
{
_cout << _year << '-' << _month << '-' << _day;
return _cout;
}
//赋值的重载。
Date& operator=(const Date& d)
{
_day = d._day;
_month = d._month;
_year = d._year;
return *this;
}
// 前置++
Date& operator++()
{
*this = *this + 1;
return *this;
}
// 后置++
Date operator++(int)
{
Date temp(*this);
*this = *this + 1;
return temp;
}
Date& operator--()
{
*this = *this - 1;
return *this;
}
Date operator--(int)
{
Date temp(*this);
*this = *this - 1;
return temp;
}
//days天之后的日期
Date operator+(int days)
{
Date temp(*this);
temp._day += days; //我们在这里先将天数直接加进去
int daysInmonth = temp.GetMonthDays(temp._month); //在这里我们可以得到这个月的总天数
while (temp._day > daysInmonth)
{
temp._day -= daysInmonth; //我们将这个月的总天数减掉,相当于走过了这些天数,日期就到了下个月的相同天数
temp._month += 1; //走过了天数,我们将月份加1
if (temp._month > 12) //在这里我们判断的是如果月份在12时天数还大于31时的情况
{
temp._month = 1;
temp._year += 1;
}
daysInmonth = temp.GetMonthDays(temp._month);// 由于月份改变,每月的天数我们也要改变
}
return temp;
}
// days天之前的日期
Date operator-(int days) //这个跟上面步骤相似
{
Date temp(*this);
temp._day -= days;
int daysInmonth = temp.GetMonthDays(temp._month);
while (temp._day <= 0)
{
temp._month--;
if (temp._month <= 0)
{
temp._year--;
temp._month = 12;
}
temp._day += temp.GetMonthDays(temp._month);
}
return temp;
}
// 两个日期之间的距离
int operator-(const Date& d)
{
Date max(*this);
Date min(d);
if (min > max)
{
Date temp = max;
max = min;
min = temp;
}
int count = 0;
{
while (min < max)
{
min = min + 1;
count += 1;
}
}
return count;
}
bool operator==(const Date& d)
{
if (_year == d._year&&_month == d._month&&_day == d._day)
{
return true;
}
return false;
}
bool operator!=(const Date& d)
{
return !(*this == d);
}
bool operator>(const Date& d)
{
if ((_year > d._year) || (_year == d._year) && (_month > d._month) || (_year == d._year) && (_month == d._month) && (_day > d._day))
{
return true;
}
return false;
}
bool operator<(const Date& d)
{
if ((_year < d._year) || (_year == d._year) && (_month < d._month) || (_year == d._year) && (_month == d._month) && (_day < d._day))
{
return true;
}
return false;
}
bool IsLeapYear()
{
if ((_year % 4 == 0) && (_year % 100 != 0) || _year % 400 == 0)
{
return true;
}
return false;
}
int GetMonthDays(int month)
{
int days[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if (IsLeapYear() && (2 == month))
{
days[month] += 1;
}
return days[month];
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date a(2016, 2, 28);
/*Date b = a++;*/
Date c(2015, 2, 28);
Date b;
b = a;
b - 100 << cout;
cout << endl;
cout << a - c << endl;
cout << (b > a);
getchar();
return 0;
}
//下图为我的编译器显示结果,