# 代码实现:

#include<iostream>
#include<Windows.h>
#include<assert.h>
using namespace std;

class Date
{

public:

Date(int year = 1997,int month = 1,int day = 1)
:years(year)
, months(month)
, days(day)
{
assert(IScorrect());
}

Date& operator=(const Date& d)
{
if (this != &d)
{
years = d.years;
months = d.months;
days = d.days;
}
return *this;
}

Date& operator + (int day)
{
while (day > 365)
{
if (ISleapyear() && day > 366)
{
years++;
day = day - 366;
}
else
{
years++;
day = day - 365;
}
}
while (day >= Getmonthsday())
{
//注意这里的次序问题,一定先减 再加 最后再判断. 如果顺序错了会出BUG的.
day = day - Getmonthsday();
months++;
if (months > 12)
{
years++;
months = 1;
}
}

while (day > 0)
{
day = day - 1;
days++;
}
return *this;
}

Date& operator - (int day) //先减去一年，然后在使用加的重载,所以你只需要写一个无懈可击的加算法就够了.
{
while (day > 365)
{
if (ISleapyear() && day > 366)
{
day = day - 366;
years--;
}
else
{
day = day - 365;
years--;
}
}
if (ISleapyear())
{
day = 366 - day;
years--;
}
else
{
day = 365 - day;
years--;
}
operator+(day);
return *this;
}

{
if (days > Getmonthsday())
{
months++;
days = 1;
}
if (months > 12)
{
years++;
months = 1;
}
}

int operator - (Date D)
{
int count = 0;
if (*this > D)
{
while (*this != D)
{
D.operator+(1);
count++;
}
}
else
{
while (*this != D)
{
operator+(1);
count++;
}
}
return count;
}

bool ISleapyear()
{
if ((years % 4 == 0 && years % 100 != 0) || (years % 400 == 0))
{
return true;
}
return false;
}
int Getmonthsday()
{
int monthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (ISleapyear() && months == 2)
{
return 29;
}
return monthDays[months];
}

void print()
{
cout << "目前的时间为";
cout << years << "." << months << "." <<days<< endl;
}

bool IScorrect()
{
if (years > 0 && ((years % 4 == 0 && years % 100 != 0) || (years % 400 == 0)) && days < 367)//闰年
{
if (months >0 && months < 13)
{
if (days > 0 && days <= Getmonthsday())
{
return true;
}
}
}
else if (years >0 && days < 366) //非闰年
{
if (months >0 && months < 13)
{
if (days > 0 && days <= Getmonthsday())
{
return true;
}
}
}
return false;
}

Date operator += (int day)
{
*this = *this + 100;
return *this;
}
Date operator -= (int day)
{
return *this = *this - day;
}
inline Date& operator++()
{
*this += 1;
return *this;
}
inline Date operator++(int)
{
Date tmp(*this);
*this = *this + 1;
return tmp;
}

bool operator == (const Date& d)
{
return (years == d.years&& months == d.months&&days == d.days);
}

bool operator != (const Date& d)
{
return !(*this == d);
}

bool operator >(const Date& d)
{
if (years > d.years ||
(years == d.years&&months > d.months)
|| (years == d.years&&months == d.months && days > d.days))
{
return true;
}
return false;
}

bool operator < (const Date& d)
{
return !(*this > d);
}

bool operator >= (const Date& d)
{
return (*this == d) && (*this > d);
}

bool operator <= (const Date& d)
{
return (*this == d) && (*this < d);
}

private:
int years;
int months;
int days;
};

void Test()
{
Date d1(2012, 4, 5);
Date d2(2013, 4, 5);
d1.print();
/*d1 = d1 - 400;*/
d1.print();
cout << d1 - d2 << endl;
d1.print();
system("pause");
}

# 总结：

#### 【C++】日期类+日期万年历+日期计算器

2016-05-30 15:04:15

#### 【c++巩固练习】c++实现日期计算器、日期万年历（日期类）

2016-07-08 17:00:07

#### 计算日期差（C/C++实现）

2018-03-25 23:30:17

#### C++ 日期计算器

2017-07-02 07:07:35

#### 简单实现日期计算器的基本功能

2017-09-21 13:43:40

#### 计算器c++实现

2008-04-15 13:18:00

#### c/c++实现简单计算器

2016-04-04 22:22:29

#### 用C实现一个计算器（带小数点和括号以及运算优先级）

2017-06-13 11:01:21

#### C++实现计算器（四则混合运算）

2017-04-29 18:34:01

#### 小程序之计算器 【C++ STL栈实现】 + 【C 数组模拟栈实现】 【适用VC， DEV, codeblack】

2015-06-13 21:19:49