#include<iostream>
#include<stdlib.h>
using namespace std;
class Date
{
public:
bool IsLeapYear(int year)
{
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}
int GetDayInMonth(int year, int month)
{
int months[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (IsLeapYear(year))
{
months[2] = 29;
}
return months[month];
}
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
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 operator+(int days)
{
_day += days;
if (_day > GetDayInMonth(_year, _month))
{
_month += 1;
if (_month > 12)
{
_year += 1;
_month = 1;
}
_day = 1;
}
return *this;
}
Date operator-(int days)
{
if (days < 0)//天数若为负数变为加
{
return *this + (0 - days);
}
Date temp(*this);
temp._day -= days;
while (temp._day <= 0)
{
temp._month--;
if (temp._month <= 0)
{
temp._year--;
temp._month = 12;
}
temp._day += temp.GetDayInMonth(temp._year,temp._month);
}
return temp;
}
int operator-(const Date& d)
{
Date maxdate(*this);
Date mindate(d);
if (maxdate < mindate)
{
maxdate = mindate;
mindate = *this;
}
int count = 0;
while (mindate < maxdate)
{
mindate = mindate + 1;
++count;
}
return count;
}
Date& operator++()
{
_day += 1;
if (_day > GetDayInMonth(_year, _month))
{
_month += 1;
if (_month > 12)
{
_year += 1;
_month = 1;
}
_day = 1;
}
return *this;
}
Date operator++(int)
{
Date temp = *this;
_day += 1;
return temp;
}
Date& operator--()
{
_day -= 1;
if (_day == 0)
{
_month -= 1;
if (_month == 0)
{
_year -= 1;
_month = 12;
_day = GetDayInMonth(_year - 1, 12);
}
else{
_day = GetDayInMonth(_year, _month);
}
}
return *this;
}
Date operator--(int)
{
Date temp = *this;
_day -= 1;
return temp;
}
bool operator>(const Date& d)const
{
while (_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)const
{
while (_year > d._year || (_year == d._year&&_month > d._month) ||
(_year == d._year && _month == d._month && _day > d._day) ||
(_year == d._year && _month == d._month && _day == d._day))
{
return true;
}
return false;
}
bool operator<(const Date& d)const
{
while (_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)const
{
while (_year <d._year || (_year == d._year&&_month < d._month) ||
(_year == d._year && _month == d._month && _day < d._day) ||
(_year == d._year && _month == d._month && _day < d._day))
{
return true;
}
return false;
}
bool operator==(const Date& d)const
{
if (_year == d._year && _month == d._month && _day == d._day)
return true;
return false;
}
bool operator!=(const Date& d)const
{
if (_year != d._year || _month != d._month || _day != d._day)
return true;
return false;
}
friend ostream& operator<<(ostream& _cout, const Date& d);
private:
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream& _cout, const Date& d)
{
_cout << d._year << "-" << d._month << "-" << d._day;
return _cout;
}
void FunTest()
{
Date d1(1900, 2, 28);
cout << ++d1 << endl;
cout << --d1 << endl;
cout << d1 << endl;
cout << d1-- << endl;
cout << d1++ << endl;
cout << d1 << endl;
Date d2(2017, 5, 1);
cout << d2 + 100 << endl;
Date d3(1997, 11, 1);
cout << d3 - 100 << endl;
cout << boolalpha << (Date(2016, 10, 1) < d1) << endl;
cout << d1 - d2 << endl;
}
int main()
{
FunTest();
system("pause\n");
return 0;
}