四则运算3

老师提出了新的要求:

1 学生写的程序必须能判定用户的输入答案是否正确

2程序必须能处理四则运算的混合模式

注意:连续的减法和除法应该遵守左结合的规定

        连续除法要打括号,否则会引起歧义。

设计思想

 基于上次程序,首先思考混合运算,这里运用两个数组,分别记录随机产生的运算数和运算符号。然后对连除进行判断,,检查运算符号数组里是否有连续的除号,有的话加上括号,避免歧义。最后对运算结果进行计算,并对用户输入的结果进行比较,给出相应提示,回答,计算方面,要优先对括号,乘除进行运算,然后再进行加减运算,每次运算完后都要记录下运算的结果。

源程序代码:

  1 #include<iostream>
  2 #include<time.h>
  3 using namespace std;
  4 #define N 100
  5 void main()
  6 {
  7     srand(time(NULL));//题目避免重复
  8     int e, num, min, max, out, d, fushu, kh, k, yushu;//有无真分数判断,题目数量,最小值,最大值,数值的范围,输出方式,是否有乘除运算,加减法是否有负数,有无括号
  9     cout << "********二柱子四则运算定制版********" << endl;
 10     cout << "请输入题目的数量num:" << endl;
 11     cin >> num;
 12     cout << "请选择数值范围最大值(大于0)" << endl;
 13     cin >> max;
 14     cout << "请选择有无乘除运算d(0没有乘除运算, 1有乘除运算)" << endl;
 15     cin >> d;
 16     cout << "请选择加减运算有无负数fushu(0没有负数 ,1有负数)" << endl;
 17     cin >> fushu;
 18     cout << "除法是否有余数(0没有余数,1有余数)" << endl;
 19     cin >> yushu;
 20     srand((unsigned)time(NULL));
 21     for (int m = 0; m < num; m++)
 22     {
 23         int a = 2 + rand() % 4;
 24         int a1[N] = {};//存运算的随机数
 25         int y;//运算结果
 26         for (int i = 0; i < a; i++)//确定数的范围(max),存入数组a1[i]中
 27         {
 28             int b = 1 + rand() % max;
 29             a1[i] = b;
 30         }
 31 
 32         char b1[N] = {};//存进行运算的符号
 33         char b[4] = { '+', '-', '*', '/' };
 34         for (int i = 0; i < a - 1; i++)//判断连续除法,以确定除法可以整除
 35         {
 36             int f = rand() % 4;
 37             b1[i] = b[f];
 38             if (f == 3)
 39             {
 40                 int c = rand() % 10 + 1;
 41                 a1[i] = a1[i + 1] * c;
 42                 if (b1[i - 1] == '/')
 43                 {
 44                     int c1 = rand() % 10 + 1;
 45                     a1[i - 1] = a1[i] * a1[i + 1] * c1;
 46                 }
 47             }
 48             for (int i = 0; i < a - 3; i++)//遇到连除三次或四次的时候变换符号
 49             {
 50                 if (b1[i] == b1[i + 1] == b1[i + 2] == '/')
 51                 {
 52                     b1[i] = '+';
 53                 }
 54                 if (b1[i] == b1[i + 1] == b1[i + 2] == b1[i + 3] == '/')
 55                 {
 56                     b1[i + 3] = '-';
 57                 }
 58             }
 59         }
 60         
 61         if (d == 0 && fushu == 0)//只有加减运算,无负数
 62         {
 63             for (int i = 0; i < a - 1; i++)//输出运算
 64             {
 65                 int g=rand()%2;
 66                 b1[i]=b[g];
 67 
 68                 if (b1[i] == '/'&&b1[i + 1] == '/')//连续除法用括号分开
 69                 {
 70                     cout << '(' << a1[i] << b1[i];
 71                 }
 72                 else
 73                     if (b1[i] == '/'&&b1[i - 1] == '/'&&i>0)
 74                     {
 75                         cout << a1[i] << ')' << b1[i];
 76                     }
 77                     else
 78                         cout << a1[i] << b1[i];
 79             }
 80             cout << a1[a - 1] << '=' << endl;
 81         }
 82         if (d == 0 && fushu == 1)//无乘除,有负数
 83         {
 84             for (int i = 0; i < a - 1; i++)//输出运算
 85             {
 86                 int g=rand()%2;
 87                 b1[i]=b[g];
 88                 int e=rand()%2;
 89                 if (e == 1)
 90                 {
 91                     a1[i]=-a1[i];
 92                 }
 93                 
 94 
 95                 if (b1[i] == '/'&&b1[i + 1] == '/')//连续除法用括号分开
 96                 {
 97                     cout << '(' << a1[i] << b1[i];
 98                 }
 99                 else
100                     if (b1[i] == '/'&&b1[i - 1] == '/'&&i>0)
101                     {
102                         cout << a1[i] << ')' << b1[i];
103                     }
104                     else
105                         cout <<a1[i]<< b1[i];
106             }
107             cout <<a1[a - 1]<< '=' << endl;
108 
109         }
110         if (d == 1 && fushu == 1)//有乘除运算,有负数
111         {
112             for (int i = 0; i < a - 1; i++)//输出运算
113             {
114                 int e=rand()%2;
115                 if (e == 1)
116                 {
117                     a1[i]=-a1[i];
118                 }
119                 
120 
121                 if (b1[i] == '/'&&b1[i + 1] == '/')//连续除法用括号分开
122                 {
123                     cout << '(' << a1[i] << b1[i];
124                 }
125                 else
126                     if (b1[i] == '/'&&b1[i - 1] == '/'&&i>0)
127                     {
128                         cout << a1[i] << ')' << b1[i];
129                     }
130                     else
131                         cout <<a1[i]<< b1[i];
132             }
133             cout <<a1[a - 1]<< '=' << endl;
134 
135         }
136         if (d == 1 && fushu == 0)//有乘除运算,无负数
137         {
138             for (int i = 0; i < a - 1; i++)//输出运算
139             {
140                 if (b1[i] == '/'&&b1[i + 1] == '/')//连续除法用括号分开
141                 {
142                     cout << '(' << a1[i] << b1[i];
143                 }
144                 else
145                     if (b1[i] == '/'&&b1[i - 1] == '/'&&i>0)
146                     {
147                         cout << a1[i] << ')' << b1[i];
148                     }
149                     else
150                         cout <<a1[i]<< b1[i];
151             }
152             cout <<a1[a - 1]<< '=' << endl;
153 
154         }
155         for (int i = 0; i < a - 1; i++)//用数组分情况计算乘除结果
156         {
157             int y = 0;
158 
159             if (b1[i] == '/'&&b1[i + 1] == '/')
160             {
161                 y = a1[i] / a1[i + 1] / a1[i + 2];
162                 a1[i] = y;
163                 a1[i + 1] = 0;
164                 a1[i + 2] = 0;
165                 b1[i] = b1[i + 1] = '+';
166 
167             }
168             else
169             if (b1[i] == '/'&&b1[i + 1] == '*')
170             {
171                 y = a1[i] / a1[i + 1] * a1[i + 2];
172                 a1[i] = y;
173                 a1[i + 1] = 0;
174                 a1[i + 2] = 0;;
175                 b1[i] = b1[i + 1] = '+';
176 
177             }
178             else
179             if (b1[i] == '*'&&b1[i + 1] == '/')
180             {
181                 y = a1[i] * a1[i + 1] / a1[i + 2];
182                 a1[i] = y;
183                 a1[i + 1] = 0;
184                 a1[i + 2] = 0;
185                 b1[i] = b1[i + 1] = '+';
186 
187             }
188             else
189             if (b1[i] == '*'&&b1[i + 1] == '*')
190             {
191                 y = a1[i] * a1[i + 1] * a1[i + 2];
192                 a1[i] = y;
193                 a1[i + 1] = 0;
194                 a1[i + 2] = 0;
195                 b1[i] = b1[i + 1] = '+';
196 
197             }
198             else
199             if (b1[i] == '/')
200             {
201                 y = a1[i] / a1[i + 1];
202                 a1[i] = y;
203                 a1[i + 1] = 0;
204                 b1[i] = '+';
205 
206             }
207             else
208             if (b1[i] == '*')
209             {
210                 y = a1[i] * a1[i + 1];
211                 a1[i] = y;
212                 a1[i + 1] = 0;
213                 b1[i] = '+';
214 
215             }
216 
217         }
218         int y1 = a1[0];
219         for (int i = 0; i < a - 1; i++)//递归计算
220         {
221             if (b1[i] == '+')
222             {
223                 y1 = y1 + a1[i + 1];
224 
225             }
226             else
227             if (b1[i] == '-')
228             {
229                 y1 = y1 - a1[i + 1];
230 
231             }
232             else
233             if (b1[i] == '*')
234             {
235                 y1 = y1 * a1[i + 1];
236 
237             }
238             else
239             if (b1[i] == '/')
240             {
241                 y1 = y1 / a1[i + 1];
242 
243             }
244 
245         }
246         int s;
247         cout << "请输入你的计算结果:";
248         cin >> s;
249         if (s == y1)
250         {
251             cout << "结果正确" << endl;
252         }
253         else
254         {
255             cout << "结果错误,正确答案为" << y1 << endl;;
256         }
257     }
258 
259         
260     }

运行结果截图:

搭档的博客号: http://www.cnblogs.com/lvstudy/ 

 

编程总结分析:

     总的来说,对这次编程还算满意,在上面花了不少的时间,相信只要有付出就会有结果。但是程序也存在很多的不足之处,比如不能很好的实现括号的功能,有时候不是负数的地方也会加上括号,另外有余数部分的功能没有怎么实现,感觉四则运算中不应该有余数,尤其是带有混合运算的式子,记着我小时侯没写余数啊。这次队友很给力,从队友身上也学到了不少东西,希望我们继续努力,大家共同进步。

posted @ 2016-03-19 22:55  apan008  阅读(255)  评论(1编辑  收藏  举报