程序代码:
/* * File Name: Easy.cpp * Date: 2010/12/21 * Auto: ZhouFeng */ #include <iostream> #include <iomanip> using std::cout; using std::endl; using std::cin; using std::setiosflags; using std::ios; const double END_RECURSION = 0.00001; // 循环结束条件 double CountX( double, double, double); double FloatAbs( double); // 计算double类型绝对值 int main() { while( true) { cout << "Please enter the value of A:\n"; double A; cin >> A; double result = CountX( 1, 2, A); cout << setiosflags( ios::fixed) << std::setprecision(3) << result << endl; } return 0; } double CountX( double preX, double lstX, double a) { while( true) { lstX = ( preX + a / preX) / 2; if ( FloatAbs( preX - lstX) > END_RECURSION) { preX = lstX; lstX = ( preX + a / preX) / 2; } else { break; } } return preX; } double FloatAbs( double num) { double res = num; if( num < 0) { res = -1 * res; } return res; }
这样,可否?
[ 本帖最后由 zhoufeng1988 于 2010-12-21 13:44 编辑 ]