鱼C论坛

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

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

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

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

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

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

int main()

{
        printf("请输入待转换的字符串:");
        char ch;
        int ch2;
        long double ch1;
        long long int num = 0;
        long long int temp = 1;
        ch = getchar();
        if (ch >= '0' && ch <= '9')
        {
                do
                {
                        ch2 = (int)ch - '0';
                        ch1 = (float)ch2/ temp;
                        num = num + ch1;
                        ch = getchar();
                        temp = temp * 10;
                } while (ch >= '0' && ch <= '9');
        }
        num = num * temp * 0.1;
        printf("结果是:%lld", num);
        return 0;
}
得到的结果是20。到底是哪里出了问题,还请大佬们看看
最佳答案
2022-8-13 11:46:20
本帖最后由 jackz007 于 2022-8-13 11:47 编辑

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

int main(void)
{
        char c                                                                      ;
        int n                                                                       ;
        printf("请输入待转换的字符串 : ")                                           ;
        for(n = 0 ; (c = getchar()) && c >= '0' && c <= '9' ; n = n * 10 + c - '0') ;
        printf("n = %d\n" , n)                                                      ;
}
        编译、运行实况:
D:\[00.Exerciese.2022]\C>g++ -o x x.c

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

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

D:\[00.Exerciese.2022]\C>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-8-13 01:44:43 | 显示全部楼层
说句题外话,13-23行的嵌套可以改成while(){
}吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

我知道了
你的代码基本没有问题,但请看第10行,你把num变量设置为int类型,在17-19行中用一个浮点数去加整型,那当然要取整再加了~
比如输入“123”,num的操作过程为:
num+=1;
num+=0.2;
num+=0.03;
那么显然,int num加后结果为1。再乘以100后,输出100.
因此只需将第10行的int改为double即可
#include <stdio.h>

int main()

{
        printf("请输入待转换的字符串:");
        char ch;
        int ch2;
        double ch1;
        double num = 0;
        long long int temp = 1;
        ch = getchar();
        if (ch >= '0' && ch <= '9')
        {
                do
                {
                        ch2 = (int)ch - '0';
                        ch1 = (float)ch2/ temp;
                        num = num + ch1;
                        ch = getchar();
                        temp = temp * 10;
                } while (ch >= '0' && ch <= '9');
        }
        num = num * temp * 0.1;
        printf("结果是:%.0lf", num);
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

int main(void)
{
        char c                                                                      ;
        int n                                                                       ;
        printf("请输入待转换的字符串 : ")                                           ;
        for(n = 0 ; (c = getchar()) && c >= '0' && c <= '9' ; n = n * 10 + c - '0') ;
        printf("n = %d\n" , n)                                                      ;
}
        编译、运行实况:
D:\[00.Exerciese.2022]\C>g++ -o x x.c

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

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

D:\[00.Exerciese.2022]\C>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-6 10:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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