四则运算程序演变3

题目:在此前四则运算题目的基础上,增加用户可以输入答案并由系统进行判断,统计正确错误题数。

思路:在每一个输出的题目后,让用户开始输入答案,直接进行判断,然后累加。

程序代码:

  1 #include<iostream.h>
  2 #include<stdio.h>
  3 #include<stdlib.h>
  4 #include <time.h>
  5 void main()
  6 {
  7     double result;
  8     double answer;
  9     int right=0,wrong=0;
 10     int Num,Values,MulDiv,Negative,Remainder;
 11     int x,y,z;
 12     cout<<"题目数量:";
 13     cin>>Num;
 14     cout<<"数值范围:";
 15     cin>>Values;
 16     cout<<"是否有乘除法(1表示有,其他字符表示没有):";
 17     cin>>MulDiv;
 18     cout<<"加减有无负数(1表示允许,其他字符表示不允许):";
 19     cin>>Negative;
 20     cout<<"除法有无余数(1表示允许,其他字符表示不允许):";
 21     cin>>Remainder;
 22     srand((unsigned) time(NULL)); //设置种子
 23 
 24 
 25     for(int i=0;i<Num;i++)     //进行 题目数量 次循环
 26     {
 27         x=rand()%Values;
 28         y=rand()%Values; //控制数值范围
 29 
 30 
 31         if(MulDiv==1)     //控制有无乘除法
 32             z=rand()%4;     //根据z的值随机出现四则运算
 33         else
 34             z=rand()%2;
 35 
 36         if(z==0)
 37         {
 38               cout<<x<<"+"<<y<<"=";
 39               cin>>answer;
 40               result=x+y;
 41               if (result==answer)
 42               {
 43                    cout<<"Right"<<endl;
 44                 right++;
 45               }
 46               else
 47               {
 48                 cout<<"Wrong!The right answer is"<<result<<endl;
 49                 wrong++;
 50               }
 51 
 52         } //加法
 53        if(z==1) 
 54        { 
 55              if(Negative != 1)
 56              {
 57                    if (x>=y)
 58                    {
 59                         cout<<x<<"-"<<y<<"=";
 60                         cin>>answer;
 61                         result=x-y;
 62                         if (result==answer)
 63                         {
 64                             cout<<"Right"<<endl;
 65                             right++;
 66                         }
 67                         else
 68                         {
 69                             cout<<"Wrong!The right answer is"<<result<<endl;
 70                             wrong++;
 71                         }
 72                    }
 73                    else
 74                    {
 75                         cout<<y<<"-"<<x<<"=";
 76                         cin>>answer;
 77                         result=y-x;
 78                         if (result==answer)
 79                         {
 80                             cout<<"Right"<<endl;
 81                             right++;
 82                         }
 83                         else
 84                         {
 85                             cout<<"Wrong!The right answer is"<<result<<endl;
 86                             wrong++;
 87                         }
 88                    }
 89              }
 90              else
 91              {
 92                    cout<<x<<"-"<<y<<"=";
 93                    cin>>answer;
 94                    result=x-y;
 95                    if (result==answer)
 96                    {
 97                        cout<<"Right"<<endl;
 98                        right++;
 99                    }
100                    else
101                    {
102                        cout<<"Wrong!The right answer is"<<result<<endl;
103                        wrong++;
104                    }
105               }
106 
107       } //减法
108       if(z==2 ) 
109       {    
110              while((Remainder != 1)&&(x%y != 0))
111              {
112                   x=rand()%Values;    
113              }
114 
115 
116              cout<<x<<"÷"<<y<<"=";
117              cin>>answer;
118              result=x/y;
119              if (result==answer)
120              {
121                  cout<<"Right"<<endl;
122                  right++;
123              }
124              else
125              {
126                  cout<<"Wrong!The right answer is"<<result<<endl;
127                  wrong++;
128              }
129 
130       } //除法
131 
132       if(z==3)
133       {
134              cout<<x<<"*"<<y<<"=";
135              cin>>answer;
136              result=x*y;
137              if (result==answer)
138              {
139                  cout<<"Right"<<endl;
140                  right++;
141              }
142              else
143              {
144                  cout<<"Wrong!The right answer is"<<result<<endl;
145                  wrong++;
146              }
147        } //乘法
148     }
149     cout<<"答题正确个数为:"<<right<<endl;
150     cout<<"答题错误个数为:"<<wrong<<endl;
151 }

程序结果:

感想:

    在设计上,程序代码仍能在简洁些。代码还有着这样或那样的缺陷,但是我会努力去修正错误,让代码更完整些。

原文地址:https://www.cnblogs.com/cainiao1hao/p/4355357.html