|
Posted on 2011-10-05 22:03 hoshelly 閱讀(1290) 評論(1) 編輯 收藏 引用 所屬分類: C++
小累,國慶假期過去一大半,C++這時(shí)候才把遞歸和函數(shù)這一塊知識(shí)點(diǎn)慢慢地啃完了,結(jié)束之前,今晚自己寫了一個(gè)小程序,實(shí)現(xiàn)四則運(yùn)算,適合小學(xué)生使用。 程序說明: 1)允許用戶選擇一種類型的算術(shù)問題來學(xué)習(xí),輸入1表示加法,2表示減法,3表示乘法,4表示除法,5表示四種混合運(yùn)算; 2)由于程序代碼中反復(fù)無窮遞歸,所以該程序的運(yùn)算會(huì)不斷進(jìn)行下去,退出請自動(dòng)關(guān)閉程序; 源代碼如下:
#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: 實(shí)現(xiàn)四則運(yùn)算的小程序源代碼 回復(fù) 更多評論
2011-12-15 18:46 by
表示感謝
|