⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pr0704.cpp

📁 practice c++, it is from the book http://www.amazon.com/Schaums-Outline-Programming-John-Hubbard
💻 CPP
字号:
//  Programming with C++, Second Edition, by John R. Hubbard
//  Copyright McGraw-Hill, 2000
//  Problem 7.4 on page 175
//  Integrating a function

#include <iostream>
using namespace std;

double riemann(double (*)(double), double, double, int);
double cube(double);

int main()
{ cout << riemann(cube,0,2,10) << endl;
  cout << riemann(cube,0,2,100) << endl;
  cout << riemann(cube,0,2,1000) << endl;
  cout << riemann(cube,0,2,10000) << endl;
}

double riemann(double (*pf)(double t), double a, double b, int n)
{ //  Returns [f(a)*h + f(a+h)*h + f(a+2h)*h + . . . + f(b-h)*h],
  //  where h = (b-a)/n:
  double s = 0, h = (b-a)/n, x;
  int i;
  for (x = a, i = 0; i < n; x += h, i++)
    s += (*pf)(x);
  return s*h;
}

double cube(double t)
{ return t*t*t;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -