问题及代码:
/*
*Copyright(c)2014,烟台大学计算机与控制工程学院
*Allrights reserved.
*文件名称:test.cpp
*作者:贾庆严
*完成日期:2016年3月29日
*版本号:v1.0
*
*问题描述:设计一个程序,用来实现帮助小学生进行算术运算练习,它具有以下功能:提供10道加、减、乘、除四种基本算术运算的题目,
*每道题中的操作数是随机产生的,且操作数不超过2位数的正整数;练习者根据显示的题目输入自己的答案,程序自动判断输入的答案是否
*正确并显示出相应的信息。最后显示做对了几道题。
*/
#include <iostream>
#include<ctime>
#include<cstdlib>
int qa();
using namespace std;
int main()
{
int sum=0;
for(int i=0; i<10; i++)
{
cout<<"第"<<i+1<<"题:"<<endl;
sum+=qa();
cout<<endl;
}
cout<<"共答对了"<<sum<<"道题,得分"<<sum*10<<endl;
return 0;
}
int qa()
{
srand(time(0)); //初始化随机种子
int a,b,c,d,t;
a=rand()%100+1;
b=rand()%100+1;
c=rand()%4;
switch(c)
{
case 0:
d=a+b;
cout<<a<<"+"<<b<<"=";
break;
case 1:
d=a-b;
cout<<a<<"—"<<b<<"=";
break;
case 2:
d=a*b;
cout<<a<<"×"<<b<<"=";
break;
case 3:
d=a/b;
cout<<a<<"÷"<<b<<"=";
break;
}
cin>>t;
if(t==d)
cout<<" right! ";
else
cout<<" wrong! ";
return t==d;
}
运行结果: