定制题目数量这个功能测试:

(1)输入题目数为负数时:

四则运算出题器(C++)-BUG修复-LMLPHP

可正确处理;

(2)输入题目数量为0时:

四则运算出题器(C++)-BUG修复-LMLPHP

可正确处理;

(3)输入题目数量为小数时:

四则运算出题器(C++)-BUG修复-LMLPHP

程序运行出错;

错误分析:

因为代码中题目数量的变量Ques1定义为int类型,无法处理小数。

解决方法:

将Ques1定义为double类型,然后使用floor()函数,将Ques1取整。

输入10.1:

四则运算出题器(C++)-BUG修复-LMLPHP

可以正常处理。

修改后完整代码:

 #include<iostream.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
int main()
{
double Ques1=;
int Ques01;
int Ques2=;
int Ques3=;
char Ques4='y';
int Ques5=;
char Ques6='n';
char Ques7='n';
Again:
cout<<"请输入题目数量:";
cin>>Ques1;
Ques01=floor(Ques1);
cout<<"请输入每行打印题目数(1-5):";
cin>>Ques2;
cout<<"请输入算式中数值的最大值:";
cin>>Ques3;
while()
{
cout<<"运算中需要乘除法吗?y:需要;n:不需要";
cin>>Ques4;
cout<<"减法运算需要有负数吗?y:需要;n:不需要";
cin>>Ques7;
cout<<"行间距(正整数):";
cin>>Ques5;
srand(time(NULL));
if(Ques4=='y')
{
while()
{
if(Ques1<)
{
cout<<"输入有误,请重新输入题目数量:";
cin>>Ques1;
Ques01=floor(Ques1);
}
else
{
for(int j=;j<Ques01;j++)
{
if(j!=&&j%Ques2==)
{
for(int i=;i<Ques5;i++)
{
cout<<endl;
}
}
int num1=rand()%Ques3;
int num2=rand()%Ques3;
int sign=rand()%;
switch(sign)
{
case :
cout<<j+<<":"<<" "<<num1<<"+"<<num2<<"="<<"\t";
break;
case :
if(Ques7=='y')
{
cout<<j+<<":"<<" "<<num1<<"-"<<num2<<"="<<"\t";
}
else
{
if(num1>num2)
{
cout<<j+<<":"<<" "<<num1<<"-"<<num2<<"="<<"\t";
}
else
{
cout<<j+<<":"<<" "<<num2<<"-"<<num1<<"="<<"\t";
}
}
break;
case :
cout<<j+<<":"<<" "<<num1<<"*"<<num2<<"="<<"\t";
break;
case :
if(num2!=)
{
cout<<j+<<":"<<" "<<num1<<"/"<<num2<<"="<<"\t";
}
else
{
j--;
}
break;
}
}
break;
}
}
break;
}
if(Ques4=='n')
{
while()
{
if(Ques01<)
{
cout<<"输入有误,请重新输入题目数量:";
cin>>Ques1;
Ques01=floor(Ques1);
}
else
{
for(int j=;j<Ques01;j++)
{
if(j!=&&j%Ques2==)
{
for(int i=;i<=Ques5;i++)
{
cout<<endl;
}
}
int num1=rand()%Ques3;
int num2=rand()%Ques3;
int sign=rand()%;
switch(sign)
{
case :
cout<<j+<<":"<<" "<<num1<<"+"<<num2<<"="<<"\t";
break;
case :
if(Ques7=='y')
{
cout<<j+<<":"<<" "<<num1<<"-"<<num2<<"="<<"\t";
break;
}
else
{
if(num1>num2)
{
cout<<j+<<":"<<" "<<num1<<"-"<<num2<<"="<<"\t";
break;
}
else
{
cout<<j+<<":"<<" "<<num2<<"-"<<num1<<"="<<"\t";
break;
}
}
break;
}
}
}
break;
}
break;
} else
{
cout<<"输入有误,请按要求输入!"<<endl;
}
}
cout<<endl;
while()
{
cout<<"还需要继续出题还是退出?(y:继续出题;n:退出)";
cin>>Ques6;
if(Ques6=='y')
{
goto Again;
}
if(Ques6=='n')
{
goto Exit;
}
else
{
cout<<"输入有误,请重新输入:";
}
}
Exit:
return ;
}
04-18 05:16