C++ Bc. 4 cpp

Z GeoWikiCZ
(přesměrováno z C plus plus Bc. 4 cpp)
#include <iostream>
#include <matvec/matvec.h>

struct Chyba
{
   template <typename T> Chyba(T t) : text(t) {}

   std::string text;
};


void reseni(const GNU_gama::Mat<>& U, GNU_gama::Vec<>& x)
{
  if (U.rows() != U.cols() || U.rows() != x.dim()) 
    throw Chyba("chybne zadane dimenze");

  const int N = x.dim();
  for (int i=N; i>0; i--)
    {
      double t = x(i);
      for (int j=i+1; j<=N; j++)
        {
          t -= U(i,j)*x(j);
        }

      double p = U(i,i);
      if (p == 0) throw Chyba("nulovy prvek na diagonale");

      x(i) = t/p;
    }
}


int main()
{
  using namespace std;
  using namespace GNU_gama;
  
  Mat<> U(5,5);
  U = 
    3, 0, 3, 4, 1,
    0, 1, 4, 2, 1,
    0, 0, 1, 0, 2,
    0, 0, 0, 2, 6,
    0, 0, 0, 0, 3;

  Vec<> b(5);  b = 33, 27, 13, 38,  15;

  try
    {
      reseni(U, b);
      
      cout << trans(b);
    }
  catch(Chyba& ch)
    {
      cout << ch.text;
    }
}

[ Zpět ]