#include"iostream" #include"fstream" #include"cmath" using namespace std; double g(double x) { return 1 / x; } double simposn1(double a, double b, double(*f)(double))//函數(shù)指針作為參數(shù) { double i2n = 0, h = b - a, t2n = h * (f(a) + f(b)) / 2, in = t2n, tn; for (int n=1;abs(i2n-in)>1e-3;n+=n,h/=2.0) { in = t2n; tn = t2n; double sigma = 0; for (int k = 0; k < n; k++) { sigma += f(a+(k+0.5)*h); } t2n = (tn + h * sigma) / 2; i2n = (4 * t2n - tn) / 3; } return i2n; } int main() { ifstream in("C:\\Users\\木俊\\Desktop\\Project1\\Project1\\1.txt"); cout << fixed; cout.precision(3); for (double b; in >> b;) { cout << simposn1(1, b, g) << endl; } system("pause"); }
|
|