Опубликован: 10.03.2009 | Доступ: свободный | Студентов: 2295 / 280 | Оценка: 4.31 / 4.07 | Длительность: 09:23:00
Дополнительный материал 1:

Приложение

< Лекция 5 || Дополнительный материал 1: 12 || Дополнительный материал 2 >
Аннотация: В приложении приведены: класс матриц, класс полиномов, класс комплексных чисел, которые использовались при написании примеров в данном курсе.

Класс матриц

matrix.h

#include <cassert>
using namespace std;
typedef unsigned int uni;
template <class type> class matrix
{
  type **arr;
  size_t srow, scol;
  virtual void error (int ne);
public:
  matrix() {arr = NULL; srow = 0; scol = 0; }
  matrix(size_t c, size_t r);
  matrix(matrix &);
  ~matrix();

  void initnum(type k);
  void initstat(type *p, size_t c, size_t r);
  void initdinam(type **p, size_t c, size_t r);
  void addrowend(matrix &mrow);
  void addcolend(matrix &mcol);
  void setcol(size_t c, matrix col);
  void setrow(size_t r, matrix row);
  void setminor(size_t cs, size_t rs, matrix &m);
  void swaprow(size_t fr, size_t sr);
  void swapcol(size_t fc, size_t sc);
  void setsize(size_t c, size_t r);
  void setsingle(size_t s);
  void setcell(type val, size_t c, size_t r);
  size_t getrowsize();
  size_t getcolsize();
  type getcell(size_t c, size_t r);
  matrix getrow(size_t r);
  matrix getcol(size_t c);
  matrix getminor(size_t cs, size_t rs, size_t ce, size_t re);
  void transp();
  void delrowend();
  void delcolend();
  
  void operator= (matrix &m);
  matrix operator+ (matrix &m);
  matrix operator- (matrix &m);
  matrix operator* (matrix &m);
  matrix operator* (type c);
  matrix operator- ();
};

matrix.cpp

template <class type> matrix<type>::matrix(size_t c, size_t r)
{
  if(c && r)
  {
    arr = new type* [c];
    assert(arr != NULL);
    for(size_t i = 0; i < c; i++)
    {
      arr[i] = new type [r];
      assert(arr[i] != NULL);
    }
    scol = c;
    srow = r;
  }
  else error(-1);
}
template <class type> matrix<type>::matrix(matrix<type> &m)
{
  arr = NULL;
  srow = scol = 0;
  if(m.srow > 0 && m.scol > 0)
  {
    srow = m.srow;
    scol = m.scol;
    arr = new type* [scol];
    assert(arr != NULL);
    for(size_t i = 0; i < scol; i++)
    {
      arr[i] = new type [srow];
      assert(arr[i] != 0);
    }
    for(size_t i = 0; i < scol; i++)
      for(size_t j = 0; j < srow; j++)
        arr[i][j] = m.arr[i][j];
  }
  else error(1);
  
}
template <class type> matrix<type>::~matrix()
{
  if(arr != NULL)
    for(size_t i = 0; i < scol; i++)
      if(arr[i] != NULL) delete [] arr[i];
  delete [] arr;
}
template <class type> void matrix<type>::setsize(size_t c, size_t r)
{
  if(c == 0 && r == 0)
  {
    if(arr != NULL)
    {
      for(size_t i = 0; i < scol; i++)
        if(arr[i] != NULL)
          delete [] arr[i];
      delete [] arr;
    }
    arr = NULL;
    scol = srow = 0;
    return;
  }
  if(c < 0 && r < 0)
  {
    if(arr != NULL)
    {
      for(size_t i = 0; i < scol; i++)
        if(arr[i] != NULL)
          delete [] arr[i];
      delete [] arr;
    }
    scol = c;
    srow = r;
    arr = new type* [scol];
    assert(arr != NULL);
    for(size_t i = 0; i < scol; i++)
    {
      arr[i] = new type [srow];
      assert(arr[i] != NULL);
    }
  }
  else error(2);
}
template <class type> void matrix<type>::initstat(type *p, size_t c, size_t r)
{
  this->setsize(c,r);
  for(size_t i = 0; i < scol; i++)
    for(size_t j = 0; j < srow; j++)
      arr[i][j] = p[i*c + j];
}
template <class type> void matrix<<type>::initdinam(type **p, size_t c, size_t r)
{
  this->setsize(c,r);
  for(size_t i = 0; i < c; i++)
    for(size_t j = 0; j < r; j++)
      arr[i][j] = p[i][j];
}
template <class type> void matrix<type>::addcolend(matrix<type> &mcol)
{
  if(arr != NULL)
  {
    if(mcol.srow == srow && mcol.scol == 1)
    {
      matrix<type> temp(*this);
      for(size_t i = 0; i < scol; i++)
        if(arr[i] != NULL)
          delete [] arr[i];
      delete [] arr;
      arr = new type* [scol + 1];
      assert(arr != NULL);
      for(size_t i = 0; i < scol + 1; i++)
      {
        arr[i] = new type [srow];
        assert(arr[i]);
      }
      for(size_t i = 0; i < scol; i++)
        for(size_t j = 0; j < srow; j++)
          arr[i][j] = temp.arr[i][j];
      for(size_t i = 0; i < srow; i++)
        arr[scol][i] = mcol.arr[0][i];
      scol++;
    }
    else error(3);
  }
  else
  {
    arr = new type* [1];
    assert(arr != NULL);
    arr[0] = new type [mcol.srow];
    assert(arr[0] != NULL);
    for(size_t i = 0; i < mcol.srow; i++)
      arr[0][i] = mcol.arr[0][i];
    scol = 1; srow = mcol.srow;
  }
}
template <class type> void matrix<type>::delcolend()
{
  if(arr != NULL)
  {
    delete [] arr[scol-1];
    scol--;
  }
  else error(4);
}
template <class type> void matrix<type>::addrowend(matrix<type> &mrow)
{
  if(arr != NULL)
  {
    if(mrow.scol == scol && mrow.srow == 1)
    {
      matrix<type> temp(*this);
      for(size_t i = 0; i < scol; i++)
        if(arr[i] != NULL)
          delete [] arr[i];
      delete [] arr;
      arr = new type* [scol];
      assert(arr != NULL);
      for(size_t i = 0; i < scol; i++)
      {
        arr[i] = new type [srow + 1];
        assert(arr[i] != NULL);
      }
      for(size_t i = 0; i < scol; i++)
        for(size_t j = 0; j < srow; j++)
          arr[i][j] = temp.arr[i][j];
      for(size_t i = 0; i < scol; i++)
        arr[i][srow] = mrow.arr[i][0];
      srow++;
    }
    else error(5);
  }
  else
  {
    arr = new type* [mrow.scol];
    assert(arr != NULL);
    for(size_t i = 0; i < mrow.scol; i++)
    {
      arr[i] = new type [1];
      assert(arr != NULL);
    }
    for(size_t i = 0; i < mrow.scol; i++)
      arr[i][0] = mrow.arr[i][0];
    scol = mrow.scol; srow = 1;
  }
}
template <class type> void matrix<type>::delrowend()
{
  if(arr != NULL)
  {
    matrix<type> temp(*this);
    for(size_t i = 0; i < scol; i++)
      if(arr[i] != NULL)
        delete [] arr[i];
    delete [] arr;
    arr = new type* [scol];
    assert(arr != NULL);
    for(size_t i = 0; i < scol; i++)
    {
      arr[i] = new type [srow - 1];
      assert(arr[i] != NULL);
    }
    for(size_t i = 0; i < scol; i++)
      for(size_t j = 0; j < srow - 1; j++)
        arr[i][j] = temp.arr[i][j];
    srow--;
  }
  else error(6);
}
template <class type> type matrix<type>::getcell(size_t c, size_t r)
{
  type res;
  if(c >= 0 && c < scol && r >= 0 && r < srow)
    res = arr[c][r];
  else error(7);
  return res;
}
template <class type> matrix<type> matrix<type>::getcol(size_t c)
{
  matrix<type> res;
  if(arr != NULL)
  {
    if(0 <= c && c < scol)
    {
      res.initstat(arr[c],1,srow);
      return res;
    }
    else error(8);
  }
  else error(9);
  return res;
}
template <class type> size_t matrix<type>::getcolsize()
{
  size_t res;
  if(arr != NULL) res = scol;
  else error(10);
  return res;
}
template <class type> matrix<type> matrix<type>::getminor(size_t cs, size_t rs, size_t ce, size_t re)
{
  matrix<type> res;
  if(arr != NULL)
  {
    if(0 <= cs && ce < scol && cs <= ce && 0 <= rs && re < srow && rs <= re)
    {
      res.arr = new type* [ce - cs + 1];
      assert(res.arr != NULL);
      for(size_t i = 0; i < ce - cs + 1; i++)
      {
        res.arr[i] = new type [re - rs + 1];
        assert(res.arr != NULL);
      }
      for(size_t i = cs; i <= ce; i++)
        for(size_t j = rs; j <= re; j++)
          res.arr[i-cs][j-rs] = arr[i][j];
      res.scol = ce - cs + 1;
      res.srow = re - rs + 1;
    }
    else error(11);
  }
  else error(12);
  return res;
}
template <class type> matrix<type> matrix<type>::getrow(size_t r)
{
  matrix<type> res;
  if(arr != NULL)
  {
    if(0 <= r && r < srow)
    {
      type *temp = new type [scol];
      assert(temp != NULL);
      for(size_t i = 0; i < scol; i++)
        type[i] = arr[i][r];
      res.initstat(temp,sco,1);
    }
    else error(13);
  }
  else error(14);
  return res;
}
template <class type> size_t matrix<type>::getrowsize()
{
  size_t res;
  if(arr != NULL) res = srow;
  else error(15);
  return res;
}
template <class type> void matrix<type>::initnum(type k)
{
  if(arr != NULL)
  {
    for(size_t i = 0; i < scol; i++)
      for(size_t j = 0; j < srow; j++)
        arr[i][j] = k;
  }
  else error(16);
}
template <class type> void matrix<type>::setcol(size_t c, matrix<type> col)
{
  if(arr != NULL)
    if(0 <= c && c < scol && col.scol == 1 && col.srow == srow && col.arr != NULL)
      for(size_t i = 0; i < srow; i++)
        arr[c][i] = col.arr[0][i];
    else error(17);
  else error(18);
}
template <class type> void matrix<type>::setrow(size_t r, matrix<type> row)
{
  if(arr != NULL)
    if(0 <= r && r < srow && row.srow == 1 && row.scol == scol && row.arr != NULL)
      for(size_t i = 0; i < scol; i++)
        arr[i][r] = row.arr[i][0];
    else error(19);
  else error(20);
}
template <class type> void matrix<type>::setsingle(size_t s)
{
  if(s > 0)
  {
    setsize(s,s);
    for(size_t i = 0; i < s; i++)
      for(size_t j = 0; j < s; j++)
        if(i == j) arr[i][j] = 1;
        else arr[i][j] = 0;
  }
  else error(21);
}
template <class type> void matrix<type>::setminor(size_t cs, size_t rs, matrix<type> &m)
{
  if(arr != NULL)
    if(0 <= cs && cs + m.scol < scol && 0 <= rs && rs + m.srow < srow)
      for(size_t i = cs; i < cs + m.scol; i++)
        for(size_t j = rs; j < rs + m.srow; j++)
          arr[i][j] = m.arr[i - m.scol][j - m.srow];
    else error(22);
  else error(23);
}
template <class type> void matrix<type>::swapcol(size_t fc, size_t sc)
{
  if(arr != NULL)
    if(0 <= fc && fc < scol && 0 <= sc && sc < scol)
      for(size_t i = 0; i < srow; i++)
      {
        type temp = arr[fc][i];
        arr[fc][i] = arr[sc][i];
        arr[sc][i] = temp;
      }
    else error(24);
  else error(25);
}
template <class type> void matrix<type>::swaprow(size_t fr, size_t sr)
{
  if(arr != NULL)
    if(0 <= fr && fr < srow && 0 <= sr && sr < srow)
      for(size_t i = 0; i < scol; i++)
      {
        type temp = arr[i][fr];
        arr[i][fr] = arr[i][sr];
        arr[i][sr] = temp;
      }
    else error(26);
  else error(27);
}
template <class type> void matrix<type>::transp()
{
  if(arr != NULL)
    if(srow == scol)
      for(size_t i = 0; i < scol; i++)
        for(size_t j = 0; j < i; j++)
        {
          type temp = arr[i][j];
          arr[i][j] = arr[j][i];
          arr[j][i] = temp;
        }
    else error(28);
  else error(29);
}
template <class type> matrix<type> operator + (matrix<type> &m)
{
  matrix<type> res;
  if(arr != NULL && m.arr != NULL && srow == m.srow && scol == m.scol)
  {
    res.setsize(scol,srow);
    for(size_t i = 0; i < scol; i++)
      for(size_t j = 0; j < srow; j++)
        res.arr[i][j] += arr[i][j];
  }
  else error(30);
  return res;
}
template <class type> void matrix<type>::operator = (matrix<type> &m)
{
  this->initdinam(m.arr,m.scol,m.srow);
}
template <class type> matrix<type> matrix<type>::operator * (matrix<type> &m)
{
  matrix<type> res;
  if(arr != NULL && m.arr != NULL && scol == m.srow)
  {
    res.setsize(m.scol,srow);
    for(size_t i = 0; i < m.scol; i++)
      for(size_t j = 0; j < srow; j++)
      {
        res.arr[i][j] = 0;
        for(size_t k = 0; k < scol; k++)
          res.arr[i][j] += arr[k][j]*m.arr[i][k];
      }
  }
  else error(31);
  return res;
}
template <class type> matrix<type> matrix<type>::operator - (matrix<type> &m)
{
  matrix<type> res;
  if(arr != NULL && m.arr != NULL && srow == m.srow && scol == m.scol)
  {
    res.setsize(scol,srow);
    for(size_t i = 0; i < scol; i++)
      for(size_t j = 0; j < srow; j++)
        res.arr[i][j] = arr[i][j] - m.arr[i][j];
  }
  else error(32);
  return res;
}
template <class type> matrix<type> matrix<type>::operator * (type c)
{
  matrix<type> res;
  if(arr != NULL)
  {
    res.initdinam(arr,scol,srow);
    for(size_t i = 0; i < scol; i++)
      for(size_t j = 0; j < srow; j++)
        res.arr[i][j] *= c;
  }
  else error(33);
  return res;
}
template <class type> matrix<type> matrix<type>::operator - ()
{
  return (*this)*(-1);
}

template <class type> void matrix<type>::setcell(type val, size_t c, size_t r)
{
  if(arr != NULL && 0 <= c && c < scol && 0 <= r && r < srow)
    arr[c][r] = val;
  else error(34);
}
template <class type> void matrix<type>::error(int ne)
{
  exit(ne);
}
< Лекция 5 || Дополнительный материал 1: 12 || Дополнительный материал 2 >