日期计算器的功能应该包含:
日期+/-天数=返回日期
日期+/-日期=返回天数
在日期与天数的运算中,以日期+天数为例我们需要考虑如下几点:
1.二月份的天数受闰年影响
2.当日期day出现大于当月天数时,month++,当month>13时,day++并且month=1
3.当所加天数为负数时,可以转化成减法运算
在日期与日期的运算中,如果单纯用两个日期相加减则需要考虑比较年月日的大小,运算过程太过复杂,这时我们需要转换思路,将两个日期用max和min进行存放,增加一个计数器通过让min++不断逼近max值,此时计算器不断++,当min与max相等时,计数器的值就是两个日期所差的天数。
下面上代码
#include<iostream>
using namespace std;
class Date
{
public:
Date(int year=1900, int month=1, int day=1)
{
if (year < 1900 || month<1 || month>12 || day<1 || day>GetMonthday(year, month))
{
cout << "非法日期" << endl;
}
_year = year;
_month = month;
_day = day;
}
~Date()
{
//完成清理工作,但这里的年月日不需要释放
}
Date(Date &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
int GetMonthday(int year, int month)
{
static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day = days[month];
if (month==2&&(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
day = day + 1;
}
return day;
}
Date operator+(int day)//传值返回
{
Date ret(*this);
ret._day += day;
while (ret._day > GetMonthday(ret._year, ret._month))
{
ret._day -= GetMonthday(ret._year, ret._month);
ret._month++;
if (ret._month == 13)
{
ret._year++;
ret._month = 1;
}
}
return ret;
}
//Date &operator+=(int day)//引用返回
//{
// *this=*this + day;//在这里调用了一次加法运算符重载
// return *this;
//}
Date operator+=(int day)
{
if (day<0)
{
return *this -= -day;
}
_day += day;
if (_day > GetMonthday(_year, _month))
{
_day -= GetMonthday(_year, _month);
_month++;
if (_month == 13)
{
_year++;
_month = 1;
}
}
return *this;
}
Date& operator-=(int day)
{
if (day < 0)
{
return *this += day;
}
_day -= day;
while (_day < 1)
{
_month--;
if (_month < 1)
{
_year--;
_month = 12;
}
_day += GetMonthday(_year, _month);
}
return *this;
}
Date operator-(int day)
{
Date ret;
ret-= day;
return ret;
}
void Display()
{
cout <<_year <<"-"<< _month<<"-" << _day << endl;
}
void operator=(const Date&d)//不加&也可以,但需要额外开辟一块空间进行拷贝构造,代价较大,返回值为Date则支持连续赋值
{
if (this != &d)
{
_year = d._year;
_month =d._month;
_day = d._day;
}
}
Date& operator++()//++d ->d.operater++(&d)
{
*this += 1;
return *this;
}
Date operator++(int )//d++ -> d.operator++(&d,0)
{
Date ret(*this);
*this += 1;
return ret;
}
Date& operator--()
{
*this -= 1;
return *this;
}
Date operator--(int)
{
Date ret(*this);
*this -= 1;
return ret;
}
bool operator>(const Date&d)
{
if (_year > d._year)
{
return true;
}
else if (_year == d._year)
{
if (_month > d._month)
{
return true;
}
else if (_month == d._day)
{
if (_day > d._day)
{
return true;
}
}
}
return false;
}
bool operator==(const Date&d)
{
return (_year == d._year&&_month == d._month&&_day == d._day);
}
bool operator>=(const Date&d)
{
return *this > d || *this == d;
}
bool operator<(const Date&d)
{
return !(*this >= d);
}
bool operator<=(const Date&d)
{
return !(*this >d);
}
bool operator!=(const Date&d)
{
return !(*this == d);
}
int operator-( const Date&d)
{
int flag = 1;
Date*max = this;
Date*min = (Date*)&d;
if (*this < d)
{
swap(max, min);
flag = -1;
}
int day = 0;
while (*min < *max)
{
++(*min);
++day;
}
return day*flag;
}
private:
int _year;
int _month;
int _day;
};
void TestDate()
{
Date d1(2018, 4, 27);
//Date d2(d1);
Date d2(2018, 5, 8);
//d2 = d3;
//d2.Display();
//d1 += 100;
//d1.Display();
//d3 -= 20;
//d3.Display();
//(d1++).Display();
//(++d1).Display();
//(d1--).Display();
//(--d1).Display();
cout << d2 - d1 << endl;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include"1.h"
int main()
{
TestDate();
system("pause");
return 0;
}