-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
73 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,76 @@ | ||
#include "matrix.hpp" | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <stdexcept> | ||
|
||
Matrix::Matrix(int numRows, int numCols) | ||
{ | ||
// your implementation here | ||
} | ||
|
||
void Matrix::Reset(int numRows, int numCols) | ||
{ | ||
// your implementation here | ||
} | ||
|
||
int& Matrix::At(int row, int col) | ||
{ | ||
// your implementation here | ||
} | ||
|
||
const int& Matrix::At(int row, int col) const | ||
{ | ||
// your implementation here | ||
} | ||
|
||
int Matrix::GetRows() const | ||
{ | ||
// your implementation here | ||
} | ||
|
||
int Matrix::GetCols() const | ||
{ | ||
// your implementation here | ||
} | ||
|
||
bool Matrix::operator==(const Matrix& m2) | ||
{ | ||
// your implementation here | ||
} | ||
|
||
bool Matrix::operator!=(const Matrix& m2) | ||
{ | ||
// your implementation here | ||
} | ||
|
||
Matrix Matrix::operator+(const Matrix& m2) | ||
{ | ||
// your implementation here | ||
} | ||
class Matrix { | ||
public: | ||
Matrix() = default; | ||
|
||
Matrix(int numRows, int numCols) { | ||
Reset(numRows, numCols); | ||
} | ||
|
||
void Reset(int numRows, int numCols) { | ||
if (numRows <= 0 || numCols <= 0) { | ||
rows_ = 0; | ||
cols_ = 0; | ||
data_.clear(); | ||
} else { | ||
rows_ = numRows; | ||
cols_ = numCols; | ||
data_.resize(rows_, std::vector<int>(cols_, 0)); | ||
} | ||
} | ||
|
||
int& At(int row, int col) { | ||
if (row < 0 || row >= rows_ || col < 0 || col >= cols_) { | ||
throw std::out_of_range("Index out of range"); | ||
} | ||
return data_[row][col]; | ||
} | ||
|
||
const int& At(int row, int col) const { | ||
if (row < 0 || row >= rows_ || col < 0 || col >= cols_) { | ||
throw std::out_of_range("Index out of range"); | ||
} | ||
return data_[row][col]; | ||
} | ||
|
||
int GetRows() const { | ||
return rows_; | ||
} | ||
|
||
int GetCols() const { | ||
return cols_; | ||
} | ||
|
||
bool operator==(const Matrix& m2) { | ||
if (rows_ != m2.rows_ || cols_ != m2.cols_) { | ||
return false; | ||
} | ||
return data_ == m2.data_; | ||
} | ||
|
||
bool operator!=(const Matrix& m2) { | ||
return !(*this == m2); | ||
} | ||
|
||
Matrix operator+(const Matrix& m2) { | ||
if (rows_ != m2.rows_ || cols_ != m2.cols_) { | ||
throw std::invalid_argument("Matrices must be of the same size for addition"); | ||
} | ||
Matrix result(rows_, cols_); | ||
for (int i = 0; i < rows_; ++i) { | ||
for (int j = 0; j < cols_; ++j) { | ||
result.At(i, j) = At(i, j) + m2.At(i, j); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
private: | ||
int rows_ = 0; // Количество строк | ||
int cols_ = 0; // Количество столбцов | ||
std::vector<std::vector<int>> data_; // Данные матрицы | ||
}; |