pr0719.cpp

来自「practice c++, it is from the book http:/」· C++ 代码 · 共 26 行

CPP
26
字号
//  Programming with C++, Second Edition, by John R. Hubbard
//  Copyright McGraw-Hill, 2000
//  Problem 7.19 on page 176
//  Summing a function with return type double

#include <cmath>     // defines the sqrt() function
#include <iostream>  // defines the cout object
using namespace std;

double sum(double (*)(double), int);

int main()
{ for (int x=0; x<5; x++)
    cout << "    sqrt(" << x << ") = " << sqrt(x)
         << "\nsum(sqrt," << x << ") = " << sum(sqrt,x) << endl;
}

double sum(double (*pf)(double x), int n)
{ // returns the sum f(0) + f(1) + f(2) + . . . + f(n-1)
  // where f() = *pf():
  double sum = 0;
  for (int i = 1; i <= n; i++)
    sum += (*pf)(i);
  return sum;
}

⌨️ 快捷键说明

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