用二分法求根:陷入了死循环(已解决)
用二分法求根:陷入了死循环#include<iostream>
using namespace std;
int main()
{
double fabs(double ,double);
double f(double );
double x2,x1,x;
do{
cout<<"请输入x1和x2的值:";
cin>>x1>>x2;
cout<<"x1="<<x1<<" x2="<<x2;
}while(f(x1)*f(x2)>0);
do{
x=(x1+x2)/2;
if(f(x)*f(x1)>0) x2=x;
else x1=x;
}while(fabs(f(x),0)>=1e-5);
cout<<"结果为:"<<x;
return 0;
}
double f(double x)
{ return(((x+-5)*x+16)*x+-80);}
double fabs(double a,double b)
{ return (a-b>0?a-b:b-a);}
[[it] 本帖最后由 yqiong 于 2008-3-21 15:41 编辑 [/it]]