不知道为什么间隔日期有的时候算不对,像1987 3 4 和1998 2 8 就不对,是哪里错了吗
/*
实现日期的下一天
两个日期 相减
日期+ 天数 得到的日期
具体要求:实现类date,其中
构造函数包括year,month,day三个参数
重载两个date类之间的-,以及date类的+=,-=
使用int year(),int month(),int day()返回三个参数*/
#include <iostream>
using namespace std;
class date
{
private:
int y;
int m;
int d;
public:
date(){};
date(int yy,int mm,int dd);
date(const date &t):y(t.y),m(t.m),d(t.d){};
~date(){};
//输出 year,month,day
int year()const{return y;}
int month()const{return m;}
int day()const{return d;}
//判断是否闰年
bool leapyear(int y) const;
//得到某个月天数
int daysOfMonth(int y,int m)const;
//改变日期
date changeDays(const int days)const;
//重载运算符
date& operator++(); //前置++
date operator++(int); //后置++
//日期加上days天数
friend date operator+(const date &t, const int days);
friend date operator+(const int days, const date &t);
date& operator +=(int days);
//日期减去days天数
friend date operator- (const date &t, const int days);
friend int operator-(const date &t1, const date &t2);
date& operator -=(int days);
//重载<,==
friend bool operator<(const date &t1,const date &t2);
friend bool operator ==(const date &d1,const date &d2);
//计算间隔天数
int distance(const date &d)const;
};
//构造函数
date::date(int yy, int mm, int dd):y(yy),m(mm),d(dd)
{
if(yy<=0 || mm<=0 || mm>12 || dd<=0||dd>daysOfMonth(y,m))
{
cout<<"Error invalid date: ";
cout<<endl;
exit(-1);
}
}
//闰年
bool date::leapyear(int yy) const
{
return (yy%4 == 0 && yy % 100 != 0 || yy % 400 == 0);
}
//每个月天数
int date::daysOfMonth(int yy, int mm)const
{
int days = 0;
switch(mm)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
days = 28+leapyear(yy);
break;
}
return days;
}
//改变日期
date date::changeDays(const int days)const
{
int yearTemp = y;
int monthTemp = m;
int dayTemp = d;
if(days>0){
dayTemp += days;
while(dayTemp>daysOfMonth(yearTemp,monthTemp)){
dayTemp -= daysOfMonth(yearTemp,monthTemp);
monthTemp++;
if(monthTemp>12){
yearTemp++;
monthTemp = 1;
}
}
}else{ //days为负数
dayTemp += days;
while(dayTemp<1){
monthTemp--;
if(monthTemp<1){
yearTemp--;
monthTemp = 12;
}
dayTemp += daysOfMonth(yearTemp,monthTemp);
}
}
return date(yearTemp,monthTemp,dayTemp);
}
//前置
date & date::operator++()
{
*this = this->changeDays(1);
return *this;
}
date date::operator++(int) //后置++
{
date dTemp(*this);
++(*this);
return dTemp;
}
//日期加上days个天数
date operator +(const date &t,const int days)
{
if(days==0){ //如果天数为0,返回当个月
return t;
}
else
return t.changeDays(days);
}
//日期加上days个天数的重载
date operator +(const int days,const date &t)
{
if(days==0){ //如果天数为0,返回当个月
return t;
}
else
return t.changeDays(days);
}
//日期自身加上days个天数
date& date::operator +=(int days)
{
if(days==0)
return *this;
else{
*this = this->changeDays(days);
return *this;
}
}
//日期减去days个天数
date operator -(const date &t,const int days)
{
if(days==0){ //如果天数为0,返回当个月
return t;
}
else
return t.changeDays(-days);
}
//两个日期相减,前一个日期必须大于后一个日期
int operator -(const date &t1,const date &t2)
{
if(t1<t2){
cout<<"被减数日期必须大于减数日期!"<<endl;
exit(-1);
}
else if(t1==t2)
return 0;
else
return t1.distance(t2);
}
//日期自身减去days个天数
date& date::operator -=(int days)
{
if(days==0)
return *this;
else{
*this = this->changeDays(-days);
return *this;
}
}
//重载小于号<
bool operator <(const date &t1,const date &t2)
{
return t1.distance(t2)<0 ? true:false;
}
//重载==
bool operator ==(const date &d1,const date &d2)
{
return d1.distance(d2)==0 ? true:false;
}
//计算间隔天数
int date::distance(const date &t) const
{
//非闰年中当月前几个月的总天数
const int DAYS_OF_MONTH[] =
{0,31,59,90,120,151,181,212,243,273,304,334,365};
int years = y-t.y;
int months = DAYS_OF_MONTH[m] - DAYS_OF_MONTH[t.m];
int days = d-t.d;
//4年一闰,100的倍数免润,400的倍数再闰
int totalDays = years*365+years/4-years/100+years/400+months+days;
return totalDays;
}
//StudybarCommentBegin
int main(void) {
using std::cin;
using std::cout;
using std::endl;
date D1,D2;
int year;
int month;
int day;
cin>>year>>month>>day; //输入第一个日期
D1=date(year,month,day);
cin>>year>>month>>day; //输入第二个日期
D2=date(year,month,day);
cout<<D2-D1<<endl;
cin>>day; //输入要减去的天数
D1+=day;
D2-=day;
cout<<D1.year()<<" "<<D1.month()<<" "<<D1.day()<<endl;
cout<<D2.year()<<" "<<D2.month()<<" "<<D2.day()<<endl;
return 0;
}
//StudybarCommentEnd