|
5啊哈币
#include <stdio.h>
#include <math.h>
float f(float x)
{
float z;
return 2*x*x-3*x;
}
float xpoint(float x1,float x2)
{float y1,y2;
y1=f(x1);
y2=f(x2);
return (x1-x2)/(y2-y1)*y1+x1;
}
float root(float a,float b)
{
float x3,y3;
do
{x3=xpoint(a,b);
y3=f(x3);
if(f(a)*y3>0) a=x3;
else b=x3;
}while(fabs(y3)>0.000001);
return x3;
}
int main()
{int flag=1;
float x1,x2;
while(flag)
{printf("enter x1 x2:");
scanf("%f%f",&x1,&x2);
if(f(x1)*f(x2)>0) continue;
else flag=0;
}
printf("one root is %f.",root(x1,x2));
getch();
}
如果输入-10 1就能很快算出来0 但是输入1 10就好像一直是个死循环。
|
|