#include<stdio.h>
#include<math.h>
int main()
{
double a=11,b,v;
printf("Enter v:\n");
scanf("%f",&v);
if(a==v||fabs(a-v)<0.000000000001)
{
printf("Speed is too high.\n");
}
return 0;
}
#include<stdio.h>
#include<math.h>
int main()
{
double a=11,b,v;
printf("Enter v:\n");
scanf("%f",&v);
if(a==v||fabs(a-v)<0.000000000001)
{
printf("Speed is too high.\n");
}
return 0;
}
抱歉因为之前一直提示有不能发的词,草草发了问题就来上课了。现在来补充了。
同学在做作业的时候遇到的,老师也不能很好地解答。
编译器是DEV。
这是我同学作业的代码,目的是判断是否车速。
#include<stdio.h>
int main()
{double v,normal,a,b;
printf("Limit:");
scanf("%lf",&normal);
printf("Speed:");
scanf("%lf",&v);
a=1.1*normal;
b=1.5*normal;
if(v<a)
{
printf("无");
}
else if(v>=a&&v<b)
{
printf("A");
}
else
{
printf("B");
}
return 0;
}
在测试代码的时候发现,速度a=110,v=110时会进入第一个分支,打印“无”。测试了1100和11两组数都没有问题。
在老师的指导下加入了fabs函数来比较a、v差值和一个极小值,怀疑是浮点数判等的问题,但是问题依然存在。
于是我尝试将代码简化,如主楼所示。
现在好了,就连11和1100也不能判等了。
求解。谢谢大佬们。