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

Z GeoWikiCZ
Skočit na navigaci Skočit na vyhledávání
Řádek 3: Řádek 3:
 
   
 
   
 
  double polynom(double x, const std::vector<double>& a);
 
  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()
 
  int main()
Řádek 15: Řádek 17:
 
   p.push_back(7);
 
   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)
 
   for (double x=0; x<1.05; x+=0.1)
 
     {
 
     {
Řádek 33: Řádek 53:
 
   
 
   
 
   return h;
 
   return h;
 +
}
 +
 +
 +
void  soucet (const std::vector<double>& q, const std::vector<double>& r,
 +
                std::vector<double>& p)
 +
{
 +
  // p je vystupni kontejner, musime nejprve zrusit vsechny jeho prvky
 +
  p.clear();
 +
  int nq = 0;
 +
  int nr = 0;
 +
  const int max = std::max(q.size(), r.size());
 +
 +
  for (int i=0; i<max; i++, nq++, nr++)
 +
    {
 +
      double s = 0;
 +
      if (nq <= i) s += q[i];
 +
      if (nr <= i) s += r[i];
 +
      p.push_back(s);
 +
    }
 
  }
 
  }
  
Řádek 46: Řádek 85:
 
  0.9    20.9007
 
  0.9    20.9007
 
  1      25
 
  1      25
 +
 +
0      9
 +
0.1    9.2547
 +
0.2    9.6432
 +
0.3    10.2147
 +
0.4    11.0352
 +
0.5    12.1875
 +
0.6    13.7712
 +
0.7    15.9027
 +
0.8    18.7152
 +
0.9    22.3587
 +
1      27
  
 
[ [[C plus plus Bc. 13|Zpět]] ]
 
[ [[C plus plus Bc. 13|Zpět]] ]

Verze z 11. 3. 2006, 22:58

#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)
{
  // p je vystupni kontejner, musime nejprve zrusit vsechny jeho prvky
  p.clear();
  int nq = 0;
  int nr = 0;
  const int max = std::max(q.size(), r.size());

  for (int i=0; i<max; i++, nq++, nr++)
    {
      double s = 0;
      if (nq <= i) s += q[i];
      if (nr <= i) 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.2547
0.2    9.6432
0.3    10.2147
0.4    11.0352
0.5    12.1875
0.6    13.7712
0.7    15.9027
0.8    18.7152
0.9    22.3587
1      27

[ Zpět ]