# include <iostream> # include <cmath> using namespace std; float Formula(float x)//方程表达式 { float y; y=x*x*x-2*x-5; //范例方程 return y; } //求导 float FormulaDao(float x){ float y; y=3*x*x-2; //范例方程的导数 return y; } void main(){ int n=100,k=0;//最大迭代次数 float x0=1.0;//初始X0的值 float x; x=x0; while(fabs(Formula(x))>0.00001&&k<n) { k++; x = x - Formula(x) /FormulaDao(x); } if(fabs(Formula(x)) < 0.00001){ cout<<"根植"<<x; } else cout<<"无根"; }