鱼C论坛

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

[已解决]关于S1E13中while语句的问题

[复制链接]
发表于 2022-8-13 01:38:23 | 显示全部楼层 |阅读模式

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

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

x
我输入132,预想的是132,得到的结果是100;输入23,预想的是23,
  1. #include <stdio.h>

  2. int main()

  3. {
  4.         printf("请输入待转换的字符串:");
  5.         char ch;
  6.         int ch2;
  7.         long double ch1;
  8.         long long int num = 0;
  9.         long long int temp = 1;
  10.         ch = getchar();
  11.         if (ch >= '0' && ch <= '9')
  12.         {
  13.                 do
  14.                 {
  15.                         ch2 = (int)ch - '0';
  16.                         ch1 = (float)ch2/ temp;
  17.                         num = num + ch1;
  18.                         ch = getchar();
  19.                         temp = temp * 10;
  20.                 } while (ch >= '0' && ch <= '9');
  21.         }
  22.         num = num * temp * 0.1;
  23.         printf("结果是:%lld", num);
  24.         return 0;
  25. }
复制代码
得到的结果是20。到底是哪里出了问题,还请大佬们看看
最佳答案
2022-8-13 11:46:20
本帖最后由 jackz007 于 2022-8-13 11:47 编辑

        字符串转数字没有那么麻烦。
  1. #include <stdio.h>

  2. int main(void)
  3. {
  4.         char c                                                                      ;
  5.         int n                                                                       ;
  6.         printf("请输入待转换的字符串 : ")                                           ;
  7.         for(n = 0 ; (c = getchar()) && c >= '0' && c <= '9' ; n = n * 10 + c - '0') ;
  8.         printf("n = %d\n" , n)                                                      ;
  9. }
复制代码

        编译、运行实况:
  1. D:\[00.Exerciese.2022]\C>g++ -o x x.c

  2. D:\[00.Exerciese.2022]\C>x
  3. 请输入待转换的字符串 : 123
  4. n = 123

  5. D:\[00.Exerciese.2022]\C>c
  6. 请输入待转换的字符串 : 23
  7. n = 23

  8. D:\[00.Exerciese.2022]\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-8-13 01:44:43 | 显示全部楼层
说句题外话,13-23行的嵌套可以改成while(){
}吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-8-13 01:47:54 | 显示全部楼层
本帖最后由 额外减小 于 2022-8-13 02:09 编辑

我知道了
你的代码基本没有问题,但请看第10行,你把num变量设置为int类型,在17-19行中用一个浮点数去加整型,那当然要取整再加了~
比如输入“123”,num的操作过程为:
  1. num+=1;
  2. num+=0.2;
  3. num+=0.03;
复制代码

那么显然,int num加后结果为1。再乘以100后,输出100.
因此只需将第10行的int改为double即可
  1. #include <stdio.h>

  2. int main()

  3. {
  4.         printf("请输入待转换的字符串:");
  5.         char ch;
  6.         int ch2;
  7.         double ch1;
  8.         double num = 0;
  9.         long long int temp = 1;
  10.         ch = getchar();
  11.         if (ch >= '0' && ch <= '9')
  12.         {
  13.                 do
  14.                 {
  15.                         ch2 = (int)ch - '0';
  16.                         ch1 = (float)ch2/ temp;
  17.                         num = num + ch1;
  18.                         ch = getchar();
  19.                         temp = temp * 10;
  20.                 } while (ch >= '0' && ch <= '9');
  21.         }
  22.         num = num * temp * 0.1;
  23.         printf("结果是:%.0lf", num);
  24.         return 0;
  25. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-8-13 11:46:20 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2022-8-13 11:47 编辑

        字符串转数字没有那么麻烦。
  1. #include <stdio.h>

  2. int main(void)
  3. {
  4.         char c                                                                      ;
  5.         int n                                                                       ;
  6.         printf("请输入待转换的字符串 : ")                                           ;
  7.         for(n = 0 ; (c = getchar()) && c >= '0' && c <= '9' ; n = n * 10 + c - '0') ;
  8.         printf("n = %d\n" , n)                                                      ;
  9. }
复制代码

        编译、运行实况:
  1. D:\[00.Exerciese.2022]\C>g++ -o x x.c

  2. D:\[00.Exerciese.2022]\C>x
  3. 请输入待转换的字符串 : 123
  4. n = 123

  5. D:\[00.Exerciese.2022]\C>c
  6. 请输入待转换的字符串 : 23
  7. n = 23

  8. D:\[00.Exerciese.2022]\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-17 00:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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