直接给x1,x2赋值和用scanf输入有什么不同吗?
我在学C,刚编的不知道哪错了,下面的程序我把 double x1,x2,x;
scanf("%f %f",&x1,&x2);
换成 double x1=0.0,x2=10.0,x;就对了为什么啊?????
#include<stdio.h>
#include<math.h>
double y(double x);
double x(double x1,double x2);
double root(double x1,double x2);
double y(double x)
{
double yj;
yj=x*x*x-5*x*x+16*x-80;
return yj;
}
double x(double x1,double x2)
{
double xj;
xj=(x1*y(x2)-x2*y(x1))/(y(x2)-y(x1));
return xj;
}
double root(double x1,double x2)
{
double xx,yy;
xx=x1;
while(fabs(y(xx))>0.0001)
{
xx=x(x1,x2);
yy=y(xx);
if(yy*y(x1)>0)
x1=xx;
else
x2=xx;
}
return xx;
}
main()
{
double x1,x2,x;
scanf("%f %f",&x1,&x2);
while(1)
{
if(y(x1)*y(x2)<0)
{x=root(x1,x2);break;}
else
{
printf("重新输入:");
scanf("%f%f",&x1,&x2);
}
}
printf("%6.3f\n",x);
}