算法思想:

算法思想:
(1) 產生兩個隨機數
(2) 產生一個運算符
(3) 用戶輸入答案
(4) 驗證答案正確與否
(5) 轉(1) 重複執行

主函數中用到了srand(time(NULL))上百度查了一下和隨機產生有關。如果在程序運行時沒有自主設置種子的話,用函數rand產生的隨機數序列會是一樣的。
而用srand設置隨機數種子後,可能產生不同的隨機序列(概率很大)。

  1. 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。

菜單函數

  1. void ShowMenu()  
  2. {  
  3.     cout<<"***********************"<<endl;  
  4.     cout<<"*     1- 加法         *"<<endl;  
  5.     cout<<"*     2- 減法         *"<<endl;  
  6.     cout<<"*     3- 乘法         *"<<endl;  
  7.     cout<<"*     4- 除法         *"<<endl;  
  8.     cout<<"*     5- 混合運算     *"<<endl;  
  9.     cout<<"*     6- 設置題目數   *"<<endl;  
  10.     cout<<"*     0- 退出         *"<<endl;  
  11.     cout<<"***********************"<<endl;  
  12. }  
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的範圍內隨機取一個整數。

運用循環控制題目次數。

  1. void Arithmetic(int kind,int number)  
  2. {  
  3.     int i;  
  4.     int answer,result;  
  5.     char str[80];  
  6.     int score=0;  
  7.     int operand1,operand2,operator1;  
  8.     cout<<"四則運算,題目數:"<<number<<endl;  
  9.     for(i=0; i<number; i++)//控制程序的題目數   
  10.     {  
  11.         operand1 =rand()%101;//產生1到100內的隨機數   
  12.         operand2 =rand()%101;  
  13.         switch(kind)  
  14.         {  
  15.         case '1':  
  16.             operator1=1;  
  17.             break;  
  18.         case '2':  
  19.             operator1=2;  
  20.             break;  
  21.         case '3':  
  22.             operator1=3;  
  23.             break;  
  24.         case '4':  
  25.             operator1=4;  
  26.             break;  
  27.         default:  
  28.             operator1=rand()%4+1;//operator1爲1到5之間的隨機數   
  29.         }  
  30.         switch(operator1)  
  31.         {  
  32.         case 1:  
  33.             cout<<operand1<<"+"<<operand2<<"=";  
  34.             result =operand1+operand2;  
  35.             break;  
  36.         case 2:  
  37.             cout<<operand1<<"-"<<operand2<<"=";  
  38.             result =operand1-operand2;  
  39.             break;  
  40.         case 3:  
  41.             cout<<operand1<<"*"<<operand2<<"=";  
  42.             result =operand1*operand2;  
  43.             break;  
  44.         case 4:  
  45.             if(operand2==0) //注意除數爲0   
  46.                 operand2=1;  
  47.             operand1 = operand1 * operand2;  
  48.             cout<<operand1<<"/"<<operand2<<"=";  
  49.             result =operand1/operand2;  
  50.         }  
  51.         //cin>>answer;   
  52.         cin.getline(str,80);  
  53.         //answer = atoi(str);   
  54.         sscanf(str,"%d",&answer);  
  55.         if(answer==result)  
  56.             score+=10;  
  57.     }  
  58.     cout<<"您的得分:"<<score<<endl;  
  59.     cout<<"按任意鍵返回!";  
  60.     getch();  
  61.     cout<<endl;  
  62. }  
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;
}

再加上相應的頭文件

  1. #include <iostream>   
  2. #include <time.h>   
  3. #include <stdlib.h>   
  4. #include <conio.h>   
  5. using namespace std;  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章