所以我有这个程序,我在其中读取 5 个数字和一个逗号作为字符,并将它们一起表示。我已经设法做到了,但是输出非常奇怪..
这是代码:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int i=0;
float number = 0.0;
char f;
printf("Introduce a number in the following format (ccc,cc):\n");
for(i=1; i<=6; i++){
f=getchar();
if(f=='\n' && i<6){
printf("the number is not correct.\n");
exit(1); }
if(i==4 && f!=','){
printf("The number doesn't have a comma.\n");
exit(1); }
if(i==4)
continue;
if((f<'0') || (f>'9')){
printf(" %d is not a number .\n", i);
exit(1); }
switch (i)
{
case 1 : number = (f*100);
break;
case 2 : number += (f*10);
break;
case 3 : number = number + f;
break;
case 4: ;
break;
case 5 : number += (f * 0.1);
break;
case 6 : number += (f*0.01);
break;
}
}
printf("The number you inserted is :%f\n",number);
}
数字 123,45 的输出应该是完全相同的数字,但我得到了一个超级尴尬的事情:
Introduce a number in the following format (ccc,cc):
123,45
The number you inserted is :5456.729980
有什么帮助吗?