标签:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<conio.h>
float f(float x)
{
float a;
a=x*x*x-2*x-5;
return a;
}
/*
www.quzhuanpan.com
转载请务必告知
*/
int main(void)
{
double low,bottom,prec,mid;//左端点,右端点,精度,中间值
//clrscr();
printf("\nPlease input data low=");
scanf("%lf",&low);
printf("\nPlease input data bottom=");
scanf("%lf",&bottom);
printf("\nPlease input data precision=");
scanf("%lf",&prec);
//mid=(low+bottom)/2;
if(f(low)*f(bottom)<0)
{
while(f(mid)!=0)
{
mid=(low+bottom)/2;
if(f(mid)*f(low)<0)
{
bottom=mid;
if(fabs(bottom-low)<prec)
{
break;
}
else
continue;
}
else
{
low=mid;
if(fabs(bottom-low)<prec)
{
break;
}
else
continue;
}
}
printf("\n");
mid=(low+bottom)/2;
printf("The root of f(x)=0 is x= %lf\n",mid);
}
else
{
printf("NO root!Refresh input\n");
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/hardmore/p/5128691.html