#include <iostream>
#include<Windows.h>
using namespace std;
class Date
{
friend ostream& operator<<(ostream& _cout, const Date& d);
public:
Date(int year = 1900, int month = 1, int day = 1)
: _year(year)
, _month(month)
, _day(day)
{
if ((_year<0)||
((_month<1) || (_month>12)) ||
((_day<0) || (_day>GetMonthdays(month))))
{
_year = 1990;
_month = 1;
_day = 1;
}
}
Date operator+(int days)
{
if (days < 0)
{
return *this - (0 - days);
}
Date temp(*this);
temp._day += days;
int daysinmonth;
while (temp._day > (daysinmonth =temp.GetMonthdays(temp._month)))
{
temp._day -= daysinmonth;
temp._month++;
if (temp._month > 12)
{
temp._year++;
temp._month = 1;
}
}
return temp;
}
Date operator-(int days)
{
if (days < 0)
{
return *this + (0 - days);
}
Date temp(*this);
temp._day -= days;
int daysinmonth = temp.GetMonthdays(temp._month-1);
while (temp._day <1)
{
temp._month--;
if (temp._month <1)
{
temp._year--;
temp._month = 12;
}
temp._day += (daysinmonth=temp.GetMonthdays(temp._month ));
}
return temp;
}
Date& operator++()
{
*this = *this + 1;
return *this;
}
Date operator++(int)
{
Date temp = *this;
++(*this);
return temp;
}
Date& operator--()
{
*this = *this - 1;
return *this;
}
Date operator--(int)
{
Date temp = *this;
--(*this);
return temp;
}
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 operator==(const Date& d)
{
if (((_year == d._year) && (_month == d._month) && (_day == d._day)))
{
return true;
}
return false;
}
bool operator!=(const Date& d)
{
if (((_year != d._year) ||(_month != d._month) || (_day != d._day)))
{
return true;
}
return false;
}
int operator-(const Date& d)
{
int sumDay = 0;
if (*this == d)
{
return 0;
}
else if (*this>d)
{
Date temp = d;
while (temp != *this)
{
temp++;
sumDay++;
}
return sumDay;
}
else
{
Date temp = *this;
while (temp != d)
{
temp++;
sumDay++;
}
return sumDay;
}
}
private:
int _year;
int _month;
int _day;
int GetMonthdays(int month)
{
int days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (Isleapyear()&&2 == month )
{
days[2] += 1;
}
return days[month];
}
bool Isleapyear()const
{
if (((_year % 4 == 0) && (_year % 100 != 0)) || (_year % 400 == 0))
{
return true;
}
return false;
}
};
ostream& operator<<(ostream& _cout, const Date& d)
{
_cout << d._year << "-" << d._month<<"-"<<d._day;
return _cout;
}
int main()
{
Date d1(2017, 2, 28);
Date d2(2016, 2, 28);
int sum = d2- d1;
cout << d1;
system("pause");
return 0;
}