Skip to content

Commit

Permalink
Лазуткин М.К. 1
Browse files Browse the repository at this point in the history
  • Loading branch information
ADOBbIU authored Nov 11, 2024
1 parent 7bc293c commit 018fa93
Showing 1 changed file with 73 additions and 45 deletions.
118 changes: 73 additions & 45 deletions task1/matrix.cpp
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_; // Данные матрицы
};

0 comments on commit 018fa93

Please sign in to comment.