#include <complex>
#include <vector>
vector< vector<complex> > complexMatrix;
#include <locale>
#include <valarray>
#include <ios>
#include <iostream>
#include <iomanip>
#include <stdexcept>
#include <ctime>
#include <cstdlib>
using namespace std;
#pragma region Класс complex декларация
class complex
{
public:
// Конструктор
complex(double re=0, double im=0);
// Оператор ()
complex& operator()(double re=0, double im=0);
complex& operator()(const complex& val);
// Оператор "равно"
bool operator==(const complex& val) const;
// Оператор "не равно"
bool operator!=(const complex& val) const;
// Возвращает действительную часть
double re(void) const;
static double re(const complex& val);
// Возвращает мнимую часть
double im(void) const;
static double im(const complex& val);
// Выделение модуля
double abs(void) const;
static double abs(const complex& val);
// Аргумент
double arg(void) const;
static double arg(const complex& val);
// Оператор сложения
friend complex operator+(const complex& val1,const complex& val2);
complex& operator+=(const complex& val);
// Оператор вычитания
friend complex operator-(const complex& val1,const complex& val2);
complex& operator-=(const complex& val);
// Оператор умножения
friend complex operator*(const complex& val1,const complex& val2);
complex& operator*=(const complex& val);
// Опрератор деления
friend complex operator/(const complex& val1,const complex& val2);
complex& operator/=(const complex& val);
// Возведение в степень
friend complex pow(const complex& val,double deg);
// Извлечение квадратного корня
friend complex sqrt(const complex& val);
// Операторы ввода/вывода
friend ostream& operator<<(ostream& stream,const complex& val);
friend istream& operator>>(istream& stream,complex& val);
private:
double _re;
double _im;
};
#pragma endregion
#pragma region class matrix
template<class T>
class matrix
{
class _row;
public:
// Конструктор создающий матрицу с rows-строками и cols-колонками
matrix(unsigned int rows,unsigned int cols);
// Конструктор копирования
matrix(const matrix<T>& matr);
// Оператор присваивания
matrix<T>& operator=(const matrix<T>& matr);
// Деструктор
virtual ~matrix();
_row& operator[](unsigned int row);
// Возвращает количество строк
unsigned int rows() const;
// Возвращает количество колонок
unsigned int cols() const;
private:
class _row
{
public:
T& operator[](unsigned int col);
private:
_row(unsigned int cols);
_row(const typename matrix<T>::_row& r);
_row& operator=(const typename matrix<T>::_row& r);
virtual ~_row();
T* _data;
unsigned int _cols;
friend class matrix;
};
_row** _data;
unsigned int _rows,_cols;
};
#pragma endregion class matrix
int rnd(int hi)
{
return static_cast<int>(static_cast<double>(rand())/RAND_MAX*hi);
}
int main()
{
srand(static_cast<unsigned int>(time(0)));
const unsigned int ROWS=5,COLS=8;
matrix<complex> arr(ROWS,COLS);
for(unsigned int i=0;i<arr.rows();++i)
{
for(unsigned int j=0;j<arr.cols();++j)
{
arr[i][j]=complex(rnd(20)-10,rnd(20)-10);
}
}
matrix<complex> arr2(arr);
arr=arr2;
for(unsigned int i=0;i<arr.rows();++i)
{
for(unsigned int j=0;j<arr.cols();++j)
{
cout<<arr[i][j]<<' ';
}
cout<<endl;
}
system("PAUSE");
return 0;
}
#pragma region Класс matrix определение
template<class T>
matrix<T>::matrix(unsigned int rows, unsigned int cols)
:_rows(rows)
,_cols(cols)
,_data(0)
{
_data=new _row*[_rows];
for(unsigned int i=0;i<_rows;++i)
{
_data[i]=new _row(_cols);
}
}
template<class T>
matrix<T>::matrix( const matrix<T>& matr )
:_rows(matr._rows)
,_cols(matr._cols)
,_data(0)
{
_data=new _row*[_rows];
for(unsigned int i=0;i<_rows;++i)
{
_data[i]=new _row(_cols);
*(_data[i])=*(matr._data[i]);
}
}
template<class T>
matrix<T>& matrix<T>::operator=( const matrix<T>& matr )
{
if(&matr!=this)
{
if(_data)
{
for(unsigned int i=0;i<_rows;++i)
{
delete _data[i];
}
delete []_data;
}
_rows=matr._rows;
_cols=matr._cols;
_data=new _row*[_rows];
for(unsigned int i=0;i<_rows;++i)
{
_data[i]=new _row(_cols);
*_data[i]=*matr._data[i];
}
}
return *this;
}
template<class T>
matrix<T>::~matrix()
{
if(_data)
{
for(unsigned int i=0;i<_rows;++i)
{
if(_data[i])delete _data[i];
}
delete []_data;
}
}
template<class T>
typename matrix<T>::_row& matrix<T>::operator[](unsigned int row)
{
if(row<_rows)return *_data[row];
else throw range_error("Bound(row) out of range");
}
template<class T>
inline unsigned int matrix<T>::cols() const
{
return _cols;
}
template<class T>
inline unsigned int matrix<T>::rows() const
{
return _rows;
}
template<class T>
matrix<T>::_row::~_row()
{
if(_data)delete []_data;
}
template<class T>
typename matrix<T>::_row& matrix<T>::_row::operator=(const typename matrix<T>::_row& r )
{
if(&r!=this)
{
if(_data)delete []_data;
_cols=r._cols;
_data=new T[_cols];
for(unsigned int i=0;i<_cols;++i)
{
_data[i]=r._data[i];
}
}
return *this;
}
template<class T>
matrix<T>::_row::_row(const typename matrix<T>::_row& r )
:_cols(r._cols)
,_data(0)
{
_data=new T[_cols];
for(unsigned int i=0;i<_cols;++i)
{
_data[i]=r._data[i];
}
}
template<class T>
matrix<T>::_row::_row( unsigned int cols )
:_cols(cols)
,_data(0)
{
_data=new T[cols];
}
template<class T>
T& matrix<T>::_row::operator [](unsigned int col)
{
if(col<_cols)return _data[col];
else throw range_error("Bound(col) out of range");
}
#pragma endregion Класс matrix определение
#pragma region Класс complex определение
// Конструктор
inline complex::complex(double re, double im)
: _re(re)
, _im(im)
{
}
inline complex& complex::operator()(double re, double im)
{
_re=re;
_im=im;
return *this;
}
inline complex& complex::operator ()(const complex& val)
{
_re=val._re;
_im=val._im;
return *this;
}
ostream& operator<<(ostream& stream,const complex& val)
{
if(val._re||!val._im)stream<<val._re;
if(val._im)
{
ios_base::fmtflags flags=stream.flags();
if(val._re)stream.setf(ios::showpos);
stream<<val._im<<'i';
stream.flags(flags);
}
return stream;
};
istream& operator>>(istream& stream,complex& val)
{
double re,im;
stream>>re>>im;
if(stream.good())
{
val._re=re;
val._im=im;
}
return stream;
};
inline bool complex::operator==(const complex& val) const
{
return (_re==val._re)&&(_im==val._im);
}
inline bool complex::operator!=(const complex& val) const
{
return (_re!=val._re)||(_im!=val._im);
}
// Возвращает действительную часть
inline double complex::re(void) const
{
return _re;
}
inline double complex::re(const complex& val)
{
return val._re;
}
// Возвращает мнимую часть
inline double complex::im(void) const
{
return _im;
}
inline double complex::im(const complex& val)
{
return val._im;
}
// Оператор сложения
inline complex operator+(const complex& val1,const complex& val2)
{
return complex(val1._re+val2._re,val1._im+val2._im);
}
inline complex& complex::operator+=(const complex& val)
{
_re+=val._re;
_im+=val._im;
return *this;
}
// Оператор вычитания
inline complex operator-(const complex& val1,const complex& val2)
{
return complex(val1._re-val2._re,val1._im-val2._im);
}
inline complex& complex::operator-=(const complex& val)
{
_re-=val._re;
_im-=val._im;
return *this;
}
// Оператор умножения
complex operator*(const complex& val1,const complex& val2)
{
return complex(val1._re*val2._re-val1._im*val2._im,val1._re*val2._im+val1._im*val2._re);
}
complex& complex::operator*=(const complex& val)
{
_re=_re*val._re-_im*val._im;
_im=_re*val._im+_im*val._re;
return *this;
}
// Опрератор деления
complex operator/(const complex& val1,const complex& val2)
{
double div=val2._re*val2._re+val2._im*val2._im;
return complex((val1._re*val2._re+val1._im*val2._im)/div,(val1._im*val2._re-val1._re*val2._im)/div);
}
complex& complex::operator/=(const complex& val)
{
double div=val._re*val._re+val._im*val._im;
_re=(_re*val._re+_im*val._im)/div;
_im=(_im*val._re-_re*val._im)/div;
return *this;
}
// Выделение модуля
inline double complex::abs(void) const
{
return std::sqrt(_re*_re+_im*_im);
}
inline double complex::abs(const complex& val)
{
return std::sqrt(val._re*val._re+val._im*val._im);
}
// Аргумент
inline double complex::arg(void) const
{
return acos(_re/abs());
}
inline double complex::arg(const complex& val)
{
return acos(val._re/val.abs());
}
complex pow(const complex& val,double deg)
{
double r=std::pow(val.abs(),deg),a=val.arg()*deg;
return complex(r*cos(a),r*sin(a));
}
complex sqrt(const complex& val)
{
double r=std::sqrt(val.abs()),a=val.arg()/2;
return complex(r*cos(a),r*sin(a));
}
#pragma endregion Класс complex определение
9+5i -3+7i 5+3i 4-3i -9+6i -7-3i -8-1i 3+9i
8-4i 2+7i -2+8i -9-2i -8-3i 3-10i -7-9i 9
2-2i -2+5i -1+3i -5-6i 3+4i 6+1i 8+4i -10-8i
-1 4-3i 5-10i 5-9i 4-6i 5+1i -10+9i -6+6i
-7i 9i 5-10i 7-9i 8-7i 8+8i -7i -10-9i
//Complex.h
#ifndef COMPLEX_H
#define COMPLEX_H
//#pragma region Класс complex декларация
class matrix;
class complex
{
private:
double _re;
double _im;
public:
// Конструктор
complex(double re=0, double im=0);
// Оператор ()
complex& operator()(double re=0, double im=0);
complex& operator()(const complex& val);
// Оператор "равно"
bool operator==(const complex& val) const;
// Оператор "не равно"
bool operator!=(const complex& val) const;
// Возвращает действительную часть
double re(void) const;
static double re(const complex& val);
// Возвращает мнимую часть
double im(void) const;
static double im(const complex& val);
// Выделение модуля
double abs(void) const;
static double abs(const complex& val);
// Аргумент
double arg(void) const;
static double arg(const complex& val);
// Оператор сложения
complex operator+(const complex& val1,const complex& val2);
complex& operator+=(const complex& val);
// Оператор вычитания
complex operator-(const complex& val1,const complex& val2);
complex& operator-=(const complex& val);
// Оператор умножения
complex operator*(const complex& val1,const complex& val2);
complex& operator*=(const complex& val);
// Опрератор деления
complex operator/(const complex& val1,const complex& val2);
complex& operator/=(const complex& val);
// Возведение в степень
complex pow(const complex& val,double deg);
// Извлечение квадратного корня
complex sqrt(const complex& val);
// Операторы ввода/вывода
//ostream& operator<<(ostream& stream,const complex& val);
//stream& operator>>(istream& stream,complex& val);
};
//#pragma endregion
#endif
//Complex.cpp
#include <stdio.h>
#include <iostream>
#include <math.h>
#include "Complex.h"
#include "Matrix.h"
using namespace std;
//#pragma region Класс complex определение
// Конструктор
inline complex::complex(double re, double im)
: _re(re), _im(im)
{
}
inline complex& complex::operator()(double re, double im)
{
_re=re;
_im=im;
return *this;
}
inline complex& complex::operator ()(const complex& val)
{
_re=val._re;
_im=val._im;
return *this;
}
ostream& operator<<(ostream& stream,const complex& val)
{
if(val._re||!val._im)stream<<val._re;
if(val._im)
{
ios_base::fmtflags flags=stream.flags();
if(val._re)stream.setf(ios::showpos);
stream<<val._im<<'i';
stream.flags(flags);
}
return stream;
};
istream& operator>>(istream& stream,complex& val)
{
double re,im;
stream>>re>>im;
if(stream.good())
{
val._re=re;
val._im=im;
}
return stream;
};
inline bool complex::operator==(const complex& val) const
{
return (_re==val._re)&&(_im==val._im);
}
inline bool complex::operator!=(const complex& val) const
{
return (_re!=val._re)||(_im!=val._im);
}
// Возвращает действительную часть
inline double complex::re(void) const
{
return _re;
}
inline double complex::re(const complex& val)
{
return val._re;
}
// Возвращает мнимую часть
inline double complex::im(void) const
{
return _im;
}
inline double complex::im(const complex& val)
{
return val._im;
}
// Оператор сложения
inline complex operator+(const complex& val1,const complex& val2)
{
return complex(val1._re+val2._re,val1._im+val2._im);
}
inline complex& complex::operator+=(const complex& val)
{
_re+=val._re;
_im+=val._im;
return *this;
}
// Оператор вычитания
inline complex operator-(const complex& val1,const complex& val2)
{
return complex(val1._re-val2._re,val1._im-val2._im);
}
inline complex& complex::operator-=(const complex& val)
{
_re-=val._re;
_im-=val._im;
return *this;
}
// Оператор умножения
complex operator*(const complex& val1,const complex& val2)
{
return complex(val1._re*val2._re-val1._im*val2._im,val1._re*val2._im+val1._im*val2._re);
}
complex& complex::operator*=(const complex& val)
{
_re=_re*val._re-_im*val._im;
_im=_re*val._im+_im*val._re;
return *this;
}
// Опрератор деления
complex operator/(const complex& val1,const complex& val2)
{
double div=val2._re*val2._re+val2._im*val2._im;
return complex((val1._re*val2._re+val1._im*val2._im)/div,(val1._im*val2._re-val1._re*val2._im)/div);
}
complex& complex::operator/=(const complex& val)
{
double div=val._re*val._re+val._im*val._im;
_re=(_re*val._re+_im*val._im)/div;
_im=(_im*val._re-_re*val._im)/div;
return *this;
}
// Выделение модуля
inline double complex::abs(void) const
{
return std::sqrt(_re*_re+_im*_im);
}
inline double complex::abs(const complex& val)
{
return std::sqrt(val._re*val._re+val._im*val._im);
}
// Аргумент
inline double complex::arg(void) const
{
return acos(_re/abs());
}
inline double complex::arg(const complex& val)
{
return acos(val._re/val.abs());
}
complex pow(const complex& val,double deg)
{
double r=std::pow(val.abs(),deg),a=val.arg()*deg;
return complex(r*cos(a),r*sin(a));
}
complex sqrt(const complex& val)
{
double r=std::sqrt(val.abs()),a=val.arg()/2;
return complex(r*cos(a),r*sin(a));
}
//#pragma endregion Класс complex определение
//Matrix.h
#ifndef MATRIX_H
#define MATRIX_H
//#pragma region class matrix
template<class T>
class matrix
{
public:
class _row
{
public:
T& operator[](unsigned int col);
private:
_row(unsigned int cols);
_row(const typename matrix<T>::_row& r);
_row& operator=(const typename matrix<T>::_row& r);
virtual ~_row();
T* _data;
unsigned int _cols;
friend class matrix;
}
_row** _data;
unsigned int _rows,_cols;
public:
// Конструктор создающий матрицу с rows-строками и cols-колонками
matrix(unsigned int rows,unsigned int cols);
// Конструктор копирования
matrix(const matrix<T>& matr);
// Оператор присваивания
matrix<T>& operator=(const matrix<T>& matr);
// Деструктор
virtual ~matrix();
_row& operator[](unsigned int row);
// Возвращает количество строк
unsigned int rows() const;
// Возвращает количество колонок
unsigned int cols() const;
};
//#pragma endregion class matrix
#endif
//Matrix.cpp
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <iomanip>
#include <string.h>
#include "Matrix.h"
#include "Complex.h"
using namespace std;
//#pragma region Класс matrix определение
template<class T>
matrix<T>::matrix(unsigned int rows, unsigned int cols)
:_rows(rows)
,_cols(cols)
,_data(0)
{
_data=new _row*[_rows];
for(unsigned int i=0;i<_rows;++i)
{
_data[i]=new _row(_cols);
}
}
template<class T>
matrix<T>::matrix( const matrix<T>& matr )
:_rows(matr._rows)
,_cols(matr._cols)
,_data(0)
{
_data=new _row*[_rows];
for(unsigned int i=0;i<_rows;++i)
{
_data[i]=new _row(_cols);
*(_data[i])=*(matr._data[i]);
}
}
template<class T>
matrix<T>& matrix<T>::operator=( const matrix<T>& matr )
{
if(&matr!=this)
{
if(_data)
{
for(unsigned int i=0;i<_rows;++i)
{
delete _data[i];
}
delete []_data;
}
_rows=matr._rows;
_cols=matr._cols;
_data=new _row*[_rows];
for(unsigned int i=0;i<_rows;++i)
{
_data[i]=new _row(_cols);
*_data[i]=*matr._data[i];
}
}
return *this;
}
template<class T>
matrix<T>::~matrix()
{
if(_data)
{
for(unsigned int i=0;i<_rows;++i)
{
if(_data[i])delete _data[i];
}
delete []_data;
}
}
template<class T>
typename matrix<T>::_row& matrix<T>::operator[](unsigned int row)
{
if(row<_rows)return *_data[row];
else throw range_error("Bound(row) out of range");
}
template<class T>
inline unsigned int matrix<T>::cols() const
{
return _cols;
}
template<class T>
inline unsigned int matrix<T>::rows() const
{
return _rows;
}
template<class T>
matrix<T>::_row::~_row()
{
if(_data)delete []_data;
}
template<class T>
typename matrix<T>::_row& matrix<T>::_row::operator=(const typename matrix<T>::_row& r )
{
if(&r!=this)
{
if(_data)delete []_data;
_cols=r._cols;
_data=new T[_cols];
for(unsigned int i=0;i<_cols;++i)
{
_data[i]=r._data[i];
}
}
return *this;
}
template<class T>
matrix<T>::_row::_row(const typename matrix<T>::_row& r )
:_cols(r._cols)
,_data(0)
{
_data=new T[_cols];
for(unsigned int i=0;i<_cols;++i)
{
_data[i]=r._data[i];
}
}
template<class T>
matrix<T>::_row::_row( unsigned int cols )
:_cols(cols)
,_data(0)
{
_data=new T[cols];
}
template<class T>
T& matrix<T>::_row::operator [](unsigned int col)
{
if(col<_cols)return _data[col];
else throw range_error("Bound(col) out of range");
}
//#pragma endregion Класс matrix определение
//main.cpp
#include <locale>
#include <valarray>
#include <ios>
#include <iostream>
#include <iomanip>
#include <exception>
#include <ctime>
#include <cstdlib>
#include "Matrix.h"
using namespace std;
int main()
{
srand(static_cast<unsigned int>(time(0)));
const unsigned int ROWS=5,COLS=8;
matrix<complex> arr(ROWS,COLS);
for(unsigned int i=0;i<arr.rows();++i)
{
for(unsigned int j=0;j<arr.cols();++j)
{
arr[i][j]=complex(rnd(20)-10,rnd(20)-10);
}
}
matrix<complex> arr2(arr);
arr=arr2;
for(unsigned int i=0;i<arr.rows();++i)
{
for(unsigned int j=0;j<arr.cols();++j)
{
cout<<arr[i][j]<<' ';
}
cout<<endl;
}
system("PAUSE");
return 0;
}
Если Вы уже зарегистрированы на Портале - войдите в систему, если Вы еще не регистрировались - пройдите простую процедуру регистрации.