算法思想:

算法思想:
(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;  

}  

主函数中调用了两个函数:菜单函数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. }  

四则运算函数中用了随机产生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. }  

再加上相应的头文件

  1. #include <iostream>   
  2. #include <time.h>   
  3. #include <stdlib.h>   
  4. #include <conio.h>   
  5. using namespace std;  
原文地址:https://www.cnblogs.com/u013533289/p/4477337.html