四则运算三
一、要求:能输入答案并判断对错
二、设计思路
此次试验是在上次的基础上添加了一个模块,使用户能够输入答案案,程序校验其正确性并反馈给用户
三、源代码
#include<iostream.h>
#include<stdlib.h>
int a[200],b[200],k[200];
int answer[200]={0};
int da[200]={0};
int i,j,c;
void Input(int tm)
{
int f;
int zq=0;
int cw=0;
cout<<"请输入答案"<<endl;
for(f=0;f<tm;f++)
{
cout<<f+1<<":";
cin>>da[f];
}
for(f=0;f<tm;f++)
{
if(da[f]==answer[f])
{
zq++;
}
else
{
cout<<"第"<<f+1<<"道错了"<<endl;
cw++;
}
}
cout<<"总计"<<zq<<"道答对"<<cw<<"道答错"<<endl;
}
void shuchu(int dy) //输出题目
{
if(dy==0)
{
if(k[i]==0)
{
cout<<a[i]<<'+'<<b[i]<<'='<<endl;
answer[i]=a[i]+b[i];
}
if(k[i]==1)
{
cout<<a[i]<<'-'<<b[i]<<'='<<endl;
answer[i]=a[i]-b[i];
}
if(k[i]==2)
{
cout<<a[i]<<'*'<<b[i]<<'='<<endl;
answer[i]=a[i]*b[i];
}
if(k[i]==3)
{
cout<<a[i]<<'/'<<b[i]<<'='<<endl;
answer[i]=a[i]/b[i];
}
}
else
{
if(k[i]==0)
{
cout<<a[i]<<'+'<<b[i]<<'='<<'\t';
answer[i]=a[i]+b[i];
}
if(k[i]==1)
{
cout<<a[i]<<'-'<<b[i]<<'='<<'\t';
answer[i]=a[i]-b[i];
}
if(k[i]==2)
{
cout<<a[i]<<'*'<<b[i]<<'='<<'\t';
answer[i]=a[i]*b[i];
}
if(k[i]==3)
{
cout<<a[i]<<'/'<<b[i]<<'='<<'\t';
answer[i]=a[i]/b[i];
}
if((i+1)%dy==0)
cout<<endl;
}
}
void yaoqiu() //对出题的要求询问
{
int cc,fw,zf,ys,ts,dy;
cout<<"请选择是否有乘除法:(0为是,1为否)"<<endl;
cin>>cc;
if(cc!=0&&cc!=1)
{
cout<<"输入错误,请重新输入!"<<endl;
yaoqiu();
}
cout<<"请输入取值范围:(输入范围0~x的最大数x)"<<endl;
cin>>fw;
if(fw<=0)
{
cout<<"输入错误,请输入正整数"<<endl;
yaoqiu();
}
cout<<"请选择加减有无负数:(0为有,1为无)"<<endl;
cin>>zf;
if(zf!=0&&zf!=1)
{
cout<<"输入错误,请重新输入!"<<endl;
yaoqiu();
}
if(cc==0)
{
cout<<"请选择除法有无余数:(0为有,1为无)"<<endl;
cin>>ys;
if(ys!=0&&ys!=1)
{
cout<<"输入错误,请重新输入!"<<endl;
yaoqiu();
}
}
cout<<"你想要一次出几道题?"<<endl;
cin>>ts;
if(ts<=0)
{
cout<<"输入错误,请重新输入!"<<endl;
yaoqiu();
}
cout<<"请选择打印方式:(1为逐行打印,输入其他数字(2,3,4,5...)则为一行有2,3,4,5...道题)"<<endl;
cin>>dy;
if(dy<=0)
{
cout<<"输入错误,请重新输入!"<<endl;
yaoqiu();
}
for(i=0;i<ts;i++)
{
for(j=0;j<i;j++)// 排重
{
if(a[j]==a[i])
a[i]++;
}
a[i]=rand()%fw;
b[i]=rand()%fw;
if(cc==0)
{
k[i]=rand()%4;
}
else
{
k[i]=rand()%2;
}
if(zf==1)
{
if(b[i]>a[i])
{
c=b[i];
b[i]=a[i];
a[i]=c;
}
}
if(ys==0)
{
shuchu(dy);
}
if(ys!=0)
{
if(k[i]==3&&b[i]==0) //除数不能为0
{
b[i]++;
}
while(a[i]%b[i]!=0)
b[i]++;
shuchu(dy);
}
}
Input(ts);
}
void main()
{
yaoqiu();
}
四、结果截图
五、总结
这次,我在上次程序的基础上添加了一个小模块就解决了这次的实验,让我体会到了分块解决的便利。
六、图表