求教!提交后总显示wrong answer。。。不知错在何处

qq_36533748 2016-10-27 06:34:35
#include <iostream>
using namespace std;

int leapyear(int year)//判断闰年
{
if(year%400==0||(year%100!=0&&year%4==0)) return 1;
else return 0;
}

int judgemonth(int month)//判断大小月
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) return 1;
else if(month==4||month==6||month==9||month==11) return 0;
else if(month==2) return 2;
}

int countday(int year,int month,int day)//计算该日为该年中的第几天
{
int count_day=0;
for(int i=1;i<month;++i)
{
if(judgemonth(i)==1) count_day=count_day+31;
else if(i==2&&leapyear(year)==1) count_day=count_day+29;
else if(i==2&&leapyear(year)==0) count_day=count_day+28;
else if(judgemonth(i)==0) count_day=count_day+30;
}
return count_day+day;
}

class CDate
{
private:
int nYear,nMonth,nDate;
public:
CDate(int y,int m,int d)
{
nYear=y;
nMonth=m;
nDate=d;
}

CDate(const CDate&x)
{
nYear=x.nYear;
nMonth=x.nMonth;
nDate=x.nDate;
}

void setnYear(int x)
{
nYear=x;
}
void setnMonth(int x)
{
if(x>=1&&x<=12) nMonth=x;
else cout<<"error"<<endl;
}
void setnDay(int x)
{
if((leapyear(nYear)==1&&nMonth==2&&x<29)||(leapyear(nYear)==0&&nMonth==2&&x<28))
nDate=x;
else if((judgemonth(nMonth)==1&&x<=31)||(judgemonth(nMonth)==0&&x<=30))
nDate=x;
else cout<<"错误"<<endl;
}

int getnYear(void)
{
return nYear;
}
int getnMonth(void)
{
return nMonth;
}
int getnDate(void)
{
return nDate;
}

int operator-(const CDate&x)
{
int D_value_day;

int D_value_year=nYear-x.nYear;

if(D_value_year==0)
{
D_value_day=countday(nYear,nMonth,nDate)-countday(x.nYear,x.nMonth,x.nDate);
}

else if(D_value_year>0)
{
D_value_day=countday(nYear,nMonth,nDate)+365-countday(x.nYear,x.nMonth,x.nDate);
if(leapyear(x.nYear)==1) D_value_day=D_value_day+1;

for(int i=x.nYear+1;i<nYear;++i)
{
if(leapyear(i)==1) D_value_day=D_value_day+366;
else D_value_day=D_value_day+365;
}
}

else if(D_value_year<0)
{
D_value_day=365-countday(nYear,nMonth,nDate)+countday(x.nYear,x.nMonth,x.nDate);
if(leapyear(nYear)==1) D_value_day=D_value_day+1;
for(int i=nYear+1;i<x.nYear;++i)
{
if(leapyear(i)==1) D_value_day=D_value_day+366;
else D_value_day=D_value_day+365;
}
}

if(D_value_day>=0)
{
return D_value_day;
}
else if(D_value_day<0)
{
return -D_value_day;
}

}

CDate operator++(void)
{
if(nMonth==12&&nDate==31)
{
nYear=nYear+1;
nMonth=1;
nDate=1;
}

else
{
if(judgemonth(nMonth)==1&&nDate==31)
{
nMonth=nMonth+1;
nDate=1;
}
else if(judgemonth(nMonth)==0&&nDate==30)
{
nMonth=nMonth+1;
nDate=1;
}
else if(judgemonth(nMonth)==2)
{
if(leapyear(nYear)==1&&nDate==28) nDate==29;
else if((leapyear(nYear)==0&&nDate==28)||(leapyear(nYear)==1&&nDate==29))
{
nMonth=nMonth+1;
nDate=1;
}
}
else nDate=nDate+1;
}
}




CDate operator--(void)
{
if(countday(nYear,nMonth,nDate)==1)
{
nYear=nYear-1;
nMonth=12;
nDate=31;
}
else
{
if(nMonth==1) nDate=nDate-1;
else if(judgemonth(nMonth-1)==1&&nDate==1)
{
nMonth=nMonth-1;
nDate=31;
}
else if(judgemonth(nMonth-1)==0&&nDate==1)
{
nMonth=nMonth-1;
nDate=30;
}
else if(judgemonth(nMonth-1)==2&&nDate==1)
{
nMonth=2;
if(leapyear(nYear)==1) nDate=29;
else if(leapyear(nYear)==0) nDate=28;
}
else nDate=nDate-1;
}
}

};

int main()
{


int number;

int y,m,d;
while(cin>>number>>y>>m>>d)
{
CDate day1(y,m,d);

if(number==2)
{
int y2,m2,d2;
cin>>y2>>m2>>d2;
CDate day2(y2,m2,d2);

cout<<day1.operator-(day2)<<endl;
}

else if(number==1)
{
int k;
cin>>k;

if(k>=0)
{
for(int i=0;i<k;++i)
{
day1.operator++();
}
}

else if(k<0)
{
for(int i=0;i<-k;++i)
{
day1.operator--();
}
}

cout<<day1.getnYear()<<" "
<<day1.getnMonth()<<" "
<<day1.getnDate()<<" "
<<endl;

}

}

return 0;
}
...全文
812 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
GoodCoder666 2020-03-13
  • 打赏
  • 举报
回复

if(leapyear(nYear)==1&&nDate==28) nDate==29;
中应该是=29而不是==
小灸舞 2016-10-28
  • 打赏
  • 举报
回复
碰到WA时:这个要做好测试,测试点主要覆盖:边界值、特殊值,按照白盒测试的方法跑每一个分支,一高代码覆盖率。
paschen 版主 2016-10-27
  • 打赏
  • 举报
回复
if(leapyear(nYear)==1&&nDate==28) nDate==29; 改成 if(leapyear(nYear)==1&&nDate==28) nDate=29;

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧