求方程的根
#include <stdio.h>#include <math.h>
float f(float x)
{
return ((x-5)*x+16)*x-80;
}
float xpoint(float x1,float x2)
{
return(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
}
float root (float x1,float x2)
{
int i;
float x,y,y1;
y1=f(x1);
do
{
x=xpoint(x1,x2);
y=f(x);
if(y*y1>0)
{
y1=y;
x1=x;
}
else
{
x2=x;
}
}
while (fabs(y)>0.00001);
return x;
}
main()
{
float x,x1,x2,y1,y2;
do
{
printf("Input x1,x2:");
scanf("%f,%f",&x1,&x2);
y1=f(x1);
y2=f(x2);
}
while (y1*y2>0);
x=root(x1,x2);
printf("A root is %f\n",x);
}
有没有简单点的方法除了用弦截法
搜索更多相关的解决方案:
方程
----------------解决方案--------------------------------------------------------
首先,主函数中的y1,y2应该是局部变量,在主函数中没有定义,其他的还没看
----------------解决方案--------------------------------------------------------
他好像是在问有没有其他方法,他那个编译也成功啊,2楼的说没定义???
----------------解决方案--------------------------------------------------------
强!
我是新手,还看不懂呢! ----------------解决方案--------------------------------------------------------