鱼C论坛

 找回密码
 立即注册
查看: 1486|回复: 3

课后作业s1e14的动手题第二个的进阶题

[复制链接]
发表于 2021-10-22 12:42:54 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 如果孤独感 于 2021-10-22 13:07 编辑

输出时出现只能输出两行就退出的情况;
在while的最后加getchar();也得不到改善;
这是什么情况?

  1. int main()
  2. {
  3.         float ch, sum = 0, d, number, x = 0;
  4.         bool dian, isOdd = 1;

  5.         while (isOdd)
  6.         {
  7.                 printf("请输入合法的数字:");
  8.                 for (d = 1, dian = 0, number = 0; (ch = getchar()) != '\n';)
  9.                 {
  10.                         if (dian)
  11.                         {
  12.                                 d /= 10;  //实现小数点后的分数
  13.                         }
  14.                         if (ch == '.')
  15.                         {
  16.                                 dian = 1;
  17.                         }
  18.                         if (ch >= '0' && ch <= '9')
  19.                         {
  20.                                 number += 1;
  21.                                 sum += (ch - '0')*d; //数字和
  22.                         }
  23.                         else if (number > 0 && !(ch >= '0' && ch <= '9') && ch != '.')  //让程序能以任意字符为数字间隔
  24.                         {
  25.                                 dian = 0;
  26.                                 number = 0;
  27.                         }
  28.                         else
  29.                         {
  30.                                 isOdd = 0;  //退出while循环
  31.                         }
  32.                 }
  33.         }

  34.         printf("结果是:%.2f\n", sum);
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-10-22 12:55:31 | 显示全部楼层
你这代码是实现什么功能的?
  1. #include <stdio.h>
  2. #include <math.h>
  3. int main()
  4. {
  5.         float ch, sum = 0, d, number, x = 0;
  6.         bool dian, isOdd = 1;

  7.         while (isOdd)
  8.         {
  9.                 printf("请输入合法的数字:");
  10.                 for (d = 1, dian = 0, number = 0; (ch = getchar() )!= '\n';)
  11.                 {
  12.                         if (dian)
  13.                         {
  14.                                 d /= 10;  //实现小数点后的分数
  15.                         }
  16.                         if (ch == '.')
  17.                         {
  18.                                 dian = 1;
  19.                         }
  20.                         if (ch >= '0' && ch <= '9')
  21.                         {
  22.                                 number += 1;
  23.                                 sum += (ch - '0')*d; //数字和
  24.                         }
  25.                         else if (number > 0 && !(ch >= '0' && ch <= '9') && ch != '.')  //让程序能以任意字符为数字间隔
  26.                         {
  27.                                 dian = 0;
  28.                                 number = 0;
  29.                         }
  30.                         else
  31.                         {
  32.                                 isOdd = 0;  //退出while循环
  33.                         }
  34.                 }
  35.         }

  36.         printf("结果是:%.2f\n", sum);
  37.         return 0;
  38. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-22 12:57:42 | 显示全部楼层
有点不明白你的问题,改了一下代码
  1. #include <stdio.h>
  2. #include <stdbool.h>

  3. int main()
  4. {
  5.     float ch, sum = 0, d, number;
  6.     bool dian, isOdd = 1;

  7.     while (isOdd)
  8.     {
  9.         printf("请输入合法的数字:");
  10.         for (d = 1, dian = 0, number = 0; (ch = getchar()) != '\n';)
  11.         {
  12.             if (dian)
  13.             {
  14.                     d /= 10;  //实现小数点后的分数
  15.             }
  16.             if (ch == '.')
  17.             {
  18.                     dian = 1;
  19.             }
  20.             if (ch >= '0' && ch <= '9')
  21.             {
  22.                     number += 1;
  23.                     sum += (ch - '0')*d; //数字和
  24.             }
  25.             else if (number > 0 && !(ch >= '0' && ch <= '9') && ch != '.')  //让程序能以任意字符为数字间隔
  26.             {
  27.                     dian = 0;
  28.                     number = 0;
  29.             }
  30.             else
  31.             {
  32.                     isOdd = 0;  //退出while循环
  33.             }
  34.         }
  35.     }

  36.     printf("结果是:%.2f\n", sum);

  37.     return 0;
  38. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-22 13:05:52 | 显示全部楼层
大马强 发表于 2021-10-22 12:55
你这代码是实现什么功能的?

课后作业s1e14期的第二个动手题的进阶
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-20 19:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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