[程序纠错]截弦法解一元三次多项式,为什么输入之后按回车会不出结果!
#include<stdio.h>#include<math.h>
int main()
{
float root(float x1,float x2);
float xpoint(float x1,float x2);
float f(float x);
float y1,y2,x1,x2;
float x;
scanf("x1=%f,x2=%f",&x1,&x2);
y1=f(x1);
y2=f(x2);
if(y1*y2<0)
{
x=root(x1,x2);
printf("The root is %.2f",x);
}
else
{
printf("Please change the nums please!\n");
return 0;
}
}
float f(float x)
{
float y;
y=x*x*x-5*x*x+16*x-80;
return y;
}
float xpoint(float x1,float x2)
{
float f(float x);
float y1,y2;
float x;
x=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
return x;
}
float root(float x1,float x2)
{
float f(float x);
float xpoint(float x1,float x2);
float y1,y2,y,x;
y1=f(x1);
y2=f(x2);
// if(y1*y2<0)
// {
x=xpoint(x1,x2);
while(abs(f(x)<=1e-6))
{
if(f(x)*f(x1)>0)
x1=x;
else
x2=x;
x=xpoint(x1,x2);
}
return x;
// }
// else
// {
// printf("Please choose num again !"); 已将此步骤于main函数内实现O(∩_∩)O~
// return 0;
// }
}