#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
float root(float,float,float,float);
float f(float);
float y1,y2,x1,x2;
while(1)
{
scanf("%f %f",&x1,&x2);
y1=f(x1);
y2=f(x2);
if(y1*y2<0)
{
printf("The root is
%.2f",root(x1,x2,y1,y2));
return 0;
}
printf("Please change the nums please!\n");
}
}
float f(float x)
{
return x*x*x-5*x*x+16*x-80;
}
float root(float x1,float x2,float y1,float y2)
{
float f(float x);
float x,y;
x=(x1*y2-x2*y1)/(y2-y1);
y=f(x);
printf("The ro
%f
%f",x,y);
if(y>0?y<1e-6:-y<1e-6) return x;
if(y*y1>0) return root( x, x2, y, y2);
return root( x1, x, y1, y);
}
初始值选不好,能造成死循环,如何避免很麻烦,弦截法中应该有解决的办法吧。