3.17 四则运算作业

作业:1759137谢汉森       1759121蔡旭洋

 

下面是作业情况:

首先先是确定代码需求,确定用C的环境编译完成。

首先先把基本的只有一个运算符的程序编译完成

编译完成,顺利通过。然后开始讨论第二个运算符的加入。

另一位同伴在做的时候,我们共同讨论第二个运算符加入的情况,发现有些运算过程出现输出0的错误结果。于是进行排查。

排查结果是在随机产生随机数时,会有0的出现,x = rand() % 100 ;


查阅资料和读懂后,后来更正为 x = rand() % 100 + 1;

但是还是会出现计算结果为0的状况。进行再次排查的时候我发现是由于除法出现问题。

在原算法中,  sum = x +  y / z;       这样子会导致 y / z 的时候,y 比 z 小的时候整除出现0。

所以后面更改为  sum = x + 1.0 * y / z;

后续程序没有出错。到了考虑循环出结果的地步,发现我们都不能用循环做出结果,不知道是根本不能还是自己能力不足,于是选择放弃循环结构,改为switch选择结构。

因为是要两个运算符的运算,于是写了16个case满足题目要求,再在结构中增加定义筛选1~100的结果,编译完成,成功运行出结果!

 

这次组队编程最大的收获就是能大家一起讨论,吸取别人的教训和经验,更能发现自己的不足之处,也锻炼了自己刨根问底的能力。

最大的观点就是,组队编程比个人编程能更清晰看到自己没有注意到的问题,也会提出一些以前自己不懂的观点,而不是存留在能用就好的观念。

下面就是最终成果的源代码以及成品截图

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

int main(){

    int x, y, z, n, sum, num,shuru=0;

    srand(time(NULL));

    printf("输入需要多少道题目:");

    scanf_s("%d", &num);

 

    while (shuru < num) {

         x = rand() % 100 + 1;

         y = rand() % 100 + 1;

         z = rand() % 100 + 1;

         n = rand() % 16;

         switch (n)

         {

         case 0:

             sum = x + y + z;

             if (sum > 100 || sum < 0) break;

             else

             {

                  printf("%d + %d + %d = %d \n", x, y, z, sum);

                  shuru++;

                  break;

             }

 

 

 

         case 1:

             sum = x + y - z;

 

             if (sum > 100 || sum < 0) break;

             else

             {

                  printf("%d + %d - %d = %d \n", x, y, z, sum);

                  shuru++;

                  break;

             }

 

         case 2:

             sum = x + y * z;

 

             if (sum > 100 || sum < 0) break;

             else

             {

                  printf("%d + %d * %d = %d \n", x, y, z, sum);

                  shuru++;

                  break;

             }

 

 

         case 3:

             sum = x + 1.0 * y / z;

 

             if (sum > 100 || sum < 0) break;

             else

             {

                  printf("%d + %d / %d = %d \n", x, y, z, sum);

                  shuru++;

                  break;

             }

 

 

         case 4:

             sum = x - y + z;

 

             if (sum > 100 || sum < 0) break;

             else

             {

                  printf("%d - %d + %d = %d \n", x, y, z, sum);

                  shuru++;

                  break;

             }

 

 

         case 5:

             sum = x - y - z;

 

             if (sum > 100 || sum < 0) break;

             else

             {

                  printf("%d - %d - %d = %d \n", x, y, z, sum);

                  shuru++;

                  break;

             }

 

         case 6:

             sum = x - y * z;

 

             if (sum > 100 || sum < 0) break;

             else

             {

                  printf("%d - %d * %d = %d \n", x, y, z, sum);

                  shuru++;

                  break;

             }

 

 

         case 7:

             sum = x - 1.0 * y / z;

 

             if (sum > 100 || sum < 0) break;

             else

             {

                  printf("%d - %d / %d = %d \n", x, y, z, sum);

                  shuru++;

                  break;

             }

 

         case 8:

             sum = x + y + z;

 

             if (sum > 100 || sum < 0) break;

             else

             {

                  printf("%d * %d + %d = %d \n", x, y, z, sum);

                  shuru++;

                  break;

             }

 

         case 9:

             sum = x * y - z;

 

             if (sum > 100 || sum < 0) break;

             else

             {

                  printf("%d * %d - %d = %d \n", x, y, z, sum);

                  shuru++;

                  break;

             }

 

         case 10:

             sum = x + y * z;

 

             if (sum > 100 || sum < 0) break;

             else

             {

                  printf("%d * %d * %d = %d \n", x, y, z, sum);

                  shuru++;

                  break;

             }

 

 

         case 11:

             sum = x * 1.0 * y / z;

 

             if (sum > 100 || sum < 0) break;

             else

             {

                  printf("%d * %d / %d = %d \n", x, y, z, sum);

                  shuru++;

                  break;

             }

 

         case 12:

             sum = 1.0 * x / y + z;

 

             if (sum > 100 || sum < 0) break;

             else

             {

                  printf("%d / %d + %d = %d \n", x, y, z, sum);

                  shuru++;

                  break;

             }

 

         case 13:

             sum = 1.0 * x / y - z;

 

             if (sum > 100 || sum < 0) break;

             else

             {

                  printf("%d / %d - %d = %d \n", x, y, z, sum);

                  shuru++;

                  break;

             }

 

         case 14:

             sum = 1.0 * x / y * z;

 

             if (sum > 100 || sum < 0) break;

             else

             {

                  printf("%d / %d * %d = %d \n", x, y, z, sum);

                 shuru++;

                  break;

             }

            

 

         case 15:

             sum = 1.0 * x / 1.0 * y / z;

 

             if (sum > 100 || sum < 0) break;

             else

             {

                  printf("%d / %d / %d = %d \n", x, y, z, sum);

                  shuru++;

                  break;

             }

         }

    }

}

运算结果太长只截取部分截图

 

posted on 2019-03-17 22:41  katycat  阅读(101)  评论(0编辑  收藏  举报

导航