polyeval.cpp

来自「datastucutre and algorithms, application」· C++ 代码 · 共 36 行

CPP
36
字号
// evaluate a polynomial

#include <iostream>
#include <algorithm> // has copy

using namespace std;


template<class T>
T polyEval(T coeff[], int n, const T& x)
{// Evaluate the degree n polynomial with
 // coefficients coeff[0:n] at the point x.
   T y = 1, value = coeff[0];
   for (int i = 1; i <= n; i++)
   {// add in next term
      y *= x;
      value += y * coeff[i];
   }
   return value;
}

int main()
{
   int a[6] = {1, 2, 3, 4, 5, 6};

   // output the coefficients
   cout << "The coefficients a[0:5] are ";
   copy(a, a+6, ostream_iterator<int>(cout, " "));
   cout << endl;

   cout << "The value at 2 is " << polyEval(a,5,2) << endl;
   cout << "The value at 1 is " << polyEval(a,5,1) << endl;
   cout << "The value at 0 is " << polyEval(a,5,0) << endl;
   return 0;
}

⌨️ 快捷键说明

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