Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include "Matrix.hpp"
- using namespace std;
- // получение элемента матрицы
- double Matrix::GetAij(int i, int j) const {
- if((i>=m)||(j>=n)||(i<0)||(j<0))
- throw "Ошибка! НЕверные индексы! Выход за границы массива.";
- return p[i][j];
- }
- // вставка элемента матрицы
- void Matrix::SetAij(int i, int j, double ch) {
- if((i>=m)||(j>=n)||(i<0)||(j<0))
- throw "Ошибка! НЕверные индексы! Выход за границы массива.";
- p[i][j]=ch;
- }
- // задание ID
- int Matrix::NextID=0;
- //функция получения ID
- int Matrix::GetID()const{return ID;}
- // конструктор без параметров
- Matrix::Matrix(): m(0), n(0), p(NULL), ID(++NextID) {}
- // конструктор с параметрами
- Matrix::Matrix(int am, int an): ID(++NextID) {
- if (am<=0 || an<=0)
- throw "Ошибка! НЕверно задано количество строк/столбцов!";
- m=am; n=an;
- p=new double*[m];
- for (int i=0; i<m; i++)
- p[i]=new double[n];
- for (int i=0; i<m; i++)
- for (int j=0; j<n; j++)
- p[i][j]=0;
- }
- // конструктор копирования
- Matrix::Matrix(const Matrix& b): ID(++NextID) {
- m=b.m; n=b.n;
- p=new double*[m];
- for (int i=0; i<m; i++)
- p[i]=new double[n];
- for (int i=0; i<m; i++)
- for (int j=0; j<n; j++)
- p[i][j]=b.p[i][j];
- }
- // деструктор
- Matrix::~Matrix() {
- for (int i=0; i<m; i++)
- delete []p[i];
- delete []p;
- m=0; n=0;
- }
- // оператор присваивания
- Matrix& Matrix::operator=(const Matrix& b) {
- if (*this==b) // -/&
- return *this;
- for (int i=0; i<m; i++)
- delete []p[i];
- delete []p;
- m=b.m; n=b.n;
- p=new double*[m];
- for (int i=0; i<m; i++)
- p[i]=new double[n];
- for (int i=0; i<m; i++)
- for (int j=0; j<n; j++)
- p[i][j]=b.p[i][j];
- return *this;
- }
- // ________________составные операторы присваивания_____________________
- // сложение + присваиваине
- Matrix& Matrix::operator+=(const Matrix& b) {
- if (m!=b.m || n!=b.n)
- throw "Ошибка! Матрицы должны быть равных размеров!(сложение)";
- for (int i=0; i<m; i++)
- for (int j=0; j<n; j++)
- p[i][j]+=b.p[i][j];
- return *this;
- }
- // вычитание + присваивание
- Matrix& Matrix::operator-=(const Matrix& b) {
- if (m!=b.m || n!=b.n)
- throw "Ошибка! Матрицы должны быть равных размеров!(разность)";
- for (int i=0; i<m; i++)
- for (int j=0; j<n; j++)
- p[i][j]-=b.p[i][j];
- return *this;
- }
- // умножение + присваивание
- Matrix& Matrix::operator*=(const Matrix& b) {
- if (n!=b.m)
- throw "Ошибка! Неверный размер матриц(число столбцов в 1 должно = числу строк 2). ";
- Matrix result(m, b.n);
- for (int i=0; i<m; i++)
- for (int j=0; j<b.n; j++)
- for (int k=0; k<n; k++)
- result.p[i][j]+=p[i][k]*b.p[k][j];
- *this=result;
- return *this;
- }
- // умножение на константу + присваивание
- Matrix& Matrix::operator*=(double c) {
- for (int i=0; i<m; i++)
- for (int j=0; j<n; j++)
- p[i][j]*=c;
- return *this;
- }
- // деление на константу + присваивание
- Matrix& Matrix::operator/=(double c) {
- for (int i=0; i<m; i++)
- for (int j=0; j<n; j++)
- p[i][j]/=c;
- return *this;
- }
- //_______________Бинарные арифметические операции________________
- // умножение на константу (справа)
- Matrix Matrix::operator*(double c) {
- Matrix b(*this);
- for (int i=0; i<b.m; i++)
- for (int j=0; j<b.n; j++)
- b.p[i][j]*=c;
- return b;
- }
- // дружественное умножение (константа слева)
- Matrix operator*(double c, const Matrix& a) {
- Matrix b(a);
- for (int i=0; i<b.m; i++)
- for (int j=0; j<b.n; j++)
- b.p[i][j]*=c;
- return b;
- }
- // деление на константу
- Matrix Matrix::operator/(double c) {
- Matrix b(*this);
- if (c==0){
- throw "Ошибка! На нуль делить нельзя!";
- }
- for (int i=0; i<b.m; i++)
- for (int j=0; j<b.n; j++)
- b.p[i][j]/=c;
- return b;
- }
- // сложение матриц
- Matrix Matrix::operator+(const Matrix& b) {
- Matrix a(*this);
- a+=b;
- return a;
- }
- // вычитание матриц
- Matrix Matrix::operator-(const Matrix& b) {
- Matrix a(*this);
- a-=b;
- return a;
- }
- // умножение матриц
- Matrix Matrix::operator*(const Matrix& b) {
- Matrix a(*this);
- a*=b;
- return a;
- }
- // сравнение матриц
- bool Matrix::operator==(const Matrix& b) const {
- if (m!=b.m || n!=b.n)
- return false;
- for (int i=0; i<m; i++)
- for (int j=0; j<n; j++)
- if (p[i][j]!=b.p[i][j])
- return false;
- return true;
- }
- // оператор не равно!
- bool Matrix::operator!=(const Matrix& b) const {
- return !(*this==b);
- }
- // ввод матриц
- istream& operator>>(istream& cin, Matrix& mtr) {
- if (mtr.m==0 || mtr.n==0)
- throw "Ошибка! Нет матрицы размером 0x0";
- cout<<"Введите матрицу по строкам, размером "<<mtr.m<<" x "<<mtr.n<<":"<<endl;
- for (int i=0; i<mtr.m; i++)
- for (int j=0; j<mtr.n; j++)
- cin>>mtr.p[i][j];
- return cin;
- }
- // вывод матриц
- ostream& operator<<(ostream& cout, const Matrix& mtr) {
- if (mtr.m==0 || mtr.n==0)
- throw "Ошибка! Нет матрицы размером 0x0";
- for (int i=0; i<mtr.m; i++) {
- for (int j=0; j<mtr.n; j++)
- cout<<setw(8)<<mtr.p[i][j];
- cout<<endl;
- }
- return cout;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement