#include <iostream>
using namespace std;
class Date
{
public:
Date(int year = 2017, int month = 9, int day = 20)
: _year(year)
, _month(month)
, _day(day)
{}
Date& operator=(const Date& d);
// 前置++
Date& operator++();
// 后置++
Date operator++(int);
Date& operator--();
Date operator--(int);
//days天之后的日期
Date operator+(int days);
// days天之前的日期
Date operator-(int days);
// 两个日期之间的距离
int operator-(const Date& d);
bool operator==(const Date& d);
bool operator!=(const Date& d);
bool operator>(const Date& d);
bool operator<(const Date& d);
private:
int _year;
int _month;
int _day;
};
Date& Date::operator=(const Date& d)
{
if (this != &d)
{
this->_year = d._year;
this->_month = d._month;
this->_day = d._day;
}
return *this;
}
Date& Date::operator++()
{
if ((this->_month == 1) || (this->_month == 3)
|| (this->_month == 5) || (this->_month == 7)
|| (this->_month == 8) || (this->_month == 10)
|| (this->_month == 12)
)
{
if ((this->_day == 31)&&(this->_month) != 12)
{
this->_month += 1;
this->_day = 1;
}
else if ((this->_day == 31) && (this->_month) == 12)
{
this->_year += 1;
this->_month = 1;
this->_day = 1;
}
else
{
this->_day += 1;
}
}
else if (this->_month == 2)
{
if (((this->_year % 4 == 0) && (this->_year % 100 != 0)) || (this->_year % 400 == 0))
{
if (this->_day == 29)
{
this->_month += 1;
this->_day = 1;
}
else
{
this->_day += 1;
}
}
else
{
if (this->_day == 28)
{
this->_month += 1;
this->_day = 1;
}
else
{
this->_day += 1;
}
}
}
else
{
if (this->_day == 30)
{
this->_month += 1;
this->_day = 1;
}
else
{
this->_day += 1;
}
}
return *this;
}
Date Date::operator++(int)
{
Date temp;
temp = *this;
if ((this->_month == 1) || (this->_month == 3)
|| (this->_month == 5) || (this->_month == 7)
|| (this->_month == 8) || (this->_month == 10)
|| (this->_month == 12)
)
{
if ((this->_day == 31) && (this->_month) != 12)
{
this->_month += 1;
this->_day = 1;
}
else if ((this->_day == 31) && (this->_month) == 12)
{
this->_year += 1;
this->_month = 1;
this->_day = 1;
}
else
{
this->_day += 1;
}
}
else if (this->_month == 2)
{
if (((this->_year % 4 == 0) && (this->_year % 100 != 0)) || (this->_year % 400 == 0))
{
if (this->_day == 29)
{
this->_month += 1;
this->_day = 1;
}
else
{
this->_day += 1;
}
}
else
{
if (this->_day == 28)
{
this->_month += 1;
this->_day = 1;
}
else
{
this->_day += 1;
}
}
}
else
{
if (this->_day == 30)
{
this->_month += 1;
this->_day = 1;
}
else
{
this->_day += 1;
}
}
return temp;
}
Date& Date::operator--()
{
if ((this->_month == 2) || (this->_month == 4)
|| (this->_month == 6) || (this->_month == 7)
|| (this->_month == 9) || (this->_month == 11)
)
{
if (this->_day == 1)
{
this->_month -= 1;
this->_day = 31;
}
else
{
this->_day -= 1;
}
}
else if (this->_month == 3)
{
if (((this->_year % 4 == 0) && (this->_year % 100 != 0)) || (this->_year % 400 == 0))
{
if (this->_day == 1)
{
this->_month -= 1;
this->_day = 29;
}
else
{
this->_day -= 1;
}
}
else
{
if (this->_day == 1)
{
this->_month -= 1;
this->_day = 28;
}
else
{
this->_day -= 1;
}
}
}
else
{
if ((this->_month == 1)&& this->_day == 1)
{
_year -= 1;
_month = 12;
_day = 31;
}
else if (this->_day == 1)
{
this->_month -= 1;
this->_day = 30;
}
else
{
this->_day -= 1;
}
}
return *this;
}
Date Date::operator--(int)
{
Date temp;
temp = *this;
if ((this->_month == 2) || (this->_month == 4)
|| (this->_month == 6) || (this->_month == 8)
|| (this->_month == 9) || (this->_month == 11)
)
{
if (this->_day == 1)
{
this->_month -= 1;
this->_day = 31;
}
else
{
this->_day -= 1;
}
}
else if (this->_month == 3)
{
if (((this->_year % 4 == 0) && (this->_year % 100 != 0)) || (this->_year % 400 == 0))
{
if (this->_day == 1)
{
this->_month -= 1;
this->_day = 29;
}
else
{
this->_day -= 1;
}
}
else
{
if (this->_day == 1)
{
this->_month -= 1;
this->_day = 28;
}
else
{
this->_day += 1;
}
}
}
else
{
if (this->_day == 1)
{
this->_month -= 1;
this->_day = 30;
}
else
{
this->_day -= 1;
}
}
return temp;
}
Date Date::operator+(int days)
{
Date temp;
temp = *this;
while (days--)
{
++temp;
}
return temp;
}
Date Date::operator-(int days)
{
Date temp;
temp = *this;
while (days--)
{
--temp;
}
return temp;
}
bool Date::operator==(const Date& d)
{
if ((this->_year) == (d._year))
{
if ((this->_month) == (d._month))
{
if ((this->_day) == (d._day))
{
return 1;
}
else
{
return 0;
}
}
else
{
return 0;
}
}
else
{
return 0;
}
}
int Date::operator-(const Date& d)
{
int count = 0;
Date temp = *this;
if (temp == d)
{
return 0;
}
else
{
while (temp != d)
{
if (temp > d)
{
--temp;
count++;
}
else
{
++temp;
count++;
}
}
return count;
}
}
bool Date::operator!=(const Date& d)
{
if (*this == d)
{
return 0;
}
else
{
return 1;
}
}
bool Date::operator>(const Date& d)
{
if ((this->_year) > (d._year))
{
return 1;
}
else if ((this->_year) == (d._year))
{
if ((this->_month) > (d._month))
{
return 1;
}
else if (this->_month == d._month)
{
if (this->_day > d._day)
{
return 1;
}
else
{
return 0;
}
}
else
{
return 0;
}
}
else
{
return 0;
}
}
bool Date::operator<(const Date& d)
{
if (*this > d)
{
return 0;
}
else if (*this == d)
{
return 0;
}
else
{
return 1;
}
}
int main()
{
Date ret(2017, 9, 21);
Date temp(2016,8, 21);
int x;
ret++;
temp = ret + 100;
x = ret - temp;
printf("%d\n", x);
system("pause");
return 0;
}
欢迎各位,批评指正!