Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <stdexcept>
- #include <vector>
- #include <time.h>
- using namespace std;
- // Реализуйте здесь
- // * класс Matrix
- class Matrix
- {
- public:
- Matrix() {
- data = {};
- }
- Matrix(const int& num_rows, const int& num_cols) {
- Reset(num_rows, num_cols);
- }
- void Reset(const int& num_rows, const int& num_cols) {
- if(num_rows >= 0 && num_cols >=0)
- {
- if(num_rows == 0 || num_cols == 0)
- {
- data = {};
- }else
- {
- data.assign(num_rows, vector<int>(num_cols));
- row_size = num_rows;
- col_size = num_cols;
- }
- }else
- {
- throw out_of_range("negative " + to_string(num_rows) + " " + to_string(num_cols));
- }
- }
- const int& At(const int& row, const int& col) const {
- if(row >=0 && col >= 0 && row < row_size && col < col_size)
- {
- return data[row][col];
- }else
- {
- throw out_of_range("const At " + to_string(row) + " " + to_string(col));
- }
- }
- int& At(const int& row, const int& col) {
- if(row >=0 && col >= 0 && row < row_size && col < col_size)
- {
- return data[row][col];
- }else
- {
- throw out_of_range("At " + to_string(row) + " " + to_string(col));
- }
- }
- int GetNumRows() const {
- return data.size();
- }
- int GetNumColumns() const {
- if(!data.empty())
- {
- return data[0].size();
- } else
- {
- return 0;
- }
- }
- private:
- int row_size = 0;
- int col_size = 0;
- vector<vector<int>> data;
- };
- // * оператор ввода для класса Matrix из потока istream
- istream& operator>>(istream& stream, Matrix& mat) {
- int R,C;
- cin >> R >> C;
- mat.Reset(R, C);
- for(int i = 0; i < R; i++)
- {
- for(int j = 0; j < C; j++)
- {
- stream >> mat.At(i, j);
- }
- }
- return stream;
- }
- // * оператор вывода класса Matrix в поток ostream
- ostream& operator<<(ostream& stream,const Matrix& mat) {
- int R = mat.GetNumRows();
- int C = mat.GetNumColumns();
- stream << R << " " << C;
- if(R == 0)
- {
- return stream;
- }
- stream << endl;
- for(int i = 0; i < R; i++)
- {
- for(int j = 0; j < C; j++)
- {
- stream << mat.At(i, j) << " ";
- }
- stream << endl;
- }
- return stream;
- }
- // * оператор проверки на равенство двух объектов класса Matrix
- bool operator== (const Matrix& lhs, const Matrix& rhs) {
- int Rl = lhs.GetNumRows();
- int Cl = lhs.GetNumColumns();
- int Rr = rhs.GetNumRows();
- int Cr = rhs.GetNumColumns();
- if(Rl == Rr && Cl == Cr)
- {
- for(int i = 0; i < Rr; i++)
- {
- for(int j = 0; j < Cr; j++)
- {
- if(rhs.At(i, j) != lhs.At(i, j))
- {
- return false;
- }
- }
- }
- return true;
- }else
- {
- return false;
- }
- }
- // * оператор сложения двух объектов класса Matrix
- Matrix operator +(const Matrix& lhs, const Matrix& rhs) {
- int Rl = lhs.GetNumRows();
- int Cl = lhs.GetNumColumns();
- int Rr = rhs.GetNumRows();
- int Cr = rhs.GetNumColumns();
- if(Rl == Rr && Cl == Cr)
- {
- Matrix res(Rr, Cr);
- for(int i = 0; i < Rr; i++)
- {
- for(int j = 0; j < Cr; j++)
- {
- res.At(i, j) = lhs.At(i, j) + rhs.At(i, j);
- }
- }
- return res;
- } else
- {
- throw invalid_argument("");
- }
- }
- int main() {
- Matrix one;
- Matrix two;
- cin >> one >> two;
- cout << one + two << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement