C++ Bc. 16 cpp

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

typedef std::vector<double> Polynom;
void soucin(Polynom& P, const Polynom& Q, const Polynom& R);

int main()
{
  Polynom A, B, C;
  A.push_back( 3);    // A[0]
  A.push_back( 0);    // A[1]
  A.push_back(-1);    // A[2]

  B.push_back(-3);    // B[0]
  B.push_back( 2);    // B[1]
  B.push_back( 4);    // B[2]
  B.push_back(-2);    // B[3]

  soucin(C, A, B);

  // deklarace pomocne funkce pro tisk polynomu
  void tisk(std::ostream&, const char*, const Polynom&);

  tisk(std::cout, " A ", A);
  tisk(std::cout, " B ", B);
  tisk(std::cout, "A*B", C);
}


void soucin(Polynom& P, const Polynom& Q, const Polynom& R)
{
  // Zmenime pocet prvku vystupniho polynomy. Hodnoty jednotlich prvku
  // jsou pri teto operaci inicializovany na nulu (tj. na implicitni
  // hodnotu daneho typu).
  P.resize(Q.size()+R.size()-1);

  for (int i=0; i<Q.size(); i++)
    for (int j=0; j<R.size(); j++)
      {
        P[i+j] += Q[i]*R[j];
      }
}


void tisk(std::ostream& out, const char* t, const Polynom& p)
{
  out << t << " [ ";
  for (Polynom::const_reverse_iterator i=p.rbegin(), e=p.rend(); i!=e; ++i)
    {
      out << std::setw(2) << *i << " ";
    }
  out << "]\n";
}
 A  [ -1  0  3 ]
 B  [ -2  4  2 -3 ]
A*B [  2 -4 -8 15  6 -9 ]

[ Zpět ]