/*
*copyright (c) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称:hellow.cpp
*作者:田甜
*完成日期:2016年3月24日
*版本号:v1.0
*
*问题描述:设计一个程序可以随机提供10道加减乘除基本运算问题,且数字是不超过两位的正整数
*输入描述:无
*程序输出:10到由不超过两位的正整数组成的加减乘除基本运算题
*/
问题及代码:
#include<iostream>
#include<cstdlib>
#include<ctime>
double suan(int m);
using namespace std;
int main()
{
int a,timu,n,i,right=0,wrong=0;
for(i=1;i<=10;i++)
{ cout<<"第"<<i<<"题:"<<endl;//输出题号
srand(time(0));
a=rand()%4;//随机产生1~4之间的数字
timu=suan(a);//随机产生加减乘除中的一种题目
cin>>n;
if(n==timu)
{
cout<<" Right!"<<endl;
right++;//统计答对题目的个数
}
else
cout<<" Wrong!"<<endl;
}
cout<<"共答对了"<<right<<"道题,得分:"<<right*10<<endl;//输出答对题目的个数
return 0;
}
double suan(int m)
{
int x,y;
switch(m)
{
case 0:
x=rand()%100;
y=rand()%100;
cout<<x<<"+"<<y<<"=";
return x+y;
case 1:
x=rand()%100;
y=rand()%100;
cout<<x<<"-"<<y<<"=";
return x-y;
case 2:
x=rand()%100;
y=rand()%100;
cout<<x<<"*"<<y<<"=";
return x*y;
case 3:
x=rand()%100;
y=rand()%100;
cout<<x<<"/"<<y<<"=";
return x/y;
}
}
运行结果:
知识点总结:
使系统产生随机数,运用switch 语句进行多种情况的选择
学习心得:
通过rand()产生随机数传入switch语句中,可以实现随机出现加减乘除中的一种