#include
#include
float f(float x)
{
return (((x-5.0)*x+16.0)*x-80.0);
}
float L(float a,float b)
{
return ((a*f(b)-b*f(a))/(f(b)-f(a)));
}
main()
{
float a,b,c;
float e=10^-6;
printf("Please input two numbers to start the program...\n");
scanf("%f %f",&a,&b);
while(f(a)*f(b)>0)
{
printf("Unideal numbers,please input two numbers again...\n");
scanf("%f%f",&a,&b);
}
do
{
c=L(a,b);
if(f(c)*f(a)>=0)
{
a=c;
}
else
{
b=c;
}
}
while(fabs(f(c))>e);//问题所在,这里fabs(f(c))肯定永远大于e,
//你的e是-16, 正数永远大于负数,这是个死循环
printf("The ideal answer is %f.\nThe distance is %f.\n",c,f(c));
printf("Press anykey for end.\n");
getch();
return 0;
}
你那个e是什么啊?e=10^-6
还有弱弱的问一下fabs是什么函数?没用过。。。