|
Posted on 2011-10-05 22:03 hoshelly 阅读(1267) 评论(1) 编辑 收藏 引用 所属分类: C++
小累,国庆假期过去一大半,C++这时候才把递归和函数这一块知识点慢慢地啃完了,结束之前,今晚自己写了一个小程序,实现四则运算,适合小学生使用。 程序说明: 1)允许用户选择一种类型的算术问题来学习,输入1表示加法,2表示减法,3表示乘法,4表示除法,5表示四种混合运算; 2)由于程序代码中反复无穷递归,所以该程序的运算会不断进行下去,退出请自动关闭程序; 源代码如下:
#include<iostream> #include<cstdlib> #include<ctime> using namespace std; int Mul(int,int); int Plus(int,int); int Sub(int,int); float Div(float,float); int main() { int a=0,b=0; int i; srand(time(0)); cout<<"What do you want to study?(1-Plus,2-Sub,3-Mul,4-division,5-all the above)"<<endl; cin>>i switch(i) { case 1:Plus(a,b);break; case 2:Sub(a,b);break; case 3:Mul(a,b);break; case 4:Div(a,b);break; case 5:switch(1+rand()%4) { case 1:Plus(a,b);break; case 2:Sub(a,b);break; case 3:Mul(a,b);break; case 4:Div(a,b);break; default:break; }break;
default:break; } return 0; } float Div(float x,float y) { float m1, m; int k; k=1+rand()%4; int a1; cout<<"Please choose the level of this game(1 or 2):"<<endl; cin>>a1; srand(time(0)); if(a1==1) { x=1+rand()%9; y=1+rand()%9; } if(a1==2) { x=1+rand()%99; y=1+rand()%99; } m=x/y; cout<<x<<"/"<<y<<"=?"<<endl; cin>>m1; if(m1==m) { switch(k) { case 1:cout<<"Very good!"<<endl;break; case 2:cout<<"Excellent!"<<endl;break; case 3:cout<<"Nice work!"<<endl;break; case 4:cout<<"Keep up the good work!"<<endl;break; default:break; } Div(x,y); } else { switch(k) { case 1:cout<<"Sorry,your answer is wrong!"<<endl;break; case 2:cout<<"Wrong.Try once again!"<<endl;break; case 3:cout<<"Don't give up!"<<endl;break; case 4:cout<<"No.Keep trying."<<endl;break; default:break; } Div(x,y); } return 0; }
int Sub(int x,int y) { int m1, m,k; k=1+rand()%4; int a1; cout<<"Please choose the level of this game(1 or 2):"<<endl; cin>>a1; srand(time(0)); if(a1==1) { x=1+rand()%9; y=1+rand()%9; } if(a1==2) { x=1+rand()%99; y=1+rand()%99; } m=x-y; cout<<x<<"-"<<y<<"=?"<<endl; cin>>m1; if(m1==m) { switch(k) { case 1:cout<<"Very good!"<<endl;break; case 2:cout<<"Excellent!"<<endl;break; case 3:cout<<"Nice work!"<<endl;break; case 4:cout<<"Keep up the good work!"<<endl;break; default:break; } Sub(x,y); } else { switch(k) { case 1:cout<<"Sorry,your answer is wrong!"<<endl;break; case 2:cout<<"Wrong.Try once again!"<<endl;break; case 3:cout<<"Don't give up!"<<endl;break; case 4:cout<<"No.Keep trying."<<endl;break; default:break; } Sub(x,y); } return 0; }
int Plus(int x,int y) { int m1, m,k; k=1+rand()%4; int a1; cout<<"Please choose the level of this game(1 or 2):"<<endl; cin>>a1; srand(time(0)); if(a1==1) { x=1+rand()%9; y=1+rand()%9; } if(a1==2) { x=1+rand()%99; y=1+rand()%99; } m=x+y; cout<<x<<"+"<<y<<"=?"<<endl; cin>>m1; if(m1==m) { switch(k) { case 1:cout<<"Very good!"<<endl;break; case 2:cout<<"Excellent!"<<endl;break; case 3:cout<<"Nice work!"<<endl;break; case 4:cout<<"Keep up the good work!"<<endl;break; default:break; } Plus(x,y); } else { switch(k) { case 1:cout<<"Sorry,your answer is wrong!"<<endl;break; case 2:cout<<"Wrong.Try once again!"<<endl;break; case 3:cout<<"Don't give up!"<<endl;break; case 4:cout<<"No.Keep trying."<<endl;break; default:break; } Plus(x,y); } return 0; }
int Mul(int x,int y) {
int m1, m,k; k=1+rand()%4; int a1; cout<<"Please choose the level of this game(1 or 2):"<<endl; cin>>a1; srand(time(0)); if(a1==1) { x=1+rand()%9; y=1+rand()%9; } if(a1==2) { x=1+rand()%99; y=1+rand()%99; } m=x*y; cout<<x<<"*"<<y<<"=?"<<endl; cin>>m1; if(m1==m) { switch(k) { case 1:cout<<"Very good!"<<endl;break; case 2:cout<<"Excellent!"<<endl;break; case 3:cout<<"Nice work!"<<endl;break; case 4:cout<<"Keep up the good work!"<<endl;break; default:break; } Mul(x,y); } else { switch(k) { case 1:cout<<"Sorry,your answer is wrong!"<<endl;break; case 2:cout<<"Wrong.Try once again!"<<endl;break; case 3:cout<<"Don't give up!"<<endl;break; case 4:cout<<"No.Keep trying."<<endl;break; default:break; } Mul(x,y); } return 0; }
小程序的压缩版下载地址: http://ishare.iask.sina.com.cn/f/19626833.html
Feedback
# re: 实现四则运算的小程序源代码 回复 更多评论
2011-12-15 18:46 by
表示感谢
|