码迷,mamicode.com
首页 > 其他好文 > 详细

四则运算四

时间:2016-06-23 23:43:38      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

一、详细要求:

1、 生成的题目中计算过程不能产生负数,也就是说算术表达式中如果存在形如e1 − e2的子表达式,那么e1 ≥ e2。

2、生成的题目中如果存在形如e1 ÷ e2的子表达式,那么其结果应是真分数。

3、每道题目中出现的运算符个数不超过3个,括号不限。

4、程序一次运行生成的题目不能重复,即任何两道题目不能通过有限次交换+和×左右的算术表达式变换为同一道题目。

例如,23 + 45 = 45 + 23 = 是重复的题目,6 × 8 = 8 × 6 = 也是重复的题目。

3+(2+1)1+2+3这两个题目是重复的,由于+是左结合的,1+2+3等价于(1+2)+3,也就是3+(1+2),也就是3+(2+1)。但是1+2+33+2+1是不重复的两道题,因为1+2+3等价于(1+2)+3,而3+2+1等价于(3+2)+1,它们之间不能通过有限次交换变成同一个题目。

5、把程序变成一个网页程序或智能手机程序, 用户通过设定参数,就可以得到各种题目,并可实现在线答题并评判。

二、代码

 

  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<<"="<<"\t";
 60                             break;
 61                         case 1:
 62                             if(Ques7==y)
 63                             {
 64                                 cout<<j+1<<":"<<" "<<num1<<"-"<<num2<<"="<<"\t";
 65                             }
 66                             else
 67                             {
 68                                 if(num1>num2)
 69                                 {
 70                                     cout<<j+1<<":"<<" "<<num1<<"-"<<num2<<"="<<"\t";
 71                                 }
 72                                 else
 73                                 {
 74                                     cout<<j+1<<":"<<" "<<num2<<"-"<<num1<<"="<<"\t";
 75                                 }
 76                             }
 77                             break;
 78                         case 2:
 79                             cout<<j+1<<":"<<" "<<num1<<"*"<<num2<<"="<<"\t";
 80                             break;
 81                         case 3:
 82                             if(num2!=0)
 83                             {
 84                                 cout<<j+1<<":"<<" "<<num1<<"/"<<num2<<"="<<"\t";
 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<<"="<<"\t";
126                             break;
127                         case 1:
128                             if(Ques7==y)
129                             {
130                                 cout<<j+1<<":"<<" "<<num1<<"-"<<num2<<"="<<"\t";
131                                 break;
132                             }
133                             else
134                             {
135                                 if(num1>num2)
136                                 {
137                                     cout<<j+1<<":"<<" "<<num1<<"-"<<num2<<"="<<"\t";
138                                     break;
139                                 }
140                                 else
141                                 {
142                                     cout<<j+1<<":"<<" "<<num2<<"-"<<num1<<"="<<"\t";
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 }

 

三、运行结果

技术分享

 

 四、总结

对于函数的使用不够娴熟,所以功能都是在主函数中实现的。

四则运算四

标签:

原文地址:http://www.cnblogs.com/liqing1/p/5612397.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!