zoukankan      html  css  js  c++  java
  • 四则运算03

    题目要求:

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

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

      3.要求两人合作分析,单独编程,单独撰写博客

    团队成员:胡浩特、朱子嘉(http://www.cnblogs.com/0jiajia1/

    源代码:

      1 //四则运算3
      2 //朱子嘉、胡浩特 2015/3/17
      3 
      4 #include<iostream>
      5 using namespace std;
      6 
      7 #include<stdlib.h>
      8 #include<time.h>
      9 #include<math.h>
     10 int display(int number, int mul, int num, int neg, int remainder)//打印方式控制输出列数
     11 {
     12     int *a = new int[number];
     13     int *b = new int[number];
     14     int *c = new int[number];
     15     int *d = new int[number];
     16     int *e = new int[number];
     17 
     18     int m;//控制题目避免重复
     19     int t;//中间变量
     20     int n1 = 0;//保存正确题目数量
     21     int n2 = 0;//保存错误题目数量
     22     double result;//用户输入的题目结果
     23     for (int i = 0; i<number; i++)//随机生成运算操作数
     24     {
     25         m = 1;//初始化
     26         a[i] = rand() % num;
     27         b[i] = rand() % num;
     28         d[i] = rand() % num;
     29         e[i] = rand() % num;
     30         if (mul == 0)//没有乘除法
     31         {
     32             c[i] = rand() % 2;//随机生成0-1的数字,分别表示加减
     33         }
     34         else if (mul == 1)//有乘除法
     35         {
     36             c[i] =rand() % 8;//随机生成0-7的数字,分别表示不同运算符号组合
     37         }
     38         for (int j = 0; j<i; j++)
     39         {
     40             if (a[j] == a[i] && b[j] == b[i] && c[j] == c[i] && d[j] == d[i] && e[j] == e[i])//比较新生成的操作数与原来的是否相同
     41             {
     42                 i = i - 1;
     43                 m = 0;
     44             }
     45         }
     46         while (m)//若不同则输出
     47         {
     48             switch (c[i])
     49             {
     50             case 0:
     51                 cout << a[i] << "+" << b[i] << "=" << endl;
     52                 cout << "请输入结果:" << endl;
     53                 cin >> result;
     54                 if (result == a[i] + b[i])
     55                 {
     56                     cout << "回答正确!" << endl;
     57                     n1++;
     58                 }
     59                 else
     60                 {
     61                     cout << "回答错误!" << endl;
     62                     cout << "正确结果为:" << a[i] + b[i] << endl;
     63                     n2++;
     64                 }
     65                 break;
     66             case 1:
     67                 if (neg == 0)//减法没有负数
     68                 {
     69                     if (a[i]<b[i])
     70                     {
     71                         t = a[i];
     72                         a[i] = b[i];
     73                         b[i] = t;
     74                     }
     75                     cout << a[i] << "-" << b[i] << "=" << endl;
     76                     cout << "请输入结果:" << endl;
     77                     cin >> result;
     78                     if (result == a[i] - b[i])
     79                     {
     80                         cout << "回答正确!" << endl;
     81                         n1++;
     82                     }
     83                     else
     84                     {
     85                         cout << "回答错误!" << endl;
     86                         cout << "正确结果为:" << a[i] - b[i] << endl;
     87                         n2++;
     88                     }
     89                     break;
     90                 }
     91 
     92                 else if (neg == 1)//减法有负数
     93                 {
     94                     cout << a[i] << "-" << b[i] << "=" << endl;
     95                     cout << "请输入结果:" << endl;
     96                     cin >> result;
     97                     if (result == a[i] - b[i])
     98                     {
     99                         cout << "回答正确!" << endl;
    100                         n1++;
    101                     }
    102                     else
    103                     {
    104                         cout << "回答错误!" << endl;
    105                         cout << "正确结果为:" << a[i] - b[i] << endl;
    106                         n2++;
    107                     }
    108                     break;
    109                 }
    110             case 2:
    111                 cout << a[i] << "+" << b[i] << "*" << d[i] << "-" << e[i] << "=" << endl;
    112                 cout << "请输入结果:" << endl;
    113                 cin >> result;
    114                 if (result == a[i] + b[i] * d[i] - e[i])
    115                 {
    116                     cout << "回答正确!" << endl;
    117                     n1++;
    118                 }
    119                 else
    120                 {
    121                     cout << "回答错误!" << endl;
    122                     cout << "正确结果为:" << a[i] + b[i] * d[i] - e[i] << endl;
    123                     n2++;
    124                 }
    125                 break;
    126             case 3:
    127                 cout << a[i] << "+" << b[i] << "-" << d[i] << "-" << e[i] << "=" << endl;
    128                 cout << "请输入结果:" << endl;
    129                 cin >> result;
    130                 if (result == a[i] + b[i] - d[i] - e[i])
    131                 {
    132                     cout << "回答正确!" << endl;
    133                     n1++;
    134                 }
    135                 else
    136                 {
    137                     cout << "回答错误!" << endl;
    138                     cout << "正确结果为:" << a[i] + b[i] - d[i] - e[i] << endl;
    139                     n2++;
    140                 }
    141                 break;
    142             case 4:
    143                 cout << a[i] << "+" << b[i] << "+" << d[i] << "-" << e[i] << "=" << endl;
    144                 cout << "请输入结果:" << endl;
    145                 cin >> result;
    146                 if (result == a[i] + b[i] + d[i] - e[i])
    147                 {
    148                     cout << "回答正确!" << endl;
    149                     n1++;
    150                 }
    151                 else
    152                 {
    153                     cout << "回答错误!" << endl;
    154                     cout << "正确结果为:" << a[i] + b[i] + d[i] - e[i] << endl;
    155                     n2++;
    156                 }
    157                 break;
    158             case 5:
    159                 if (neg == 0)//结果没有负数
    160                 {
    161                     if (a[i]<b[i])
    162                     {
    163                         t = a[i];
    164                         a[i] = b[i];
    165                         b[i] = t;
    166                     }
    167                     cout << a[i] << "-" << b[i] << "*" << c[i] << "+" << d[i] << "/" << e[i] << "=" << endl;
    168                     cout << "请输入结果:" << endl;
    169                     cin >> result;
    170                     if (result == a[i] - b[i] * c[i] + d[i] / e[i])
    171                     {
    172                         cout << "回答正确!" << endl;
    173                         n1++;
    174                     }
    175                     else
    176                     {
    177                         cout << "回答错误!" << endl;
    178                         cout << "正确结果为:" << a[i] - b[i] * c[i] + d[i] / e[i] << endl;
    179                         n2++;
    180                     }
    181                     break;
    182                 }
    183 
    184                 else if (neg == 1)//结果有负数
    185                 {
    186                     cout << a[i] << "-" << b[i] << "*" << c[i] << "+" << d[i] << "/" << e[i] << "=" << endl;
    187                     cout << "请输入结果:" << endl;
    188                     cin >> result;
    189                     if (result == a[i] - b[i] * c[i] + d[i] / e[i])
    190                     {
    191                         cout << "回答正确!" << endl;
    192                         n1++;
    193                     }
    194                     else
    195                     {
    196                         cout << "回答错误!" << endl;
    197                         cout << "正确结果为:" << a[i] - b[i] * c[i] + d[i] / e[i] << endl;
    198                         n2++;
    199                     }
    200                     break;
    201                 }
    202 
    203             case 6:
    204                 cout << a[i] << "*" << b[i] << "*" << d[i] << "/" << e[i] << "=" << endl;
    205                 cout << "请输入结果:" << endl;
    206                 cin >> result;
    207                 if (result == a[i] * b[i] * d[i] / e[i])
    208                 {
    209                     cout << "回答正确!" << endl;
    210                     n1++;
    211                 }
    212                 else
    213                 {
    214                     cout << "回答错误!" << endl;
    215                     cout << "正确结果为:" << a[i] * b[i] * d[i] / e[i] << endl;
    216                     n2++;
    217                 }
    218                 break;
    219             case 7:
    220                 if (b[i] == 0)//分母为零则不计入总数
    221                 {
    222                     i = i - 1; break;
    223                 }
    224                 else if (remainder == 0)//除法没有余数
    225                 {
    226                     if (a[i] % b[i] == 0)
    227                     {
    228                         cout << a[i] << "/" << b[i] << "=" << endl;
    229                         cout << "请输入结果:" << endl;
    230                         cin >> result;
    231                         if (result == a[i] / b[i])
    232                         {
    233                             cout << "回答正确!" << endl;
    234                             n1++;
    235                         }
    236                         else
    237                         {
    238                             cout << "回答错误!" << endl;
    239                             cout << "正确结果为:" << a[i] / b[i] << endl;
    240                             n2++;
    241                         }
    242                         break;
    243                     }
    244                     else
    245                     {
    246                         i = i - 1; break;
    247                     }
    248                 }
    249                 else if (remainder == 1)//除法有余数
    250                 {
    251                     if (a[i] % b[i] != 0)
    252                     {
    253                         cout << a[i] << "/" << b[i] << "=" << endl;
    254                         cout << "请输入结果:" << endl;
    255                         cin >> result;
    256                         if (result == (double)a[i] / b[i])
    257                         {
    258                             cout << "回答正确!" << endl;
    259                             n1++;
    260                         }
    261                         else
    262                         {
    263                             cout << "回答错误!" << endl;
    264                             cout << "正确结果为:" << (double)a[i] / b[i] << endl;
    265                             n2++;
    266                         }
    267                         break;
    268                     }
    269                     else
    270                     {
    271                         i = i - 1; break;
    272                     }
    273                 }
    274 
    275             }
    276             break;//跳出循环
    277         }
    278     }
    279     cout << "您一共答对" << n1 << "道题目" << endl;
    280     cout << "您一共答错" << n2 << "道题目" << endl;
    281     return 0;
    282 }
    283 void main()
    284 {
    285     int number;//题目数量
    286     int mul;//乘除法
    287     int num;//数值范围
    288     int neg;//负数
    289     int remainder;//余数
    290     int c;//循环变量
    291     
    292         srand((unsigned)time(NULL));//调用随机函数发生器
    293         cout << "---------------------------------" << endl;
    294         cout << "|            四则运算            |" << endl;
    295         cout << "---------------------------------" << endl;
    296         cout << "请输入要打印的题目数量:" << endl;
    297         cin >> number;
    298 
    299         while (number<0)
    300         {
    301         cout << "输入错误!" << endl;
    302         cout << "请输入要打印的题目数量:" << endl;
    303         cin >> number;
    304         }
    305 
    306         cout << "是否有乘除法(0表示没有;1表示有)" << endl;
    307         cin >> mul;
    308         while (mul != 0 && mul != 1)
    309         {
    310             cout << "输入错误,请重新输入!是否有乘除法(0表示没有;1表示有)" << endl;
    311             cin >> mul;
    312         }
    313         if (mul == 1)
    314         {
    315             cout << "除法有无余数(0表示没有;1表示有)" << endl;
    316             cin >> remainder;
    317             while (remainder != 0 && remainder != 1)
    318             {
    319                 cout << "输入错误,请重新输入!除法有无余数(0表示没有;1表示有)" << endl;
    320                 cin >> remainder;
    321             }
    322         }
    323         cout << "请输入正整数的数值范围(即最大数):" << endl;
    324         cin >> num;
    325         cout << "减法有无负数(0表示没有;1表示有)" << endl;
    326         cin >> neg;
    327         while (neg != 0 && neg != 1)
    328         {
    329             cout << "输入错误,请重新输入!" << endl;
    330             cout << "减法有无负数(0表示没有;1表示有)" << endl;
    331             cin >> neg;
    332         }
    333         display(number, mul, num, neg, remainder);
    334         cout << "继续请输入1,退出请输入0" << endl;
    335         cin >> c;
    336 }

    结果截图:

    设计思路:

    本次实验的主要功是:能够判定用户的输入答案是否正确和能够处理四种运算的混合运算。这两个基本要求基本都可以实现,虽然实现的方法比较笨重,就是各自分别计算结构,然后再判断是否正确。这次实验没有实现括号的功能,本想运用递归的思想,把两个操作数的运算看成一个操作数,然后再参与运算,直到满足功能需求,但是这个想法没有实现,还有一个问题就是:括号问题,不知道怎么用合适的方法把括号添加到运算式中,最后也没有想到比较合适的方法。总之,这次实验还是很费力的,花费的时间也很长,但是自己也没有比较好的完成。

    工作照片:

    周活动总结表:

    日期/任务 听课 编写程序 阅读课本 准备考试 日总计
    周日   60 60   120
    周一 100    60   160
    周二     100   100
    周三   100 60   160
    周四 100 60     160
    周五     120   120
    周六   240 100   340
    周总结 200 460 500   1160

    时间记录日志:  

     

    日期 开始时间 结束时间 中断时间 净时间 活动 备注
    3.14 19:00 9:30 30 120

    构建之法

    数据结构

     
    3.16

    2:00

    4:30

    4:00

    6:00

    120

    90

    编写程序

    阅读

     
    3.18

    4:30

    7:30

    6:30

    9:30

    30

    15

    90

    105

    阅读

    编写程序

     
    3.19

    9:00

    2:00

    11:30

    5:30

    30

    30

    120

    180

    编写程序

    撰写博客

     
                 
                 

    缺陷记录日志: 

    日期 编号 类型 引入阶段 排除阶段 修复时间 修复缺陷
    3.14 1 范围 设计 编译 10min  
    描述:随机数范围输入时,出现错误。
    3.16 2 循环 编码 编译 5min  
    描述:在打印题目时,循环打印出现问题。
    3.18 3 余数、乘除法 编码 编译 25min  
    描述:在选择有无余数时,不能出现有余数的式子;乘除出现编译终止的问题
    3.19 4 整合 编码 编译 5min  
    描述:使用switch语句来进行各项的选择时没整和好
  • 相关阅读:
    Redis作者谈Redis应用场景(转)
    程序员必读书籍及导读指南(转)
    Python迭代器包itertools(转)
    Flash 0day漏洞(CVE-2018-4878)复现
    第二届“强网杯”全国网络安全挑战赛来袭——线上赛
    网站漏洞——文件判断函数的安全风险(实战篇)
    Android逆向进阶——让你自由自在脱壳的热身运动(dex篇)
    【python入门】之教你编写自动获取金币脚本
    Python大法之从火车余票查询到打造抢Supreme神器
    读DEDECMS找后台目录有感
  • 原文地址:https://www.cnblogs.com/JYQ-hu/p/5296109.html
Copyright © 2011-2022 走看看