问题:二柱子将做好的项目交给了老师,几天后又得到新的需求:
1、实现在线答题;
2、答题结束后,可以判断对错;
3、并将错题的结果保存起来
#include<iostream>
#include<stdarg.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
using namespace std;
int main()
{
cout<<endl;
cout<<"四则运算在线答题"<<endl;
cout<<endl;
cout<<endl;
Again:
double Q1=1;
int Q01;
int Q2=1;
int Q3=100;
char Q4=‘y‘;
int Q5=1;
char Q6=‘n‘;
char Q7=‘n‘;
double ans=0;
int right=0;
cout<<" 一.输入题目数量:";
cin>>Q1;
Q01=floor(Q1);
// cout<<"输入每行打印题目数(1-5):";
// cin>>Q2;
cout<<" 二.每行间距(正整数):";
cin>>Q5;
cout<<" 三.请输入算式中数值的最大值:";
cin>>Q3;
while(1)
{
cout<<" 四.运算中需要乘除法吗?需要:y;不需要:n";
cin>>Q4;
cout<<" 五.减法中需要有负数吗?需要:y;不需要:n";
cin>>Q7;
cout<<"-------------------------------------------------------------------------"<<endl;
cout<<"开始进行答题:"<<endl;
srand(time(NULL));
if(Q4==‘y‘)
{
while(1)
{
if(Q1<1)
{
cout<<"输入有误,请重新输入题目数量:";
cin>>Q1;
Q01=floor(Q1);
}
else
{
for(int j=0;j<Q01;j++)
{
if(j!=0&&j%Q2==0)
{
for(int i=0;i<Q5;i++)
{
cout<<endl;
}
}
int num1=rand()%Q3;
int num2=rand()%Q3;
int sign=rand()%4;
switch(sign)
{
case 0:
cout<<j+1<<":"<<" "<<num1<<"+"<<num2<<"="<<"\t";
cin>>ans;
if(ans==num1+num2)
{
cout<<"正确"<<endl;
right++;
}
else
{
cout<<"答错了!"<<endl;
cout<<"正确答案是:"<<num1+num2<<endl;
}
break;
case 1:
if(Q7==‘y‘)
{
cout<<j+1<<":"<<" "<<num1<<"-"<<num2<<"="<<"\t";
cin>>ans;
if(ans==num1-num2)
{
cout<<"正确"<<endl;
right++;
}
else
{
cout<<"答错了!"<<endl;
cout<<"正确答案是:"<<num1-num2<<endl;
}
}
else
{
if(num1>num2)
{
cout<<j+1<<":"<<" "<<num1<<"-"<<num2<<"="<<"\t";
cin>>ans;
if(ans==num1-num2)
{
cout<<"正确"<<endl;
right++;
}
else
{
cout<<"答错了!"<<endl;
cout<<"正确答案是:"<<num1-num2<<endl;
}
}
else
{
cout<<j+1<<":"<<" "<<num2<<"-"<<num1<<"="<<"\t";
cin>>ans;
if(ans==num2-num1)
{
cout<<"正确"<<endl;
right++;
}
else
{
cout<<"答错了!"<<endl;
cout<<"正确答案是:"<<num2-num1<<endl;
}
}
}
break;
case 2:
cout<<j+1<<":"<<" "<<num1<<"*"<<num2<<"="<<"\t";
cin>>ans;
if(ans==num1*num2)
{
cout<<"正确"<<endl;
right++;
}
else
{
cout<<"答错了!"<<endl;
cout<<"正确答案是:"<<num1*num2<<endl;
}
break;
case 3:
if(num2!=0)
{
cout<<j+1<<":"<<" "<<num1<<"/"<<num2<<"="<<"\t";
cin>>ans;
if(ans==num1/num2)
{
cout<<"正确"<<endl;
right++;
}
else
{
cout<<"答错了!"<<endl;
cout<<"正确答案是:"<<num1/num2<<endl;
}
}
else
{
j--;
}
break;
}
}
break;
}
}
cout<<"共答对"<<right<<"道题;"<<"答错"<<Q01-right<<"道题。";
cout<<"继续努力!"<<endl;
break;
}
if(Q4==‘n‘)
{
while(1)
{
if(Q01<1)
{
cout<<"输入有误,请重新输入题目数量:";
cin>>Q1;
Q01=floor(Q1);
}
else
{
for(int j=0;j<Q01;j++)
{
if(j!=0&&j%Q2==0)
{
for(int i=0;i<=Q5;i++)
{
cout<<endl;
}
}
int num1=rand()%Q3;
int num2=rand()%Q3;
int sign=rand()%2;
switch(sign)
{
case 0:
cout<<j+1<<":"<<" "<<num1<<"+"<<num2<<"="<<"\t";
cin>>ans;
if(ans==num1+num2)
{
cout<<"正确"<<endl;
right++;
}
else
{
cout<<"答错了!"<<endl;
cout<<"正确答案是:"<<num1+num2<<endl;
}
break;
case 1:
if(Q7==‘y‘)
{
cout<<j+1<<":"<<" "<<num1<<"-"<<num2<<"="<<"\t";
cin>>ans;
if(ans==num1-num2)
{
cout<<"正确"<<endl;
right++;
}
else
{
cout<<"答错了!"<<endl;
cout<<"正确答案是:"<<num1-num2<<endl;
}
break;
}
else
{
if(num1>num2)
{
cout<<j+1<<":"<<" "<<num1<<"-"<<num2<<"="<<"\t";
cin>>ans;
if(ans==num1-num2)
{
cout<<"正确"<<endl;
right++;
}
else
{
cout<<"答错了!"<<endl;
cout<<"正确答案是:"<<num1-num2<<endl;
}
break;
}
else
{
cout<<j+1<<":"<<" "<<num2<<"-"<<num1<<"="<<"\t";
cin>>ans;
if(ans==num2-num1)
{
cout<<"正确";
}
else
{
cout<<"答错了!"<<endl;
cout<<"正确答案是:"<<num2-num1<<endl;
}
break;
}
}
break;
}
}
}
break;
}
cout<<"共答对"<<right<<"道题;"<<"答错"<<Q01-right<<"道题。"<<endl;
cout<<"继续努力!"<<endl;
break;
}
else
{
cout<<"输入有误,请按要求输入!"<<endl;
}
}
cout<<endl;
while(1)
{
cout<<"还需要继续出题还是退出?(y:继续出题;n:退出)";
cin>>Q6;
if(Q6==‘y‘)
{
goto Again;
}
if(Q6==‘n‘)
{
goto Exit;
}
else
{
cout<<"输入有误,请重新输入:";
}
}
Exit:
return 0;
}
原文地址:https://www.cnblogs.com/wangbowu/p/9886442.html