二分法求ax+b=0的解

#include <stdio.h>
int   main()
{double a=-100.0,b=100.0,c,d,f;
printf("请输入f(x)中的x的系数c和常数项d:");
scanf("%lf%lf",&c,&d);
while(b-a>1e-6)
{ 
f=(a+b)/2.0;
if(c*f+d==0)
{printf("该方程%lf*x+%lf=0的解是:%f",c,d,f);
return 0;}
if((c*f+d)*(c*a+d)<0)
b=f;
if((c*f+d)*(c*b+d)<0)
a=f;
}
f=(a+b)/2.0;
printf("该方程%lf*x+%lf=0的解是:%f",c,d,f);
}
在这里插入代码片

猜你喜欢

转载自blog.csdn.net/feiqipengcheng/article/details/83020077
今日推荐