读书人

求教C语言解决办法

发布时间: 2013-01-17 10:28:54 作者: rapoo

求教C语言
我在visual studio 2010下编译的代码如下:
#include<stdio.h>
main()
{
double c=0, ff=0;
scanf("enter the temperature:%lf",&ff);
c=(5/9)*(ff-32);
printf("exchange ff to c temperature is %fl",c);
getch();
return(0);
}
编译成功了。
可是不论我输入的变量是多少出来都是一个答案-0.0000001
[解决办法]
scanf的用法和浮点数运算不对,这样改:


#include<stdio.h>
main()
{
double c=0, ff=0;
printf("enter the temperature:");
scanf("%lf",&ff);
c=(5.0/9)*(ff-32);
printf("exchange ff to c temperature is %lf\n",c);
getch();
return(0);
}

[解决办法]

#include<stdio.h>
main()
{
double c=0, ff=0;
printf("enter the temperature:");
scanf("%lf", &ff);
c=(5/9.0)*(ff-32); //这儿5/9应该改为5.0/9或者5/9.0否则计算的结果是5/9是0
printf("exchange ff to c temperature is %fl",c);
getch();
return(0);
}

读书人网 >C++

热点推荐