一、功能
- 计算出一个固定日期d在n天后/前的日期(n无限制,d不能比初始值小)
- 计算出d的前一天和后一天
- 计算出两个日期的相距天数
二、源代码
1、date.h
#ifndef _DATE__H_
#define _DATE__H_
#include "iostream"
using namespace std;
class Date {
public:
Date(int year = 1900, int month = 1, int day = 1)//构造函数
:_year(year)
, _month(month)
, _day(day)
{
if (_year < 1990 || _month<1 || _month>12 || _day<1 || _day>Getmonth(_year, _month)) //判断日期是否合法
{
cout << "日期非法" << endl;
return;
}
}
bool Leapyear(int year) //判断是否是闰年
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return true;
return false;
}
int Getmonth(int year, int month) //返回天数
{
int mon[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (Leapyear(year))
mon[2] = 29;
return mon[month];
}
Date(const Date& d) //拷贝构造
:_year(d._year)
, _month(d._month)
, _day(d._day)
{}
Date& operator=(const Date& d)
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
friend ostream& operator<<(ostream& _cout, const Date d)
{
_cout << d._year << "-" << d._month << "-" << d._day;
return _cout;
}
//计算
int operator-(const Date& d); //两个日期差
Date operator+(int days); //days天后日期
Date& operator+=(int days);
Date operator-(int days); //days天前日期
Date& operator-=(int days);
Date& operator++(); //++d
Date operator++(int); //d++
Date& operator--(); //--d
Date operator--(int); //d--
//比较
bool operator>(const Date& d);
bool operator>=(const Date& d);
bool operator<(const Date& d);
bool operator<=(const Date& d);
bool operator==(const Date& d);
bool operator!=(const Date& d);
~Date() //析构函数
{}
private:
int _year;
int _month;
int _day;
};
#endif
2、date.cpp
#include "date.h"
int Date::operator-(const Date& d)
{
Date tmp = *this;
Date cur = d;
int num = 0, flag = 0;
if (tmp > cur)//tmp较小cur较大
{
tmp = d;
cur = *this;
flag = 1;
}
while (cur > tmp)
{
tmp += 1;
num++;
}
if (flag)
num = -num;
return num;
}
Date Date::operator-(int days)
{
if (days < 0)
{
days = -days;
return *this + days;
}
Date tmp(*this);
if (days == 0)
return tmp;
tmp._day -= days;
while (tmp._day <= 0)
{
if (tmp._month == 1)
{
tmp._year--;
tmp._month = 12;
}
else
tmp._month--;
tmp._day += Getmonth(tmp._year, tmp._month);
}
return tmp;
}
Date& Date::operator-=(int days)
{
Date tmp(*this);
tmp = (*this) - days;
*this = tmp;
return *this;
}
Date Date::operator+(int days)
{
if (days < 0)
{
days = -days;
return *this - days;
}
Date tmp(*this);
if (days == 0)
return tmp;
tmp._day += days;
while (tmp._day>Getmonth(tmp._year, tmp._month))
{
if (tmp._month == 12)
{
tmp._year++;
tmp._month = 1;
}
else
tmp._month++;
tmp._day -= Getmonth(tmp._year, tmp._month);
}
return tmp;
}
Date& Date::operator+=(int days)
{
Date tmp(*this);
tmp = (*this) + days;
*this = tmp;
return *this;
}
Date& Date::operator++()
{
*this += 1;
return *this;
}
Date Date::operator++(int)
{
Date tmp(*this);
*this += 1;
return tmp;
}
Date& Date::operator--()
{
*this -= 1;
return *this;
}
Date Date::operator--(int)
{
Date tmp(*this);
*this -= 1;
return tmp;
}
bool Date::operator>(const Date& d)
{
if (_year > d._year)
return true;
if (_year == d._year)
{
if (_month > d._month)
return true;
if (_month == d._month)
{
if (_day > d._day)
return true;
}
}
return false;
}
bool Date::operator>=(const Date& d)
{
return (*this > d) || (*this == d);
}
bool Date::operator<(const Date& d)
{
return !(*this >= d);
}
bool Date::operator<=(const Date& d)
{
return (*this < d) || (*this == d);
}
bool Date::operator==(const Date& d)
{
return (_year == d._year) && (_month == d._month) && (_day == d._day);
}
bool Date::operator!=(const Date& d)
{
return !(*this == d);
}
3、自己写一个测试(test.cpp),让代码跑起来并加以检验