用C++实现日期类,其中包含 日期加上天数结果为一个新日期,日期减去一个天数结果为新日期,两个日期相减结果为相差的天数等等函数的实现。
#pragma once
#include
#include
using namespace std;
class Date
{
private:
int _year;
int _month;
int _day;
public:
Date(int year=1900, int month=1, int day=1)
:_year(year), _month(month), _day(day)
{
assert(IsInvalid());
}
void Display()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
bool IsInvalid()
{
if (_year >= 1900 && _month > 0 && _month<13 && _day>0 && _day <= GetMonthDays(_year, _month))
{
return true;
}
return false;
}
int GetMonthDays(int year, int month) //计算每个月的天数
{
int monthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (month != 2)
{
return monthDays[month];
}
else
{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
return 29;
}
else
{
return 28;
}
}
}
Date operator+(int day) //计算一个日期加上天数结果为一个日期
{
if (day < 0)
{
return *this - (-day);
}
Date tmp(*this);
tmp._day += day;
while (tmp.IsInvalid() == false)
{
tmp._day -= GetMonthDays(tmp._year, tmp._month);
if (tmp._month == 12)
{
tmp._year++;
tmp._month = 1;
}
else
{
tmp._month++;
}
}
return tmp;
}
Date operator-(int day) //计算日期减去一个天数结果为一个日期
{
if (day < 0)
{
return *this + (-day);
}
Date tmp(*this);
tmp._day -= day;
while (tmp.IsInvalid() == false)
{
if (tmp._month == 1)
{
tmp._year--;
tmp._month = 12;
}
else
{
tmp._month--;
}
int day = GetMonthDays(tmp._year, tmp._month);
tmp._day += day;
}
return tmp;
}
inline Date& operator+=(int day)
{
*this = *this + 100;
return *this;
}
inline Date& operator-=(int day)
{
*this = *this - 100;
return *this;
}
inline Date& operator++() //前加加
{
*this += 1;
return *this;
}
inline Date operator++(int) //后加加
{
Date tmp(*this);
*this += 1;
return tmp;
}
inline Date& operator--() //前减减
{
*this -= 1;
return *this;
}
inline Date operator--(int) //后减减
{
Date tmp(*this);
*this -= 1;
return tmp;
}
bool operator >(const Date& d) //比较大小
{
if (_year > d._year || (_year == d._year && (_month > d._month || (_month == d._month&&_day > d._day))))
{
return true;
}
return false;
}
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 _year == d._year
&&_month == d._month
&&_day == d._day;
}
bool operator!=(const Date& d)
{
return !(*this == d);
}
int operator-(const Date& d) //计算两个日期之差为天数
{
int sum1 = 0;
int sum2 = 0;
int sum3 = 0;
int sum = 0;
int month = 0;
int year = 0;
for (month = 1; month < _month; month++)
{
sum1 += GetMonthDays(_year, month);
}
sum1 += _day;
for (month = 1; month < d._month; month++)
{
sum2 += GetMonthDays(d._year, month);
}
sum2 += d._day;
sum3 = abs(sum1 - sum2);
if (_year == d._year)
{
return sum3;
}
else if (_year > d._year)
{
for (year = d._year; year < _year; year++)
{
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
{
sum += 366;
}
else
{
sum += 365;
}
}
return abs(sum - sum3);
}
else if (_year < d._year)
{
for (year = _year; year < d._year; year++)
{
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
{
sum3 += 366;
}
else
{
sum3 += 365;
}
}
return abs(sum - sum3);
}
}
};
void TestDate()
{
Date d1(2017,3,9);
d1.Display();
Date d2(2008,4,20);
d2.Display();
int d3 = d1 - d2;
cout <<"两个日期相差的天数为:" <
<< endl;
/*Date d2 = d1 +100;
d2.Display();
Date d3 = d2 - 100;
d3.Display();*/
}
test.c
#include "Date.h"
int main()
{
TestDate();
return 0;
}