|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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。到底是哪里出了问题,还请大佬们看看
本帖最后由 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>
复制代码
|
|