理强 2015-05-19 03:28 采纳率: 40%
浏览 2000
已采纳

C++日期+1计算问题,看下代码是否正确

帮我看下是不是代码不对,29/2/2001日期+1就成30/2/2001了,而28/2/2001日期+1是1/3/2001
#include
using namespace std;
class Date
{
public:
Date()
{year=0;
month=0;
day=0;
}
void show_date();
void add_date();
void set_date();
private:
int year;
int month;
int day;
};
void Date::set_date()
{cin>>day;
cin>>month;
cin>>year;
}
void Date::show_date()
{cout<<day<<"/"<<month<<"/"<<year<<endl;}
void Date::add_date()
{if (year/4==0)
{if (month==1||month==3||month==5||month==7||month==8||month==10)
{if (day==31)
{day=1;month++;}
else day++;
}
else if (month==4||month==6||month==9||month==11)
{if (day==30)
{day=1;month++;}
else day++;
}
else if (month==12)
{ if(day==31)
{day=1;month=1;year++;}
else day++;
}
else {if (day==29)
{day=1;month++;}
else day++;}
}
else
{if (month==1||month==3||month==5||month==7||month==8||month==10)
{if (day==31)
{day=1;month++;}
else day++;
}
else if (month==4||month==6||month==9||month==11)
{if (day==30)
{day=1;month++;}
else day++;
}
else if (month==12)
{ if(day==31)
{day=1;month=1;year++;}
else day++;
}
else {if (day==28)
{day=1;month++;}
else day++;}
}
}
int main()
{cout<<"please enter the date:day/month/year"<<endl;
Date t1;
t1.set_date();
cout<<"the original date:"<<endl;
t1.show_date();
t1.add_date();
cout<<"the alerant date:"<<endl;
t1.show_date();
Date t2;
t2.set_date();
cout<<"the original date:"<<endl;
t2.show_date();
t2.add_date();
cout<<"the alerant date:"<<endl;
t2.show_date();
Date t3;
t3.set_date();
cout<<"the original date:"<<endl;
t3.show_date();
t3.add_date();
cout<<"the alerant date:"<<endl;
t3.show_date();
Date t4;
t4.set_date();
cout<<"the original date:"<<endl;
t4.show_date();
t4.add_date();
cout<<"the alerant date:"<<endl;
t4.show_date();

  • 写回答

4条回答 默认 最新

  • Tiger_Zhao 2015-05-19 03:49
    关注

    应该是取模而不是整除啊。

        if (year%4==0) 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路