主题:哥哥姐姐们,有点长,但是请您耐心看完指导下好么
#include <iostream>
using namespace std;
class Date
{
int year;
int month;
int day;
public:
int y(){return year;}
int m(){return month;}
int d(){return day;}
Date (int y,int m,int d){month=m;day=d;year=y;}
void set(int y,int m,int d){month=m;day=d;year=y;}
int isyun();
int operator-(Date &);
};
int Date::isyun()
{
if(year%4==0&&year%100!=0||year%400==0)
return 1;
else return 0;
}
int Date:: operator-(Date &d )
{
int t1,t2,sum1,sum2;
switch(d.m())
{
case 1:t2=0;break;
case 2:t2=31;break;
case 3:t2=59;break;
case 4:t2=90;break;
case 5:t2=120;break;
case 6:t2=151;break;
case 7:t2=181;break;
case 8:t2=212;break;
case 9:t2=243;break;
case 10:t2=273;break;
case 11:t2=304;break;
case 12:t2=334;break;
default:cout<<"data error"<<endl;break;
}
sum2=t2+d.d();//d2占当年的第几天
switch(month)
{
case 1:t2=0;break;
case 2:t2=31;break;
case 3:t2=59;break;
case 4:t2=90;break;
case 5:t2=120;break;
case 6:t2=151;break;
case 7:t2=181;break;
case 8:t2=212;break;
case 9:t2=243;break;
case 10:t2=273;break;
case 11:t2=304;break;
case 12:t2=334;break;
default:cout<<"data error"<<endl;break;
}
sum1=t1+day;//d1占当年的第几天
if(year==d.y())//若为同一年
{
if(d.isyun()==1)//判断当年是否为闰年
{
if(d.month>=3&&month<3)
return (sum2+1-sum1);
if((d.month>=3&&month>=3)||(d.month<3&&month<3))
return (sum2-sum1);
}
else
return (sum2-sum1) ;
}
else//不为同一年
{
int c=year-1-1980;//以1980年为基准
int zheng=c%4;
int yu=c/4;
if(isyun()==1)
sum1++;
int sum3=366*zheng+365*yu+sum1;
c=d.y()-1-1980;
zheng=c%4;
yu=c/4;
if(d.isyun()==1)
sum2++;
int sum4=366*zheng+365*yu+sum2;
int cha=sum4-sum3;
if(cha>0)
return cha ;
else
return (0-cha) ;
}
}
int main()
{
Date d1(2000,2,1),d2(2000,2,2);
int cha;
cha=d1-d2;
cout<<"相差天数是:"<<endl;
cout<<cha<<endl;
return 0;
}
可以运行,但是我的结果出来的就是什么8几几几几几几几的一串数,为什么呢,该怎么改能,大哥大姐帮帮小妹吧~~~~
using namespace std;
class Date
{
int year;
int month;
int day;
public:
int y(){return year;}
int m(){return month;}
int d(){return day;}
Date (int y,int m,int d){month=m;day=d;year=y;}
void set(int y,int m,int d){month=m;day=d;year=y;}
int isyun();
int operator-(Date &);
};
int Date::isyun()
{
if(year%4==0&&year%100!=0||year%400==0)
return 1;
else return 0;
}
int Date:: operator-(Date &d )
{
int t1,t2,sum1,sum2;
switch(d.m())
{
case 1:t2=0;break;
case 2:t2=31;break;
case 3:t2=59;break;
case 4:t2=90;break;
case 5:t2=120;break;
case 6:t2=151;break;
case 7:t2=181;break;
case 8:t2=212;break;
case 9:t2=243;break;
case 10:t2=273;break;
case 11:t2=304;break;
case 12:t2=334;break;
default:cout<<"data error"<<endl;break;
}
sum2=t2+d.d();//d2占当年的第几天
switch(month)
{
case 1:t2=0;break;
case 2:t2=31;break;
case 3:t2=59;break;
case 4:t2=90;break;
case 5:t2=120;break;
case 6:t2=151;break;
case 7:t2=181;break;
case 8:t2=212;break;
case 9:t2=243;break;
case 10:t2=273;break;
case 11:t2=304;break;
case 12:t2=334;break;
default:cout<<"data error"<<endl;break;
}
sum1=t1+day;//d1占当年的第几天
if(year==d.y())//若为同一年
{
if(d.isyun()==1)//判断当年是否为闰年
{
if(d.month>=3&&month<3)
return (sum2+1-sum1);
if((d.month>=3&&month>=3)||(d.month<3&&month<3))
return (sum2-sum1);
}
else
return (sum2-sum1) ;
}
else//不为同一年
{
int c=year-1-1980;//以1980年为基准
int zheng=c%4;
int yu=c/4;
if(isyun()==1)
sum1++;
int sum3=366*zheng+365*yu+sum1;
c=d.y()-1-1980;
zheng=c%4;
yu=c/4;
if(d.isyun()==1)
sum2++;
int sum4=366*zheng+365*yu+sum2;
int cha=sum4-sum3;
if(cha>0)
return cha ;
else
return (0-cha) ;
}
}
int main()
{
Date d1(2000,2,1),d2(2000,2,2);
int cha;
cha=d1-d2;
cout<<"相差天数是:"<<endl;
cout<<cha<<endl;
return 0;
}
可以运行,但是我的结果出来的就是什么8几几几几几几几的一串数,为什么呢,该怎么改能,大哥大姐帮帮小妹吧~~~~