本帖最后由 mymoon 于 2021-4-19 08:06 编辑
include <iostream>
include<ctype.h>
using namespace std;
int main()
{
cout<<"闪退代表输入字符非数字或是浮点数"<<endl;
cout<<"请输入一个不是字符q的字符进入循环输入,输入q退出循环"<<endl;
while(getchar()!='q'){
int year,month,day;
cout<<"请输入 3 个整数:"<<endl;
cin>>year>>month>>day;
if(isalpha(year)==2||isalpha(month)==2||isalpha(day)==2){ //判断输入字符是字母
//不知道怎么处理
}else if (isdigit(year)==0||isdigit(month)==0||isdigit(day)==0) //判断输入字符是数字
{
if(month<1||month>12||day<1||day>31||year<1){ //超出取值范围处理
if(month<1||month>12) cout<<"不满足月份条件取值范围"<<endl;
if(day<1||day>31) cout<<"不满足日期取值条件范围"<<endl;
if(year<1) cout<<"不满足年份取值条件范围"<<endl;
}else{
if((year%4==0 && year%100!=0)||(year%400==0)){ //闰年的情况
if(month==2&&day==29)
cout<<year<<"-"<<month+1<<"-"<<01<<endl;
else if((month==1||month==3||month==5||month==7||month==8||month==10)&&(day==31))
cout<<year<<"-"<<month+1<<"-"<<01<<endl;
else if(month==12&&day==31)
cout<<year+1<<"-"<<01<<"-"<<01<<endl;
else
cout<<year<<"-"<<month<<"-"<<day+1<<endl;
}
else{ //非闰年
if(month==2&&day==28)
cout<<year<<"-"<<month+1<<"-"<<01<<endl;
else if((month==1||month==3||month==5||month==7||month==8||month==10)&&(day==31))
cout<<year<<"-"<<month+1<<"-"<<01<<endl;
else if(month==12&&day==31)
cout<<year+1<<"-"<<01<<"-"<<01<<endl;
else
cout<<year<<"-"<<month<<"-"<<day+1<<endl;
}
}
}
else{//不知道怎么处理
}
}
system("pause");
return 1;
}
我的这个代码输入一个年月日,输出第二天的年月日。if套if,有点混乱,见谅。我把代码搞这么乱是因为当输入的年月日不是整数时,输出提示错误。我阴差阳错自己也不知道原因的达成了只要输入字母或标点符号等非数字,以及浮点数时,会闪退推出。其他情况正常,问问大家,这是怎么回事? |