鱼C论坛

 找回密码
 立即注册
查看: 2032|回复: 1

[技术交流] C primer plus 第8章编程练习第八题自己做的答案过于繁琐,求指教

[复制链接]
发表于 2017-10-23 17:11:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 小蒙 于 2017-10-24 09:05 编辑

图床上的图在手机版网页中刷不出来,我用手机chrome切换到桌面版就能看到了。

                               
登录/注册后可看大图


                               
登录/注册后可看大图


网上有很多答案,但是做的很粗糙,没有考虑到特殊情况,比如需要输入有效数字的时候却输入了文件结束符,我的答案包括了对任意时刻文件结束符的处理代码,当然,即使繁琐,代码本身没大的逻辑错误,无奈初学,自己感觉都过于繁琐,为了一个文件结束符,都得需要从被调函数一志追到主调函数,我相信肯定有更优雅的方式处理文件结束符,希望得到大牛的指点,谢谢。

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <string.h>
  4. #include <iso646.h>
  5. #include <ctype.h>
  6. #include <math.h>

  7. #define ZERO 0
  8. #define ADD 'a'
  9. #define SUB 's'
  10. #define MUL 'm'
  11. #define DIV 'd'


  12. char get_operator(void);

  13. float get_num(void);

  14. float add(float a, float b);

  15. float sub(float a, float b);

  16. float mul(float a, float b);

  17. float div(float a, float b);

  18. int main(void) {
  19. //    char operator;
  20.     char oper;
  21.     float first;
  22.     float second;
  23.     while ((oper = get_operator()) != 'q') {
  24.         if (EOF == oper) {
  25.             break;
  26.         }
  27.         if (oper != ADD && oper != SUB && oper != MUL && oper != DIV) {
  28.             printf("Invalid operator, try again.\n");
  29.             continue;
  30.         }
  31.         printf("Enter first num:");
  32.         first = get_num();
  33.         // get_num()出现的结束符在这里进行处理,但是结束符根本无法处理,所以只能break,直接结束程序。
  34.         if (getchar() == EOF) break;
  35.         printf("Enter second num:");
  36.         second = get_num();
  37.         // get_num()出现的结束符在这里进行处理,但是结束符根本无法处理,所以只能break,直接结束程序。
  38.         if (getchar() == EOF) break;
  39.         while ((oper == DIV) && (ZERO == second)) {
  40.             printf("Enter a num other than 0:");
  41.             second = get_num();
  42.             //同样也是结束符的处理,但是在这里break只能跳出本层while,之前second作为ZERO,现在还是0,0作为除法会进行计算
  43.             // 所以在switch前加了一层逻辑判断,如果second等于0且oper是DIV(相当于在"Enter a num other than 0:"处键入文件结束符)再次跳出最外层循环。
  44.             if (getchar() == EOF) break;
  45.             if (ZERO != second) {
  46.                 break;
  47.             }
  48.         }
  49.         // 注意,用的是非逻辑操作符
  50.         if (!(ZERO == second && oper == DIV)) {

  51.             switch (oper) {
  52.                 case ADD:
  53.                     printf("%.2f + %.2f = %.2f\n", first, second, add(first, second));
  54.                     break;
  55.                 case SUB:
  56.                     printf("%.2f - %.2f = %.2f\n", first, second, sub(first, second));
  57.                     break;
  58.                 case MUL:
  59.                     printf("%.2f * %.2f = %.2f\n", first, second, mul(first, second));
  60.                     break;
  61.                 case DIV:
  62.                     printf("%.2f / %.2f = %.2f\n", first, second, div(first, second));
  63.                     break;
  64.             }
  65.         } else {
  66.             break;
  67.         }
  68.     }
  69.     printf("Bye.\n");
  70.     return 0;
  71. }


  72. char get_operator(void) {
  73.     char c;
  74.     printf("Enter the operation of your choice:\n");
  75.     printf("%-20s%-20s\n%-20s%-20s\n%-20s\n", "a. add", "s. substract", "m. multiply", "d. divide", "q. quit");
  76.     while ((c = getchar()) != EOF) {
  77.         if (isspace(c)) {
  78.             continue;
  79.         }
  80.         while (getchar() != '\n') {
  81.             continue;
  82.         }
  83.         return c;
  84.     }
  85.     return EOF;
  86. }

  87. float get_num(void) {
  88.     float num;
  89.     char c;
  90.     int item = scanf("%f", &num);
  91.     while (item != 1) {
  92.         // 因为程序本身返回数字类型,所以不可以让他返回EOF,容易混淆,所以只能让文件结束符继续向上走,走到主调函数中。
  93.         if (EOF == item) {
  94.             break;
  95.         }
  96.         while ((c = getchar()) != '\n') {
  97.             putchar(c);
  98.         }
  99.         printf(" is not a num.\n");
  100.         printf("Please enter a num, such as 2.5, -1.78E8, or 3:");
  101.         item = scanf("%f", &num);
  102.     }
  103.     if (1 == item) {
  104.         return num;
  105.     }
  106. }

  107. float add(float a, float b) {
  108.     return a + b;
  109. }

  110. float sub(float a, float b) {
  111.     return a - b;
  112. }

  113. float mul(float a, float b) {
  114.     return a * b;
  115. }

  116. float div(float a, float b) {
  117.     return a / b;
  118. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-10-23 18:16:00 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 12:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表