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

Приложение

< Лекция 5 || Дополнительный материал 1: 12 || Дополнительный материал 2 >

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

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;
}
< Лекция 5 || Дополнительный материал 1: 12 || Дополнительный материал 2 >