• 四则运算出题器(C++)-BUG修复


    定制题目数量这个功能测试:

    1)输入题目数为负数时:

    可正确处理;

    2)输入题目数量为0时:

    可正确处理;

    (3)输入题目数量为小数时:

    程序运行出错;

    错误分析:

    因为代码中题目数量的变量Ques1定义为int类型,无法处理小数。

    解决方法:

    Ques1定义为double类型,然后使用floor()函数,将Ques1取整。

    输入10.1

    可以正常处理。

    修改后完整代码:

      1 #include<iostream.h>
      2 #include<stdlib.h>
      3 #include<time.h>
      4 #include<math.h>
      5 int main()
      6 {
      7     double Ques1=1;
      8     int Ques01;
      9     int Ques2=1;
     10     int Ques3=100;
     11     char Ques4='y';
     12     int Ques5=1;
     13     char Ques6='n';
     14     char Ques7='n';
     15 Again:
     16     cout<<"请输入题目数量:";
     17     cin>>Ques1;
     18     Ques01=floor(Ques1);
     19     cout<<"请输入每行打印题目数(1-5):";
     20     cin>>Ques2;
     21     cout<<"请输入算式中数值的最大值:";
     22     cin>>Ques3;
     23     while(1)
     24     {
     25         cout<<"运算中需要乘除法吗?y:需要;n:不需要";
     26         cin>>Ques4;
     27         cout<<"减法运算需要有负数吗?y:需要;n:不需要";
     28         cin>>Ques7;
     29         cout<<"行间距(正整数):";
     30         cin>>Ques5;
     31         srand(time(NULL));
     32         if(Ques4=='y')
     33         {
     34             while(1)
     35             {
     36                 if(Ques1<1)
     37                 {
     38                     cout<<"输入有误,请重新输入题目数量:";
     39                     cin>>Ques1;
     40                     Ques01=floor(Ques1);
     41                 }
     42                 else
     43                 {
     44                     for(int j=0;j<Ques01;j++)
     45                     {
     46                         if(j!=0&&j%Ques2==0)
     47                         {
     48                             for(int i=0;i<Ques5;i++)
     49                             {
     50                                 cout<<endl;
     51                             }        
     52                         }
     53                         int num1=rand()%Ques3;
     54                         int num2=rand()%Ques3;
     55                         int sign=rand()%4;
     56                         switch(sign)
     57                         {
     58                         case 0:
     59                             cout<<j+1<<":"<<" "<<num1<<"+"<<num2<<"="<<"	";
     60                             break;
     61                         case 1:
     62                             if(Ques7=='y')
     63                             {
     64                                 cout<<j+1<<":"<<" "<<num1<<"-"<<num2<<"="<<"	";
     65                             }
     66                             else
     67                             {
     68                                 if(num1>num2)
     69                                 {
     70                                     cout<<j+1<<":"<<" "<<num1<<"-"<<num2<<"="<<"	";
     71                                 }
     72                                 else
     73                                 {
     74                                     cout<<j+1<<":"<<" "<<num2<<"-"<<num1<<"="<<"	";
     75                                 }
     76                             }
     77                             break;
     78                         case 2:
     79                             cout<<j+1<<":"<<" "<<num1<<"*"<<num2<<"="<<"	";
     80                             break;
     81                         case 3:
     82                             if(num2!=0)
     83                             {
     84                                 cout<<j+1<<":"<<" "<<num1<<"/"<<num2<<"="<<"	";
     85                             }
     86                             else
     87                             {
     88                                 j--;
     89                             }
     90                             break;
     91                         }
     92                     }
     93                     break;
     94                 }
     95             }
     96             break;
     97         }
     98         if(Ques4=='n')
     99         {
    100             while(1)
    101             {
    102                 if(Ques01<1)
    103                 {
    104                     cout<<"输入有误,请重新输入题目数量:";
    105                     cin>>Ques1;
    106                     Ques01=floor(Ques1);
    107                 }
    108                 else
    109                 {
    110                     for(int j=0;j<Ques01;j++)
    111                     {
    112                         if(j!=0&&j%Ques2==0)
    113                         {
    114                             for(int i=0;i<=Ques5;i++)
    115                             {
    116                                 cout<<endl;
    117                             }
    118                         }
    119                         int num1=rand()%Ques3;
    120                         int num2=rand()%Ques3;
    121                         int sign=rand()%2;
    122                         switch(sign)
    123                         {
    124                         case 0:
    125                             cout<<j+1<<":"<<" "<<num1<<"+"<<num2<<"="<<"	";
    126                             break;
    127                         case 1:
    128                             if(Ques7=='y')
    129                             {
    130                                 cout<<j+1<<":"<<" "<<num1<<"-"<<num2<<"="<<"	";
    131                                 break;
    132                             }
    133                             else
    134                             {
    135                                 if(num1>num2)
    136                                 {
    137                                     cout<<j+1<<":"<<" "<<num1<<"-"<<num2<<"="<<"	";
    138                                     break;
    139                                 }
    140                                 else
    141                                 {
    142                                     cout<<j+1<<":"<<" "<<num2<<"-"<<num1<<"="<<"	";
    143                                     break;
    144                                 }
    145                             }
    146                             break;
    147                         }
    148                     }
    149                 }
    150                 break;
    151             }
    152             break;
    153         }
    154         
    155         else
    156         {
    157             cout<<"输入有误,请按要求输入!"<<endl;
    158         }
    159     }
    160     cout<<endl;
    161     while(1)
    162     {
    163         cout<<"还需要继续出题还是退出?(y:继续出题;n:退出)";
    164         cin>>Ques6;
    165         if(Ques6=='y')
    166         {
    167             goto Again;
    168         }
    169         if(Ques6=='n')
    170         {
    171             goto Exit;
    172         }
    173         else
    174         {
    175             cout<<"输入有误,请重新输入:";
    176         }
    177     }
    178 Exit:
    179     return 0;
    180 }
  • 相关阅读:
    windows :Tomcat免安装版环境变量配置 + jdk配置
    如何在官网下载Spring jar包
    浅析win32 Win64 x86 x64 区别 及Eclipse启动报Java was started but returned exit code=13 错误
    MyBatis拦截器打印不带问号的完整sql语句方法
    MyBatis多个接口参数报错:Available parameters are [0, 1, param1, param2], 及解决方法
    Leetcode40--->Combination Sum II
    Leetcode39--->Combination Sum(在数组中找出和为target的组合)
    Leetcode38--->Count and Say
    js 保留小数位数
    如何禁用easyui-linkbutton 中的Click事件
  • 原文地址:https://www.cnblogs.com/zglsh/p/4342013.html
Copyright © 2020-2023  润新知