随机实现100以内的的加减乘除运算
2014-02-14 14:04
344 查看
算法思想:
(1) 产生两个随机数
(2) 产生一个运算符
(3) 用户输入答案
(4) 验证答案正确与否
(5) 转(1) 重复执行
主函数中用到了srand(time(NULL))上百度查了一下和随机产生有关。如果在程序运行时没有自主设置种子的话,用函数rand产生的随机数序列会是一样的。
而用srand设置随机数种子后,可能产生不同的随机序列(概率很大)。
菜单函数
四则运算函数中用了随机产生rand()%101。 rand()是随机取一个整数,再摸101,就是取余数。合起来的意思就是在0-100的范围内随机取一个整数。
运用循环控制题目次数。
再加上相应的头文件
就可以了。
(1) 产生两个随机数
(2) 产生一个运算符
(3) 用户输入答案
(4) 验证答案正确与否
(5) 转(1) 重复执行
主函数中用到了srand(time(NULL))上百度查了一下和随机产生有关。如果在程序运行时没有自主设置种子的话,用函数rand产生的随机数序列会是一样的。
而用srand设置随机数种子后,可能产生不同的随机序列(概率很大)。
int main() { int number = 2; int choice; srand(time(NULL)); do { ShowMenu(); choice = getchar(); getchar(); //清除行尾回车 switch(choice) { case '1': case '2': case '3': case '4': case '5': Arithmetic(choice,number); break; case '6': cout<<"输入题目数:"; cin>>number; getchar(); } } while(choice!='0'); return 0; }主函数中调用了两个函数:菜单函数ShowMenu和四则运算函数Arithmetic。
菜单函数
void ShowMenu() { cout<<"***********************"<<endl; cout<<"* 1- 加法 *"<<endl; cout<<"* 2- 减法 *"<<endl; cout<<"* 3- 乘法 *"<<endl; cout<<"* 4- 除法 *"<<endl; cout<<"* 5- 混合运算 *"<<endl; cout<<"* 6- 设置题目数 *"<<endl; cout<<"* 0- 退出 *"<<endl; cout<<"***********************"<<endl; }
四则运算函数中用了随机产生rand()%101。 rand()是随机取一个整数,再摸101,就是取余数。合起来的意思就是在0-100的范围内随机取一个整数。
运用循环控制题目次数。
void Arithmetic(int kind,int number) { int i; int answer,result; char str[80]; int score=0; int operand1,operand2,operator1; cout<<"四则运算,题目数:"<<number<<endl; for(i=0; i<number; i++)//控制程序的题目数 { operand1 =rand()%101;//产生1到100内的随机数 operand2 =rand()%101; switch(kind) { case '1': operator1=1; break; case '2': operator1=2; break; case '3': operator1=3; break; case '4': operator1=4; break; default: operator1=rand()%4+1;//operator1为1到5之间的随机数 } switch(operator1) { case 1: cout<<operand1<<"+"<<operand2<<"="; result =operand1+operand2; break; case 2: cout<<operand1<<"-"<<operand2<<"="; result =operand1-operand2; break; case 3: cout<<operand1<<"*"<<operand2<<"="; result =operand1*operand2; break; case 4: if(operand2==0) //注意除数为0 operand2=1; operand1 = operand1 * operand2; cout<<operand1<<"/"<<operand2<<"="; result =operand1/operand2; } //cin>>answer; cin.getline(str,80); //answer = atoi(str); sscanf(str,"%d",&answer); if(answer==result) score+=10; } cout<<"您的得分:"<<score<<endl; cout<<"按任意键返回!"; getch(); cout<<endl; }
再加上相应的头文件
#include <iostream> #include <time.h> #include <stdlib.h> #include <conio.h> using namespace std;
就可以了。
相关文章推荐
- 1----编程实现100以内两个随机整数的加法运算练习(rand函数)
- 随机生成30道100以内的四则运算题
- 随机出100以内的数的加减法运算
- python:100以内的俩个数随机加减乘除
- 随机100以内的加法联系程序代码实现(c#)
- 软件工程概论第一次课堂小测-------产生30个100以内的随机整数四则运算的小程序
- No.3 (code review)复杂计算器(实现8位以内的连续加减乘除运算---可能有BUG,还希望多多帮忙改善!)
- 软件工程实验一--编程随机生成30个四则运算,算数包括100以内的整数和真分数。
- 随机生成30道100以内的四则运算题目
- 用位运算实现四则运算之加减乘除(用位运算求一个数的1/3)
- 正整数加减乘除四则混合运算求值方法 java实现
- BigDecimal进行精确运算(实现加减乘除运算)
- 用位运算实现两个整数的加减乘除运算
- 打印100以内的素数(质数)----C++实现、python实现
- 实现一个用分子分母的格式来表示有理数的结构体rational以及相关的函数,rational结构体之间可以做加减乘除运算,运算的结果仍然是rational
- 通过键盘输入100 以内正整数的加、减运算式,请编写一个程序输出运算结果字符串
- JS最基础的获取元素的值实现加减乘除运算
- 用位运算实现四则运算之加减乘除
- 随机生成加减乘除混合运算加减乘除数学式子题目答案批量生成工具软件使用方法
- c语言:编写一个简易计算器,打印菜单界面,实现加减乘除运算,可以退出菜单界面