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

四则运算3

时间:2016-03-19 16:18:43      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:

一、程序要求:

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

  2、程序必须能处理四种运算的混合算式;

  3、在上次程序的基础上,添加新的功能。

二、程序设计思想:

  1、在上次程序的基础之上添加要求;

  2、判断答案正误时,整数比分数容易判断(判断过程不予叙述),所以单独引入分数类,设计函数分别计算分数四则运算的结果并与输入的结果比较判断;

三、源程序代码:

  1 //李俏,张莹荧,2016.3.15
  2 //随机生成四则运算3
  3 
  4 #include<iostream>
  5 #include<stdio.h>
  6 #include<stdlib.h>
  7 #include<time.h>
  8 using namespace std;
  9 
 10 class Fenshu
 11 {
 12 private:
 13     int fenzi;  // 分子  
 14     int fenmu;  // 分母 
 15 public:
 16     Fenshu(int fz = 0, int fm = 1) :fenzi(fz), fenmu(fm){}
 17     void set(int, int);
 18     void simplify();
 19     friend bool judge1(const Fenshu &c1, const Fenshu &c2);     //判断两个分数是否相等
 20     friend bool judge2(const Fenshu &c1, int z);     //判断两个分数是否相等
 21     friend Fenshu add(const Fenshu &c1, const Fenshu &c2);  //两个分数相加,结果要化简  
 22     friend Fenshu sub(const Fenshu &c1, const Fenshu &c2);  //两个分数相减,结果要化简  
 23     friend Fenshu mul(const Fenshu &c1, const Fenshu &c2);  //两个分数相乘,结果要化简  
 24     friend Fenshu div(const Fenshu &c1, const Fenshu &c2);  //两个分数相除,结果要化简 
 25 };
 26 
 27 void Fenshu::set(int m, int n)// 分数赋值
 28 {
 29     fenzi = m;
 30     fenmu = n;
 31 }
 32 
 33 void Fenshu::simplify()// 分数化简
 34 {
 35     int m, n, r;
 36     m = abs(fenmu);
 37     n = abs(fenzi);
 38     while (r = m%n)  // 求m,n的最大公约数  
 39     {
 40         m = n;
 41         n = r;
 42     }
 43     fenmu /= n;     // 化简  
 44     fenzi /= n;
 45     if (fenmu<0)  // 将分母转化为正数  
 46     {
 47         fenmu = -fenmu;
 48         fenzi = -fenzi;
 49     }
 50 }
 51 
 52 Fenshu add(const Fenshu &c1, const Fenshu &c2)// 分数相加  
 53 {
 54     Fenshu t;
 55     t.fenzi = c1.fenzi*c2.fenmu + c2.fenzi*c1.fenmu;
 56     t.fenmu = c1.fenmu*c2.fenmu;
 57     t.simplify();
 58     return t;
 59 }
 60 
 61 Fenshu sub(const Fenshu &c1, const Fenshu &c2)// 分数相减 
 62 {
 63     Fenshu t;
 64     t.fenzi = c1.fenzi*c2.fenmu - c2.fenzi*c1.fenmu;
 65     t.fenmu = c1.fenmu*c2.fenmu;
 66     t.simplify();
 67     return t;
 68 }
 69 
 70 Fenshu mul(const Fenshu &c1, const Fenshu &c2)// 分数相乘
 71 {
 72     Fenshu t;
 73     t.fenzi = c1.fenzi*c2.fenzi;
 74     t.fenmu = c1.fenmu*c2.fenmu;
 75     t.simplify();
 76     return t;
 77 }
 78 
 79 Fenshu div(const Fenshu &c1, const Fenshu &c2)// 分数相除 
 80 {
 81     Fenshu t;
 82     if (!c2.fenzi) return c1;
 83     t.fenzi = c1.fenzi*c2.fenmu;
 84     t.fenmu = c1.fenmu*c2.fenzi;
 85     t.simplify();
 86     return t;
 87 }
 88 
 89 bool judge1(const Fenshu &c1, const Fenshu &c2)//判断正误
 90 {
 91     bool flag;
 92     if ((c1.fenzi == c2.fenzi) && (c1.fenmu == c2.fenmu))
 93     {
 94         flag = true;
 95         return flag;
 96     }
 97     else
 98     {
 99         flag = false;
100         return flag;
101     }
102 }
103 
104 bool judge2(const Fenshu &c1, int z)//判断正误
105 {
106     bool flag;
107     if (0 == c1.fenzi - z)
108     {
109         flag = true;
110         return flag;
111     }
112     else
113     {
114         flag = false;
115         return flag;
116     }
117 }
118 
119 void main()
120 {
121     int first, second, firstm, secondm;
122     int sign, i, j;
123     int num, chengchu, fushu, yushu, fanwei;
124     int ans, ansm, ansy;
125     int right = 0, wrong = 0;
126     Fenshu x1, x2, x, y;
127 
128     cout << "请输入题目数量:";
129     cin >> num;
130     if (num == 0)
131     {
132         exit(1);
133     }
134 
135     cout << "是否有乘除法?1.是,2.否:";
136     cin >> chengchu;
137 
138     if (chengchu == 1)
139     {
140         cout << "除法是否有余数?1.是,2.否:";
141         cin >> yushu;
142     }
143 
144     cout << "减法是否有负数?1.是,2.否:";
145     cin >> fushu;
146 
147     cout << "请输入数值范围:";
148     cin >> fanwei;
149 
150     srand((int)time(NULL)); //用时间做种子,每次产生随机数都不一样
151 
152     for (i = 0; i<num; i++)
153     {
154         j = rand() % 2;        //选择生成整数运算还是分数运算
155 
156         if (j == 0)           //选择整数
157         {
158             first = rand() % (fanwei + 1);
159             second = rand() % (fanwei + 1);
160             if (chengchu == 1)
161             {
162                 sign = rand() % 4;
163             }
164             else
165             {
166                 sign = rand() % 2;
167             }
168             switch (sign)
169             {
170             case 0:                      //整数加法
171                 cout << first << "+" << second << "=" << endl;
172                 cout << "请输入结果:";
173                 cin >> ans;
174                 if (ans == first + second)
175                 {
176                     cout << "答对了!" << endl;
177                     right = right + 1;
178                 }
179                 else
180                 {
181                     cout << "答错了!" << endl;
182                     wrong = wrong + 1;
183                 }
184                 break;
185             case 1:                      //整数减法
186                 if (fushu == 1)
187                 {
188                     cout << first << "-" << second << "=" << endl;
189                     cout << "请输入结果:";
190                     cin >> ans;
191                     if (ans == first - second)
192                     {
193                         cout << "答对了!" << endl;
194                         right = right + 1;
195                     }
196                     else
197                     {
198                         cout << "答错了!" << endl;
199                         wrong = wrong + 1;
200                     }
201                 }
202                 else
203                 {
204                     if (first>second)
205                     {
206                         cout << first << "-" << second << "=" << endl;
207                         cout << "请输入结果:";
208                         cin >> ans;
209                         if (ans == first - second)
210                         {
211                             cout << "答对了!" << endl;
212                             right = right + 1;
213                         }
214                         else
215                         {
216                             cout << "答错了!" << endl;
217                             wrong = wrong + 1;
218                         }
219                     }
220                     else
221                     {
222                         cout << second << "-" << first << "=" << endl;
223                         cout << "请输入结果:";
224                         cin >> ans;
225                         if (ans == second - first)
226                         {
227                             cout << "答对了!" << endl;
228                             right = right + 1;
229                         }
230                         else
231                         {
232                             cout << "答错了!" << endl;
233                             wrong = wrong + 1;
234                         }
235                     }
236                 }
237                 break;
238             case 2:                   //整数乘法
239                 cout << first << "*" << second << "=" << endl;
240                 cout << "请输入结果:";
241                 cin >> ans;
242                 if (ans == first * second)
243                 {
244                     cout << "答对了!" << endl;
245                     right = right + 1;
246                 }
247                 else
248                 {
249                     cout << "答错了!" << endl;
250                     wrong = wrong + 1;
251                 }
252                 break;
253             case 3:                   //整数除法
254                 if (yushu == 1)
255                 {
256                     if (second != 0)
257                     {
258                         cout << first << "/" << second << "=" << endl;
259                         cout << "请输入结果(商和余数):";
260                         cin >> ans >> ansy;
261                         if ((ans == (first / second)) && (ansy == (first%second)))
262                         {
263                             cout << "答对了!" << endl;
264                             right = right + 1;
265                         }
266                         else
267                         {
268                             cout << "答错了!" << endl;
269                             wrong = wrong + 1;
270                         }
271                     }
272                     else
273                     {
274                         i = i - 1;
275                     }
276                 }
277                 else
278                 {
279                     if (second != 0 && (first%second == 0))
280                     {
281                         cout << first << "/" << second << "=" << endl;
282                         cout << "请输入结果:";
283                         cin >> ans;
284                         if (ans == first / second)
285                         {
286                             cout << "答对了!" << endl;
287                             right = right + 1;
288                         }
289                         else
290                         {
291                             cout << "答错了!" << endl;
292                             wrong = wrong + 1;
293                         }
294                     }
295                     else
296                     {
297                         i = i - 1;
298                     }
299                 }
300                 break;
301             }
302         }
303 
304         else              //选择分数
305         {
306             first = rand() % (fanwei + 1);
307             second = rand() % (fanwei + 1);//分子
308             firstm = rand() % (fanwei + 1);
309             secondm = rand() % (fanwei + 1);//分母
310             if (chengchu == 1)
311             {
312                 sign = rand() % 4;
313             }
314             else
315             {
316                 sign = rand() % 2;
317             }
318 
319             switch (sign)
320             {
321             case 0:                //分数加法
322                 if ((firstm != 0) && (secondm != 0) && (first<firstm) && (second<secondm))
323                 {
324                     cout << first << "/" << firstm << "  +  " << second << "/" << secondm << "=" << endl;
325                     cout << "请输入结果(分子和分母):";
326                     cin >> ans >> ansm;
327                     x.set(ans, ansm);
328                     x1.set(first, firstm);
329                     x2.set(second, secondm);
330                     y = add(x1, x2);
331                     if (true == judge1(x, y))
332                     {
333                         cout << "答对了!" << endl;
334                         right = right + 1;
335                     }
336                     else
337                     {
338                         cout << "答错了!" << endl;
339                         wrong = wrong + 1;
340                     }
341                 }
342                 else
343                 {
344                     i = i - 1;
345                 }
346                 break;
347             case 1:                //分数减法
348                 if (fushu == 1)
349                 {
350                     if ((firstm != 0) && (secondm != 0) && (first<firstm) && (second<secondm))
351                     {
352                         cout << first << "/" << firstm << "  -  " << second << "/" << secondm << "=" << endl;
353                         cout << "请输入结果(分子和分母):";
354                         cin >> ans;
355                         if (ans != 0)
356                         {
357                             cin >> ansm;
358                             x.set(ans, ansm);
359                             x1.set(first, firstm);
360                             x2.set(second, secondm);
361                             y = sub(x1, x2);
362                             if (true == judge1(x, y))
363                             {
364                                 cout << "答对了!" << endl;
365                                 right = right + 1;
366                             }
367                             else
368                             {
369                                 cout << "答错了!" << endl;
370                                 wrong = wrong + 1;
371                             }
372                         }
373                         else
374                         {
375                             x1.set(first, firstm);
376                             x2.set(second, secondm);
377                             y = sub(x1, x2);
378                             if (true == judge2(y, ans))
379                             {
380                                 cout << "答对了!" << endl;
381                                 right = right + 1;
382                             }
383                             else
384                             {
385                                 cout << "答错了!" << endl;
386                                 wrong = wrong + 1;
387                             }
388                         }
389                     }
390                     else
391                     {
392                         i = i - 1;
393                     }
394                 }
395                 else
396                 {
397                     if ((firstm != 0) && (secondm != 0) && (first<firstm) && (second<secondm) && ((first / firstm)>(second / secondm)))
398                     {
399                         cout << first << "/" << firstm << "  -  " << second << "/" << secondm << "=" << endl;
400                         cout << "请输入结果(分子和分母):";
401                         cin >> ans;
402                         if (ans != 0)
403                         {
404                             cin >> ansm;
405                             x.set(ans, ansm);
406                             x1.set(first, firstm);
407                             x2.set(second, secondm);
408                             y = sub(x1, x2);
409                             if (true == judge1(x, y))
410                             {
411                                 cout << "答对了!" << endl;
412                                 right = right + 1;
413                             }
414                             else
415                             {
416                                 cout << "答错了!" << endl;
417                                 wrong = wrong + 1;
418                             }
419                         }
420                         else
421                         {
422                             x1.set(first, firstm);
423                             x2.set(second, secondm);
424                             y = sub(x1, x2);
425                             if (true == judge2(y, ans))
426                             {
427                                 cout << "答对了!" << endl;
428                                 right = right + 1;
429                             }
430                             else
431                             {
432                                 cout << "答错了!" << endl;
433                                 wrong = wrong + 1;
434                             }
435                         }
436                     }
437                     else
438                     {
439                         i = i - 1;
440                     }
441                 }
442                 break;
443             case 2:                //分数乘法
444                 if ((firstm != 0) && (secondm != 0) && (first<firstm) && (second<secondm))
445                 {
446                     cout << first << "/" << firstm << "  *  " << second << "/" << secondm << "=" << endl;
447                     cout << "请输入结果(分子和分母):";
448                     cin >> ans;
449                     if (ans != 0)
450                     {
451                         cin >> ansm;
452                         x.set(ans, ansm);
453                         x1.set(first, firstm);
454                         x2.set(second, secondm);
455                         y = sub(x1, x2);
456                         if (true == judge1(x, y))
457                         {
458                             cout << "答对了!" << endl;
459                             right = right + 1;
460                         }
461                         else
462                         {
463                             cout << "答错了!" << endl;
464                             wrong = wrong + 1;
465                         }
466                     }
467                     else
468                     {
469                         x1.set(first, firstm);
470                         x2.set(second, secondm);
471                         y = sub(x1, x2);
472                         if (true == judge2(y, ans))
473                         {
474                             cout << "答对了!" << endl;
475                             right = right + 1;
476                         }
477                         else
478                         {
479                             cout << "答错了!" << endl;
480                             wrong = wrong + 1;
481                         }
482                     }
483                 }
484                 else
485                 {
486                     i = i - 1;
487                 }
488                 break;
489             case 3:                //分数除法
490                 if ((firstm != 0) && (secondm != 0) && (first != 0) && (second != 0) && (first<firstm) && (second<secondm))
491                 {
492                     cout << first << "/" << firstm << "  /  " << second << "/" << secondm << "=" << endl;
493                     cout << "请输入结果(分子和分母):";
494                     cin >> ans;
495                     if (ans != 0)
496                     {
497                         cin >> ansm;
498                         x.set(ans, ansm);
499                         x1.set(first, firstm);
500                         x2.set(second, secondm);
501                         y = sub(x1, x2);
502                         if (true == judge1(x, y))
503                         {
504                             cout << "答对了!" << endl;
505                             right = right + 1;
506                         }
507                         else
508                         {
509                             cout << "答错了!" << endl;
510                             wrong = wrong + 1;
511                         }
512                     }
513                     else
514                     {
515                         x1.set(first, firstm);
516                         x2.set(second, secondm);
517                         y = sub(x1, x2);
518                         if (true == judge2(y, ans))
519                         {
520                             cout << "答对了!" << endl;
521                             right = right + 1;
522                         }
523                         else
524                         {
525                             cout << "答错了!" << endl;
526                             wrong = wrong + 1;
527                         }
528                     }
529                 }
530                 else
531                 {
532                     i = i - 1;
533                 }
534                 break;
535             }
536         }
537     }
538     cout << "总共完成" << num << "道题,做对" << right << "道,做错" << wrong << "道题!" << endl;
539 }

四、运行结果截图:

  技术分享

  技术分享

五、项目计划日志:

          周活动总结表

姓名:张莹荧                                      日期:2016-03-19

日期/任务    听课     编写程序    阅读课本   准备考试                        日总计  
周日3.13   60 120 120     300
周一 300 30 30 120     480
周二 300 30 30  120     453
周三 200 180   180     560
周四 300     120     420
周五 100 180 30       310
周六    60          60
周总结 1200 540 210 660     2610

 

 

 

 

 

 

 

 

 

 

阶段时间和效率                      周数:2

不包括上一周在内的累计时间

总计                     
平均              
最大              
最小              

 

 

 

 

 

以前各周的累计时间

总计 1200 540 210 660       2610
平均 1200 540 210 660     2610
最大 1200 540 210 660     2610
最小 1200 540 210 660     2610

 

 

 

 

 

六、时间记录日志:

  学生:张莹荧                                              日期:2016-03-19

  教师:王建民                                              课程: PSP  

日期 开始时间 结束时间 中断时间 净时间 活动 备注
3.16

14:30

16:10

16:00

17:40

0

0

90

90

编程

阅读课本

 中间上网、休息
3.17

8:00

14:00

12:00

15:50

40

10

200

100

上课 课间休息
3.18

14:30

19:00

17:40

19:30

10

0

180

30

编程

阅读课本

中间休息
3.19

12:30

19:00

13:30

19:30

0

0

60

30

编程

阅读课本

 

 

 

 

 

 

 

 

 

 

 

 

七、缺陷记录日志:

  学生:  张莹荧                                                          

  日期: 2016-03-12 

  教员:  王建民                                                                           

  程序号:  2  

 日期  编号 类型 引入阶段 排除阶段 修复时间 修复缺陷
 3.16  1 计算 设计 编译 30min  
  描述:分数部分判断正误时,分数该怎样计算结果。通过分数化简,通分等过程解决。
 3.17  2 类与对象 编码 编译 5min  
描述:在引用类中的变量时,出现错误。
 3.18  3 余数 编码 编译 5min  
描述:在整数除法中,需加上余数的正误判断。
 3.19  4 答案 编码 编译 10min  
  描述:分数的减、乘、除运算,若结果为0,则出错。通过先判断分子是否为0来解决,若为0,则不需输入分母,若不为0,则需要继续输入分母并判断。

 

 
 
 
 
 
 
 
 
 
 
 
 
 
八、结对开发队友:
  李俏 信1301-2班 20132912
  博客地址:http://www.cnblogs.com/Aliqiao/
  工作照:
  技术分享
  技术分享
     

 

四则运算3

标签:

原文地址:http://www.cnblogs.com/zhyying/p/5295278.html

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