二分法,滿足一個精度
#include<stdio.h>
#include<math.h>
double fx(double x);
int main()
{
float a,b,c;
do
{
scanf("%f %f",&a,&b);
}
while(fx(a)*fx(b)>=0); // 滿足小於零才結束輸入
printf("there is a root between[%f,%f]\n",a,b);
do{
c=(a+b)/2;
if(fx(a)*fx(c)<0)
b=c;
else
a=c;
}while((fx(c)>0.00001)||(fx(c)<-0.00001));
printf("the root is %.5f\n",c);
return 0;
}
double fx(double x)
{
double ret;
ret = 2*x*x*x - 5*x*x + 3*x - 6;
return ret;
}