C++ Bc. 13 cpp

Z GeoWikiCZ
#include <iostream>
#include <vector>

double polynom(double x, const std::vector<double>& a);
void   soucet (const std::vector<double>& q, const std::vector<double>& r,
               std::vector<double>& p);

int main()
{
  using namespace std;

  vector<double> p;
  p.push_back(9);
  p.push_back(2);
  p.push_back(5);
  p.push_back(2);
  p.push_back(7);

  for (double x=0; x<1.05; x+=0.1)
    {
      cout << x << "\t" << polynom(x, p) << endl;
    }
  cout << endl;

  vector<double> q, r;
  q.push_back( 3);
  q.push_back(-1);
  q.push_back( 2);

  r.push_back(6);
  r.push_back(3);
  r.push_back(3);
  r.push_back(2);
  r.push_back(7);

  soucet(q, r, p);
  for (double x=0; x<1.05; x+=0.1)
    {
      cout << x << "\t" << polynom(x, p) << endl;
    }
}


double polynom(double x, const std::vector<double>& a)
{
  int    n = a.size();
  double h = a[--n];
  do 
    {
      h *= x;
      h += a[--n];
    } while (n);

  return h;
}


void   soucet (const std::vector<double>& q, const std::vector<double>& r,
               std::vector<double>& p)
{
  // ve vystupnim kontejneru 'p' musime nejprve zrusit vsechny jeho prvky
  p.clear();
  const int nq  = q.size();
  const int nr  = r.size();
  const int max = std::max(nq, nr);

  for (int i=0; i<max; i++)
    {
      double s = 0;
      if (i < nq) s += q[i];
      if (i < nr) s += r[i];
      p.push_back(s);
    }
}
0      9
0.1    9.2527
0.2    9.6272
0.3    10.1607
0.4    10.9072
0.5    11.9375
0.6    13.3392
0.7    15.2167
0.8    17.6912
0.9    20.9007
1      25

0      9
0.1    9.2527
0.2    9.6272
0.3    10.1607
0.4    10.9072
0.5    11.9375
0.6    13.3392
0.7    15.2167
0.8    17.6912
0.9    20.9007
1      25

[ Zpět ]