解决的计算器问题
来源:互联网 发布:减速箱装配图 淘宝 编辑:程序博客网 时间:2024/06/05 20:22
<pre name="code" class="cpp">#include<iostream>#include <cstdio>#include<ctime>#include<cstdlib>#include <cmath>using namespace std;void add_fun();void sub_fun();void mul_fun();void div_fun();int sum=0;int main(){int i,m;char xx[4]={'+','-','*','/'};m=xx[4];for(i=0;i<10;i++){ srand((unsigned)time(0)); m=rand()%4; switch(m) { case 0:add_fun();break; case 1:sub_fun();break; case 2:mul_fun();break;case 3:div_fun();break; } } cout<<"你一共做对了"<<sum<<"道题!"<<endl; return 0;}void add_fun(){int x,y,z,w; char yy;srand((unsigned)time(NULL));x=rand()%100;y=rand()%100;z=x+y;//得到正确答案cout << x <<"+"<< y << "=";cin >> w ; if(w==z) { cout << "right!" << endl; sum++;}else { cout << "wrong!点击 y 重新此题" << endl; cin >> yy; if(yy=='y') { cout<<x<<"+"<<y<<"="; cin>>w ; if(w==z) { cout << "right!" << endl; } } }}void sub_fun(){int x,y,z,w;char yy; srand((unsigned)time(NULL)); do { x=rand()%100; y=rand()%100; }while(x<=y); z=x-y; cout << x <<"-"<< y << "="; cin >> w; if(w==z) { cout << "right!" << endl; sum++; } else { cout << "wrong!点击 y 重新此题" << endl; cin >> yy; if(yy=='y') { cout<<x<<"-"<<y<<"="; cin>>w ; if(w==z) { cout << "right!" << endl; } } }}void mul_fun(){ int x,y,z,w; char yy; srand((unsigned)time(0)); do { x=rand()%10; y=rand()%10; }while(x<=y); z=x*y;cout<<x<<"*"<<y<<"="; cin>>w ; if(w==z) { cout<<"right!" <<endl; sum++; } else { cout << "wrong!点击 y 重新此题" << endl; cin >> yy; if(yy=='y') { cout<<x<<"*"<<y<<"="; cin>>w ; if(w==z) { cout << "right!" << endl; } } }}void div_fun(){ int x,y,z,w; char yy;srand((unsigned)time(0));do { x=rand()%10; y=rand()%10; }while(x%y!=0); z=x/y;cout<<x<<"/"<<y<<"="; cin >> w ; if(w==z) { cout << "right!" << endl; sum++; } else { cout << "wrong!点击 y 重新此题" << endl; cin >> yy; if(yy=='y') { cout<<x<<"*"<<y<<"="; cin>>w ; if(w==z) { cout << "right!" << endl; } } }}以上是改正过后的程序,改之前的程序,用到了递归调用,导致程序不能运行,因为对于随机生成的x,y,不满足条件的一直在缓存区中,没有释放!
#include<iostream>#include <cstdio>#include<ctime>#include<cstdlib>#include <cmath>using namespace std;void add_fun();void sub_fun();void mul_fun();void div_fun();int main(){<span style="white-space:pre"></span>int i,m;<span style="white-space:pre"></span>char xx[4]={'+','-','*','/'};<span style="white-space:pre"></span>m=xx[4];<span style="white-space:pre"></span>for(i=0;i<10;i++)<span style="white-space:pre"></span>{<span style="white-space:pre"></span> srand((unsigned)time(0));<span style="white-space:pre"></span> m=rand()%4;<span style="white-space:pre"></span> switch(m)<span style="white-space:pre"></span> {<span style="white-space:pre"></span> <span style="white-space:pre"></span>case 0:add_fun();break;<span style="white-space:pre"></span> <span style="white-space:pre"></span>case 1:sub_fun();break;<span style="white-space:pre"></span> <span style="white-space:pre"></span>case 2:mul_fun();break;<span style="white-space:pre"></span>case 3:div_fun();break;<span style="white-space:pre"></span> } } //cout<<"你一共做对了"<<sum1<<"道题!"<<endl; return 0;}void add_fun(){<span style="white-space:pre"></span>int x,y,z,w; <span style="white-space:pre"></span>char yy;<span style="white-space:pre"></span>srand((unsigned)time(NULL));<span style="white-space:pre"></span>x=rand()%100;<span style="white-space:pre"></span>y=rand()%100;<span style="white-space:pre"></span>z=x+y;//得到正确答案<span style="white-space:pre"></span>cout << x <<"+"<< y << "=";<span style="white-space:pre"></span>cin >> w ; if(w==z) { <span style="white-space:pre"></span>cout << "right!" << endl;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>else { cout << "wrong!点击 y 重新此题" << endl; cin >> yy; if(yy=='y') { cout<<x<<"+"<<y<<"="; cin>>w ; if(w==z) { cout << "right!" << endl; } } }}void sub_fun(){<span style="white-space:pre"></span>int x,y,z,w;<span style="white-space:pre"></span>char yy; srand((unsigned)time(NULL)); x=rand()%100; y=rand()%100; if(x>y) { z=x-y; cout << x <<"-"<< y << "="; cin >> w; if(w==z) { cout << "right!" << endl; } else { cout << "wrong!点击 y 重新此题" << endl; cin >> yy; if(yy=='y') { cout<<x<<"-"<<y<<"="; cin>>w ; if(w==z) <span style="white-space:pre"></span>{ <span style="white-space:pre"></span>cout << "right!" << endl; <span style="white-space:pre"></span>} } } } else { sub_fun(); }}void mul_fun(){ <span style="white-space:pre"></span>int x,y,z,w; <span style="white-space:pre"></span>char yy; srand((unsigned)time(0)); do { x=rand()%10; y=rand()%10; }while(x<=y); z=x*y;<span style="white-space:pre"></span>cout<<x<<"*"<<y<<"="; <span style="white-space:pre"></span>cin>>w ; if(w==z) { cout<<"right!" <<endl; } else { cout << "wrong!点击 y 重新此题" << endl; cin >> yy; if(yy=='y') { cout<<x<<"*"<<y<<"="; cin>>w ; if(w==z) <span style="white-space:pre"></span>{ <span style="white-space:pre"></span>cout << "right!" << endl; <span style="white-space:pre"></span>} } }}void div_fun(){ int x,y,z,w; char yy;<span style="white-space:pre"></span>srand((unsigned)time(0));<span style="white-space:pre"></span>do { x=rand()%10; y=rand()%10; }while(x%y!=0); <span style="white-space:pre"></span>z=x/y;<span style="white-space:pre"></span>cout<<x<<"/"<<y<<"="; <span style="white-space:pre"></span>cin >> w ; <span style="white-space:pre"></span>if(w==z) <span style="white-space:pre"></span>{ <span style="white-space:pre"></span>cout << "right!" << endl;<span style="white-space:pre"></span> <span style="white-space:pre"></span>}}所以在我们写程序的时候,要考虑多种因素!争取到最简洁,最少耗时的程序!
0 0
- 解决的计算器问题
- 计算器原代码的问题
- Java 递归解决 "只能两数相乘的计算器计算x^y" 问题
- 问题 : 小A的计算器
- 计算器之如何解决运算符优先级问题
- 计算器问题
- 计算器问题...
- 关于那个计算器编译器的问题
- PHP防灌水计算器(TIME_TO_SEC)的问题
- 关于windows计算器符号的问题
- JAVA中计算器的小数问题
- ArcGIS中字段计算器的问题
- 一个C的逆波兰式计算器的问题
- 小加法计算器的实现,遇到的问题
- 关于计算器事件的监听问题求指教!
- Qt5编写科学计算器碰到的一个问题
- 问题C:某计算器的超电磁炮
- 需要解决的问题
- 架构案例丨苏宁易购:商品详情系统架构设计
- Laravel 5.2 使用ckeditor
- 欢迎使用CSDN-markdown编辑器
- Java 单例
- IPv6地址表示方法
- 解决的计算器问题
- 集合框架的阅读技巧
- HTML简单表单
- FND_GLOBAL PACKAGE返回全局变量
- 分页加载
- 自定义控件开发入门(一)
- 设计模式
- 使用苹果原生API进行二维码和条形码的扫描
- java复习一 ——java web项目启动加载顺序