(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;
}
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;
- }
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;
- }
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;