Программирование под Windows в среде Visual C++ 2005

Приложение

Показывать лекцию целиком

Класс матриц

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);
}

Класс полиномов

TPolinom.h

;
#include <vector>
using namespace std;
template <class A> class Polinom
{
public:
  vector<A> Pol; //a0 + a1*x + a2*x^2 + ... + an*x^n
public:
  Polinom(){Pol.resize(1); Pol[0] = 0;}
  Polinom(A a){Pol.resize(1); Pol[0] = a;}
  Polinom(int n, A *koef){Pol.resize(n); for(int i = 0; i < n; i++) Pol[i] = koef[i];}
  Polinom(const Polinom initPol){Pol = initPol.Pol;}
  Polinom<A> operator - ();
  Polinom<A> operator + (Polinom<A>);
  Polinom<A> operator - (Polinom<A>);
  Polinom<A> operator * (Polinom<A>);
  Polinom<A> operator = (Polinom<A>);
  bool operator == (Polinom<A>);
  A operator () (A arg);
};

TPolinom.cpp

template <class A> Polinom<A> Polinom<A> :: operator + (Polinom<A> add)
{
  Polinom<A> result;
  Polinom<A> *md;
  size_t maxdeg, mindeg;
  if(this->Pol.size() <= add.Pol.size())
    md = add;
  else
    md = this;
  maxdeg = max(this->Pol.size(),add.Pol.size());
  mindeg = min(this->Pol.size(),add.Pol.size());
  result.Pol.resize(maxdeg);
  for(unsigned int i = 0; i < mindeg; i++)
    result.Pol[i] = this->Pol[i] + add.Pol[i];
  for(size_t i = mindeg; i < maxdeg; i++)
    result.Pol[i] = md->Pol[i];
  return result;
}
template <class A> Polinom<A> Polinom<A> :: operator - ()
{
  Polinom result;
  for(uni i = 0; i < this->Pol.size(); i++)
    result.Pol[i] = -this->Pol[i];
  return result;
}
template <class A> Polinom<A> Polinom<A> :: operator - (Polinom<A> sub)
{
  Polinom result;
  result = *this + (-sub);
  return result;
}
template <class A> Polinom<A> Polinom<A> :: operator * (Polinom<A> mult)
{
  Polinom<A> result;
  result.Pol.resize(this->Pol.size() + mult.Pol.size() - 1);
  for(unsigned int i = 0; i < this->Pol.size(); i++)
    for(unsigned int j = 0; j < mult.Pol.size(); j++)
      result.Pol[i+j] += this->Pol[i]*mult.Pol[j];
  return result;
}
template <class A> Polinom<A> Polinom<A> :: operator = (Polinom<A> right)
{
   this->Pol = right.Pol; return *this; 
}
template <class A> A Polinom<A> :: operator () (A arg)
{
  size_t n = Pol.size();
  A result = Pol[n-1]*arg + Pol[n-2];
  for(size_t i = n-1; i > 1; i--)
    result = result*arg + Pol[i-2];
  return result;
}
template <class A> bool Polinom<A> :: operator == (Polinom<A> comp)
{
  bool result = true;
  if(Pol.size() != comp.Pol.size()) return false;
  for(unsigned int i = 0; i < Pol.size(); i++)
    if(Pol[i] == comp.Pol[i]) continue;
    else result = false;
  return result;
}

Класс комплексных чисел

Complex.h

#pragma once

class Complex
{
  double Re;
  double Im;
public:
  Complex():Re(0.0),Im(0.0){}
  Complex(double x, double y):Re(x),Im(y){}
  Complex(Complex z){Re=z.Re; Im = z.Im;}
  ~Complex(){}
  double GetRe(){return Re;}
  double GetIm(){return Im;}
  void SetRe(double x){Re = x;}
  void SetIm(double y){Im = y;}

  friend Complex operator * (double op1, Complex op2);
  friend Complex operator + (double op1, Complex op2);
  friend Complex operator - (double op1, Complex op2);
  friend Complex operator / (double op1, Complex op2);
  Complex operator * (double op);
  Complex operator * (Complex op);
  Complex operator + (double op);
  Complex operator + (Complex op);
  Complex operator - (double op);
  Complex operator - (Complex op);
  Complex operator / (double op);
  Complex operator / (Complex op);
  void operator = (Complex op);
  void operator += (Complex op);
  void operator -= (Complex op);
  void operator *= (Complex op);
  void operator /= (Complex op);
  void operator += (double op);
  void operator -= (double op);
  void operator *= (double op);
  void operator /= (double op);
  bool operator == (Complex op);
  Complex operator - ();

  double Abs(){return Re*Re + Im*Im;}
  double Arg(){return atan2(Im,Re);}
};

Complex exp(Complex z);
Complex sin(Complex z);
Complex cos(Complex z);
Complex pow(Complex z_base, Complex z_pow);
Complex ln(Complex z);

Complex.cpp

#include "Complex.h"

Complex Complex::operator + (Complex op)
{
  Complex Res;
  Res.Re = Re + op.Re;
  Res.Im = Im + op.Im;
  return Res;
}
Complex Complex::operator * (Complex op)
{
  Complex Res;
  Res.Re = Re*op.Re - Im*op.Im;
  Res.Im = Re*op.Im + Im*op.Re;
  return Res;
}
Complex Complex::operator * (double op)
{
  return *this*Complex(op,0);
}
Complex Complex::operator - ()
{
  return *this*(-1.0);
}
Complex operator * (double op1, Complex op2)
{
  return op2*op1;
}
Complex Complex::operator + (double op)
{
  return *this + Complex(op,0);
}
Complex operator + (double op1, Complex op2)
{
  return op2 + op1;
}
Complex Complex::operator / (Complex op)
{
  Complex Res;
  Res.Re = (Re*op.Re + Im*op.Im)/(op.Re*op.Re + op.Im*op.Im);
  Res.Im = (Im*op.Re - Re*op.Im)/(op.Re*op.Re + op.Im*op.Im);
  return Res;
}
Complex Complex::operator / (double op)
{
  return *this/Complex(op,0);
}
Complex operator / (double op1, Complex op2)
{
  return Complex(op1,0)/op2;
}
Complex Complex::operator - (Complex op)
{
  return *this + (-op);
}
Complex Complex::operator - (double op)
{
  return *this - Complex(op,0);
}
Complex operator - (double op1, Complex op2)
{
  return Complex(op1,0) - op2;
}
void Complex::operator = (Complex op)
{
  Re = op.Re;
  Im = op.Im;
}
bool Complex::operator == (Complex op)
{
  return Re==op.Re  Im==op.Im ? true : false;
}
void Complex::operator += (Complex op)
{
  *this = *this + op;
}
void Complex::operator += (double op)
{
  *this = *this + op;
}
void Complex::operator -= (Complex op)
{
  *this = *this - op;
}
void Complex::operator -= (double op)
{
  *this = *this - op;
}
void Complex::operator *= (Complex op)
{
  *this = *this*op;
}
void Complex::operator *= (double op)
{
  *this = *this*op;
}
void Complex::operator /= (Complex op)
{
  *this = *this/op;
}
void Complex::operator /= (double op)
{
  *this = *this/op;
}

Complex exp(Complex z)
{
  Complex Res;
  Res.SetRe(exp(z.GetRe())*cos(z.GetIm()));
  Res.SetIm(exp(z.GetRe())*sin(z.GetIm()));
  return Res;
}

Complex ln(Complex z)
{
  Complex Res;
  Res.SetRe(log(z.Abs()));
  Res.SetIm(z.Arg());
  return Res;
}

Complex pow(Complex z_base, Complex z_pow)
{
  if(z_pow == Complex())return Complex(1,0);
  else return exp(z_base*ln(z_pow));
}

Complex sin(Complex z)
{
  Complex Res;
  Res.SetRe(sin(z.GetRe())*(exp(z.GetIm()) + exp(-z.GetIm()))/2);
  Res.SetIm(cos(z.GetRe())*(exp(z.GetIm()) - exp(-z.GetIm()))/2);
  return Res;
}

Complex cos(Complex z)
{
  Complex Res;
  Res.SetRe(cos(z.GetRe())*(exp(z.GetIm()) + exp(-z.GetIm()))/2);
  Res.SetIm(-sin(z.GetRe())*(exp(z.GetIm()) - exp(-z.GetIm()))/2);
  return Res;
}
Вернуться к учебному плану