#include<iostream>
using namespace std;
int runnian(int year)//判断该年是否是闰年
{
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
return 1;
else
return 0;
}
int riqi(int& month,int& year)//因为每个月的的天数不一样,所以用一个函数进行判断
{
int day = 0;
if (month <= 12)
{
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
day = 31;
else if (month == 2)
{
if (runnian(year) == 1)
day = 29;
else
day = 28;
}
else
day = 30;
}
else
{
year++;
day = 1;
month = 1;
}
return day;
}
class Date
{
public:
Date(int year = 2017, int month = 9, int day = 10)//构造函数 大括号上面的三个代码是用来初始化
: _year(year)
, _month(month)
, _day(day)
{}
Date& operator=(const Date& d)//重载=,因为在下文我们会用到将两个类变量进行等号赋值,而系统中的等号
{
this->_day = d._day;
this->_month = d._month;
this->_year = d._year;
return *this;
}
// 前置++
Date& operator++()
{
Date temp;
temp._day = this->_day;
temp._month = this->_month;
temp._year = this->_year;
this->_day = this->_day + 1;
this->_month = this->_month + 1;
this->_year = this->_year + 1;
return temp;
}
// 后置++
Date operator++(int)
{
this->_day = this->_day + 1;
this->_month = this->_month + 1;
this->_year = this->_year + 1;
return *this;
}
//前置--
Date& operator--()
{
Date temp;
temp._day = this->_day;
temp._month = this->_month;
temp._year = this->_year;
this->_day = this->_day - 1;
this->_month = this->_month - 1;
this->_year = this->_year - 1;
return temp;
}
//后置--
Date operator--(int)
{
this->_day = this->_day - 1;
this->_month = this->_month - 1;
this->_year = this->_year - 1;
return *this;
}
//计算days天之后的日期 重载+运算符
Date operator+(int days)
{
Date temp = *this;
int day = riqi(temp._month, temp._year);
for (int i = days; i > 0; i--)
{
if (temp._day ==day)
{
if (temp._month < 12)
++temp._month;
else
{
++temp._year;
temp._month = 1;
temp._day = 1;
}
day = riqi(temp._month, temp._year);
temp._day = 1;
}
else
temp._day++;
}
return temp;
}
// 计算days天之前的日期
Date operator-(int days)
{
Date temp = *this;
int day = riqi(temp._month, temp._year);
for (int i = days; i > 0; i--)
{
if (temp._day == 1)
{
if (temp._month > 1)
--temp._month;
else
{
--temp._year;
temp._month = 12;
temp._day = 31;
}
day = riqi(temp._month, temp._year);
temp._day = day;
}
else
temp._day--;
}
return temp;
}
// 两个日期之间的距离
int operator-(const Date& d)
{
int count = 0;
Date temp = *this;
//int day = riqi(temp._month, temp._year);
while (1)
{
if (temp != d)
{
if (temp._day > 1)
temp._day--;
else
{
if (temp._month > 1)
{
--temp._month;
temp._day = riqi(temp._month, temp._year);
}
else
{
--temp._year;
temp._month = 12;
temp._day = 31;
}
}
}
else
break;
count++;
}
return count;
}
bool operator==(const Date& d)
{
if (_year != d._year)
return 0;
else
{
if (_month != d._month)
return 0;
else
{
if (_day != d._day)
return 0;
else
return 1;
}
}
}
bool operator!=(const Date& d)
{
if (_year != d._year)
return 1;
else
{
if (_month != d._month)
return 1;
else
{
if (_day != d._day)
return 1;
else
return 0;
}
}
}
bool operator>(const Date& d)
{
if (_year > d._year)
return 1;
else if (_year < d._year)
return 0;
else
{
if (_month > d._month)
return 1;
else if (_month < d._month)
return 0;
else
{
if (_day > d._day)
return 1;
else if (_day < d._day)
return 0;
else
return 0;
}
}
}
bool operator<(const Date& d)
{
if (_year > d._year)
return 0;
else if (_year < d._year)
return 1;
else
{
if (_month > d._month)
return 0;
else if (_month < d._month)
return 1;
else
{
if (_day > d._day)
return 0;
else if (_day < d._day)
return 1;
else
return 0;
}
}
}
~Date()
{}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date s1(2017, 1, 10);
Date s2(2016,1, 10);
int z = s1 - s2;
cout << z << endl;
system("pause");
return 0;
}
注:上述代码,因为太过冗长所以如果if后面如果只跟一条语句,就会把大括号去掉,但不会影响代码的执行,只是看起来可能会稍有点困难,带来的不便,在此说一声抱歉。