Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //.h
- #pragma once
- #ifndef MATRIX_H
- #define MATRIX_H
- class Matrix {
- public:
- Matrix();
- Matrix(int m, int n);
- Matrix(int tab[]);
- Matrix(Matrix &m);
- ~Matrix();
- void ChangeSize(int n, int m);
- int GetColumns();
- int GetRows();
- void FillRandom();
- void Print();
- int **_tab;
- Matrix operator = (Matrix m);
- Matrix operator + (Matrix m);
- Matrix operator - (Matrix m);
- Matrix operator * (float m);
- Matrix operator * (Matrix m);
- Matrix operator += (Matrix m);
- Matrix operator -= (Matrix m);
- Matrix operator *= (float m);
- Matrix operator *= (Matrix m);
- bool operator == (Matrix m);
- bool operator != (Matrix m);
- Matrix operator >> (Matrix m);
- void operator << (Matrix m);
- private:
- int columns;
- int rows;
- };
- #endif
- //end.h
- //.cpp
- #include "matrix.h"
- #include <random>
- #include <iostream>
- #define RAND_MAX = 1;
- using namespace std;
- Matrix::Matrix(int n, int m) {
- rows = n;
- columns = m;
- _tab = new int*[n];
- for (int i = 0; i < n; ++i)
- {
- _tab[i] = new int[m];
- }
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- _tab[i][j] = 0;
- }
- }
- }
- Matrix::Matrix(int tab[]) {
- rows = tab[0];
- columns = tab[1];
- _tab = new int*[tab[0]];
- for (int i = 0; i < tab[0]; ++i)
- {
- _tab[i] = new int[tab[1]];
- }
- for (int i = 0; i < tab[0]; i++) {
- for (int j = 0; j < tab[1]; j++) {
- _tab[i][j] = 0;
- }
- }
- }
- Matrix::Matrix() {
- _tab = new int*[0];
- _tab[0] = new int[0];
- }
- Matrix::~Matrix() {
- for (int i = 0; i < rows; ++i)
- {
- delete _tab[i];
- }
- delete _tab;
- }
- int Matrix::GetColumns() {
- return columns;
- }
- int Matrix::GetRows() {
- return rows;
- }
- void Matrix::FillRandom() {
- for (int i = 0; i < GetRows(); i++) {
- for (int j = 0; j < GetColumns(); j++) {
- _tab[i][j] = (rand() %2 );
- }
- }
- }
- void Matrix::Print() {
- for (int i = 0; i < GetRows(); i++) {
- for (int j = 0; j < GetColumns(); j++) {
- printf("%d", _tab[i][j]);
- }
- printf("\n");
- }
- }
- void Matrix::ChangeSize(int n, int m) {
- rows = n;
- columns = m;
- _tab = new int*[n];
- for (int i = 0; i < n; ++i)
- {
- _tab[i] = new int[m];
- }
- for (int i = 0; i < n; ++i)
- {
- for (int j = 0; j < n; ++j)
- {
- _tab[i][j] = 0;
- }
- }
- }
- Matrix::Matrix(Matrix &m) {
- _tab = new int*[m.GetRows()];
- for (int i = 0; i < m.GetRows(); ++i)
- {
- _tab[i] = new int[m.GetColumns()];
- }
- for (int i = 0; i < m.GetRows(); i++) {
- for (int j = 0; j < m.GetColumns(); j++) {
- _tab[i][j] = m._tab[i][j];
- }
- }
- }
- Matrix Matrix::operator=(Matrix m) {
- if (rows == m.GetRows() && columns == m.GetColumns()) {
- Matrix r(rows, columns);
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < columns; j++) {
- r._tab[i][j] = this->_tab[i][j] - m._tab[i][j];
- }
- }
- }
- }
- Matrix Matrix::operator+(Matrix m) {
- if (rows == m.GetRows() && columns == m.GetColumns()) {
- Matrix r(rows, columns);
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < columns; j++) {
- r._tab[i][j] = this->_tab[i][j] + m._tab[i][j];
- }
- }
- return r;
- }
- return NULL;
- }
- Matrix Matrix::operator-(Matrix m) {
- if (rows == m.GetRows() && columns == m.GetColumns()) {
- Matrix r(rows, columns);
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < columns; j++) {
- r._tab[i][j] = this->_tab[i][j] - m._tab[i][j];
- }
- }
- return r;
- }
- return NULL;
- }
- Matrix Matrix::operator*(float m) {
- Matrix r(rows, columns);
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < columns; j++) {
- r._tab[i][j] = this->_tab[i][j] * m;
- }
- }
- return r;
- }
- Matrix Matrix::operator*(Matrix m) {
- if (rows == m.GetRows() && columns == m.GetColumns()) {
- Matrix r(rows, columns);
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < columns; j++) {
- r._tab[i][j] = this->_tab[i][j] * m._tab[i][j];
- }
- }
- return r;
- }
- return NULL;
- }
- Matrix Matrix::operator+=(Matrix m) {
- if (rows == m.GetRows() && columns == m.GetColumns()) {
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < columns; j++) {
- _tab[i][j] += m._tab[i][j];
- }
- }
- }
- }
- Matrix Matrix::operator-=(Matrix m) {
- if (rows == m.GetRows() && columns == m.GetColumns()) {
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < columns; j++) {
- _tab[i][j] -= m._tab[i][j];
- }
- }
- }
- }
- Matrix Matrix::operator *= (float m) {
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < columns; j++) {
- _tab[i][j] *= m;
- }
- }
- }
- Matrix Matrix::operator *= (Matrix m) {
- if (rows == m.GetRows() && columns == m.GetColumns()) {
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < columns; j++) {
- _tab[i][j] *= m._tab[i][j];
- }
- }
- }
- }
- bool Matrix::operator == (Matrix m) {
- if (rows == m.GetRows() && columns == m.GetColumns()) {
- bool r = true;
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < columns; j++) {
- if (_tab[i][j] != m._tab[i][j]) {
- r = false;
- }
- }
- }
- return r;
- }
- return NULL;
- }
- bool Matrix::operator != (Matrix m) {
- if (rows == m.GetRows() && columns == m.GetColumns()) {
- bool r = false;
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < columns; j++) {
- if (_tab[i][j] != m._tab[i][j]) {
- r = true;
- }
- }
- }
- return r;
- }
- return NULL;
- }
- Matrix Matrix::operator >> (Matrix m) {
- for (int i = 0; i < m.GetRows(); i++) {
- for (int j = 0; j < m.GetColumns(); j++) {
- cout << std::endl << "Matrix[" << i << "][" << j << "]";
- cin >> m._tab[i][j];
- }
- }
- }
- void Matrix::operator << (Matrix m) {
- if (rows == m.GetRows() && columns == m.GetColumns()) {
- m.Print();
- }
- }
- //.cpp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement