C++ Bc. 13 cpp: Porovnání verzí

Z GeoWikiCZ
Bez shrnutí editace
Bez shrnutí editace
Řádek 59: Řádek 59:
                 std::vector<double>& p)
                 std::vector<double>& p)
  {
  {
   // p je vystupni kontejner, musime nejprve zrusit vsechny jeho prvky
   // vw vystupnim kontejneru 'p' musime nejprve zrusit vsechny jeho prvky
   p.clear();
   p.clear();
   int nq = 0;
   const int nq = q.size();
   int nr = 0;
   const int nr = r.size();
   const int max = std::max(q.size(), r.size());
   const int max = std::max(nq, nr);
   
   
   for (int i=0; i<max; i++, nq++, nr++)
   for (int i=0; i<max; i++)
     {
     {
       double s = 0;
       double s = 0;
       if (nq <= i) s += q[i];
       if (i < nq) s += q[i];
       if (nr <= i) s += r[i];
       if (i < nr) s += r[i];
       p.push_back(s);
       p.push_back(s);
     }
     }
Řádek 87: Řádek 87:
   
   
  0      9
  0      9
  0.1    9.2547
  0.1    9.2527
  0.2    9.6432
  0.2    9.6272
  0.3    10.2147
  0.3    10.1607
  0.4    11.0352
  0.4    10.9072
  0.5    12.1875
  0.5    11.9375
  0.6    13.7712
  0.6    13.3392
  0.7    15.9027
  0.7    15.2167
  0.8    18.7152
  0.8    17.6912
  0.9    22.3587
  0.9    20.9007
  1      27
  1      25


[ [[C plus plus Bc. 13|Zpět]] ]
[ [[C plus plus Bc. 13|Zpět]] ]

Verze z 11. 3. 2006, 22:13

#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)
{
  // vw 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 ]