#include <iostream> #include <vector> #include <algorithm> #include <fstream> #include <string> #include <iomanip> #include <sstream> #include <set> #include <map> #include <exception> #include <string> #include <stdexcept> using namespace std; class Matrix { public: int* matrix; int n_col = 0; int n_row = 0; Matrix() { matrix = new int[0]; } Matrix(int num_rows, int num_cols) { if ((num_rows < 0) || (num_cols < 0)) { throw out_of_range(""); } matrix = new int[num_cols*num_rows]; n_col = num_cols; n_row = num_rows; for (int i = 0; i < num_rows; i++) { for (int j = 0; j < num_cols; j++) { *(matrix + i*num_cols + j) = 0; } } } void Reset(int num_rows, int num_cols) { if (num_rows < 0 || num_cols < 0) { throw out_of_range(""); } n_col = num_cols; n_row = num_rows; matrix = new int[num_cols*num_rows]; for (int i = 0; i < num_rows; i++) { for (int j = 0; j < num_cols; j++) { *(matrix + i*num_cols + j) = 0; } } } int At(int row, int col) const { if (row >= n_row || col >= n_col || row < 0 || col < 0) { throw out_of_range(""); } return *(matrix + row*n_col + col); } int& At(int row, int col) { if (row >= n_row || col >= n_col || row < 0 || col < 0) { throw out_of_range(""); } return *(matrix + row * n_col + col); } int GetNumRows() const { return n_row; } int GetNumColumns() const { return n_col; } ~Matrix() { delete[] matrix; } }; std::istream& operator >> (std::istream& in, Matrix& m) { in >> m.n_row >> m.n_col; int val; for (int i = 0; i < m.n_row; i++) { for (int j = 0; j < m.n_col; j++) { in >> val; *(m.matrix + i*m.n_col + j) = val; } } return in; } std::ostream& operator << (std::ostream &os, const Matrix &m) { os << m.n_row << ' ' << m.n_col << endl; for (int i = 0; i < m.n_row; i++) { for (int j = 0; j < m.n_col; j++) { os << *(m.matrix + i*m.n_col + j) << ' '; } os << endl; } return os; } bool operator==(const Matrix &m1, const Matrix &m2) { if (m1.n_col != m2.n_col || m1.n_row != m2.n_row) { return false; } for (int i = 0; i < m1.n_row; i++) { for (int j = 0; j < m1.n_col; j++) { if (*(m1.matrix + i*m1.n_col + j) != *(m2.matrix + i*m2.n_col + j)) { return false; } } } return true; } Matrix operator+(const Matrix &m1, const Matrix &m2) { if ((m1.n_col == 0 || m1.n_row == 0) && (m2.n_col == 0 || m2.n_row == 0)) { return Matrix(0, 0); } if (m1.n_col != m2.n_col || m1.n_row != m2.n_row) { throw invalid_argument(""); } Matrix m(m1.n_row, m1.n_col); for (int i = 0; i < m1.n_row; i++) { for (int j = 0; j < m1.n_col; j++) { m.At(i,j) = m1.At(i,j) + m2.At(i,j); } } return m; } int main() { Matrix one(3,5); Matrix two(3,5); cin >> one >> two; cout << one << endl; cout << two << endl; cout << one + two << endl; return 0; }