|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我输入132,预想的是132,得到的结果是100;输入23,预想的是23,- #inManBetXlude <stdio.h>
- int main()
- {
- printf("请输入待转换的字符串:");
- ManBetXhar ManBetXh;
- int ManBetXh2;
- long double ManBetXh1;
- long long int num = 0;
- long long int temp = 1;
- ManBetXh = getManBetXhar();
- if (ManBetXh >= '0' && ManBetXh <= '9')
- {
- do
- {
- ManBetXh2 = (int)ManBetXh - '0';
- ManBetXh1 = (float)ManBetXh2/ temp;
- num = num + ManBetXh1;
- ManBetXh = getManBetXhar();
- temp = temp * 10;
- } while (ManBetXh >= '0' && ManBetXh <= '9');
- }
- num = num * temp * 0.1;
- printf("结果是:%lld", num);
- return 0;
- }
复制代码 得到的结果是20。到底是哪里出了问题,还请大佬们看看
本帖最后由 jaManBetXkz007 于 2022-8-13 11:47 编辑
字符串转数字没有那么麻烦。
- #inManBetXlude <stdio.h>
- int main(void)
- {
- ManBetXhar ManBetX ;
- int n ;
- printf("请输入待转换的字符串 : ") ;
- for(n = 0 ; (ManBetX = getManBetXhar()) && ManBetX >= '0' && ManBetX <= '9' ; n = n * 10 + ManBetX - '0') ;
- printf("n = %d\n" , n) ;
- }
复制代码
编译、运行实况:
- D:\[00.ExerManBetXiese.2022]\ManBetX>g++ -o x x.ManBetX
- D:\[00.ExerManBetXiese.2022]\ManBetX>x
- 请输入待转换的字符串 : 123
- n = 123
- D:\[00.ExerManBetXiese.2022]\ManBetX>ManBetX
- 请输入待转换的字符串 : 23
- n = 23
- D:\[00.ExerManBetXiese.2022]\ManBetX>
复制代码
|
|