Untitled
unknown
c_cpp
2 years ago
6.2 kB
5
Indexable
// // ФАЙЛ Matrix.h // #pragma once #include <iostream> using namespace std; class Matrix { float** ptr; int rows, columns; public: //Конструктор без параметрів Matrix() { this->rows = 3; this->columns = 3; this->ptr = new float* [rows]; for (int i = 0; i < rows; i++) { this->ptr[i] = new float[columns]; for (int j = 0; j < columns; j++) { this->ptr[i][j] = (i+1)*(j+1); } } } //Конструктор з параметрами Matrix(int row, int column, float** ptr) { this->rows = row; this->columns = column; this->ptr = ptr; } //Конструктор копіювання Matrix(const Matrix& matrix) : rows(matrix.rows), columns(matrix.columns), ptr(matrix.ptr) { } //Getter для rows int getRows() const { return this->rows; } //Getter для columns int getColumns() const { return this->columns; } //Getter для ptr float ** getPtr() const { return this->ptr; } //Перевантаження операції = Matrix& operator = (Matrix m) { this->rows = m.getRows(); this->columns = m.getColumns(); this->ptr = new float* [m.getRows()]; for (int i = 0; i < m.getRows(); i++) { this->ptr[i] = new float[m.getColumns()]; for (int j = 0; j < m.getColumns(); j++) { this->ptr[i][j] = m.getPtr()[i][j]; } } return *this; } //Перевантаження операції + Matrix operator + (Matrix &other) const { if ((this->getRows() != other.getRows()) || (this->getColumns() != other.getColumns())) { cout << "Impossible to add!" << endl; } else { auto** ptr = new float*[this->getRows()]; for (int i = 0; i < this->getRows(); i++) { ptr[i] = new float[this->getColumns()]; } for (int i = 0; i < this->getRows(); i++) { for (int j = 0; j < this->getColumns(); j++) { ptr[i][j] = this->getPtr()[i][j] + other.getPtr()[i][j]; } } Matrix res(this->getRows(), this->getColumns(), ptr); return res; } return {}; } //Перевантаження операції - Matrix operator - (Matrix &other) { if ((this->getRows() != other.getRows()) || (this->getColumns() != other.getColumns())) { cout << "Impossible to subtract!" << endl; } else { auto** ptr = new float* [this->getRows()]; for (int i = 0; i < this->getRows(); i++) { ptr[i] = new float[this->getColumns()]; } for (int i = 0; i < this->getRows(); i++) { for (int j = 0; j < this->getColumns(); j++) { ptr[i][j] = this->getPtr()[i][j] - other.getPtr()[i][j]; } } Matrix res(this->getRows(), this->getColumns(), ptr); return res; } return {}; } //Перевантаження операції += void operator += (Matrix m) { if ((this->rows != m.getRows()) || (this->columns != m.getColumns())) { cout << "Impossible to add!" << endl; } else { for (int i = 0; i < m.getRows(); i++) { for (int j = 0; j < m.getColumns(); j++) { this->ptr[i][j] += m.getPtr()[i][j]; } } } } //Перевантаження операції -= void operator -= (Matrix m) { if ((this->rows != m.getRows()) || (this->columns != m.getColumns())) { cout << "Impossible to subtract!" << endl; } else { for (int i = 0; i < m.getRows(); i++) { for (int j = 0; j < m.getColumns(); j++) { this->ptr[i][j] -= m.getPtr()[i][j]; } } } } //Перевантаження операції *= void operator *= (Matrix m) { if (this->columns != m.getRows()) { cout << "Impossible to multiply!" << endl; } else { auto** ptr1 = new float* [this->rows]; for (int i = 0; i < this->rows; i++) { ptr1[i] = new float[m.getColumns()]; for (int j = 0; j < m.getColumns(); j++) { ptr1[i][j] = 0; } } for (int i = 0; i < this->rows; i++) { for (int j = 0; j < m.getColumns(); j++) { for (int h = 0; h < this->columns; h++) { ptr1[i][j] += this->ptr[i][h] * m.getPtr()[h][j]; } } } this->columns = m.getColumns(); this->ptr = ptr1; } } //Перевантаження операції << friend std::ostream& operator<< (std::ostream& out, const Matrix& matrix) { for (int i = 0; i < matrix.rows; i++) { for (int j = 0; j < matrix.columns; j++) { out << matrix.ptr[i][j] << "\t"; } out << "\n"; } return out; } //Перевантаження операції >> friend std::istream& operator>> (std::istream& in, Matrix& matrix) { cout << "Enter count of rows: "; in >> matrix.rows; cout << "Enter count of columns: "; in >> matrix.columns; for (int i = 0; i < matrix.rows; i++) { for (int j = 0; j < matrix.columns; j++) { cout << "Enter [" << i << "][" << j << "] element:"; in >> matrix.ptr[i][j]; } } return in; } //Деструктор ~Matrix() = default; };
Editor is loading...