Advertisement
Guest User

Untitled

a guest
May 19th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdexcept>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. class Matrix {
  9. public:
  10.     Matrix() {
  11.  
  12.     }
  13.  
  14.     Matrix(const int& num_rows, const int& num_cols) {
  15.         if (num_rows < 0 || num_cols < 0) {
  16.             throw out_of_range("");
  17.         }
  18.  
  19.         Reset(num_rows, num_cols);
  20.     }
  21.  
  22. void Reset(const int& num_rows, const int& num_cols) {
  23.     if (num_rows < 0 || num_cols < 0) {
  24.             throw out_of_range("");
  25.     }
  26.  
  27.     matrix.assign(num_rows, vector<int>(num_cols));
  28. }
  29. int GetNumRows() const {
  30.     return matrix.size();
  31. }
  32.  
  33. int GetNumColumns() const {
  34.     return matrix[0].size();
  35. }
  36.  
  37. int At(const int& row, const int& col) const {
  38.     if (row < 0 || col < 0 || row > GetNumRows() || col > GetNumColumns()) {
  39.         throw out_of_range("");
  40.     }
  41.  
  42.     return matrix[row][col];
  43. }
  44.  
  45. int& At(const int& row, const int& col) {
  46.     if (row < 0 || col < 0 || row > GetNumRows() || col > GetNumColumns()) {
  47.         throw out_of_range("");
  48.     }
  49.  
  50.     return matrix[row][col];
  51. }
  52.  
  53. private:
  54.     vector <vector<int> > matrix;
  55. };
  56.  
  57. istream& operator>>(istream& stream, Matrix& matrix) {
  58.     int rows, cols;
  59.  
  60.     stream >> rows >> cols;
  61.  
  62.     if (rows < 0 || cols < 0) {
  63.             throw out_of_range("");
  64.     }
  65.  
  66.     matrix.Reset(rows, cols);
  67.  
  68.     for (int r = 0; r < rows; ++r) {
  69.         for (int c = 0; c < cols; ++c) {
  70.             stream >> matrix.At(r, c);
  71.         }
  72.     }
  73.  
  74.     return stream;
  75. }
  76.  
  77. ostream& operator<<(ostream& stream, const Matrix& matrix) {
  78.     const int rows = matrix.GetNumRows(), cols = matrix.GetNumColumns();
  79.  
  80.     stream << rows << ' ' << cols << endl;
  81.  
  82.     for (int r = 0; r < rows; ++r) {
  83.         for (int c = 0; c < cols; ++c) {
  84.             stream << matrix.At(r, c) << ' ';
  85.         }
  86.         stream << endl;
  87.     }
  88.  
  89.     return stream;
  90. }
  91.  
  92. Matrix operator+(const Matrix& lhs, const Matrix& rhs) {
  93.     if (lhs.GetNumRows() != rhs.GetNumRows() || lhs.GetNumColumns() != rhs.GetNumColumns()) {
  94.         throw invalid_argument("");
  95.     }
  96.  
  97.     const int rows = lhs.GetNumRows(), cols = lhs.GetNumColumns();
  98.  
  99.     Matrix result(rows, cols);
  100.  
  101.     for (int r = 0; r < rows; ++r) {
  102.         for (int c = 0; c < cols; ++c) {
  103.             result.At(r, c) =  lhs.At(r, c) + rhs.At(r, c);
  104.         }
  105.     }
  106.  
  107.     return result;
  108. }
  109.  
  110. bool operator==(const Matrix& lhs, const Matrix rhs) {
  111.     if ((lhs.GetNumRows() == 0 || lhs.GetNumColumns() == 0) && (rhs.GetNumRows() == 0 || rhs.GetNumColumns() == 0)) {
  112.         return true;
  113.     } else if (lhs.GetNumRows() != rhs.GetNumRows() || lhs.GetNumColumns() != rhs.GetNumColumns()) {
  114.         return false;
  115.     } else {
  116.         const int rows = lhs.GetNumRows(), cols = lhs.GetNumColumns();
  117.  
  118.         for (int r = 0; r < rows; ++r) {
  119.             for (int c = 0; c < cols; ++c) {
  120.                 if (lhs.At(r, c) != rhs.At(r, c)) {
  121.                     return false;
  122.                 }
  123.             }
  124.         }
  125.     }
  126.  
  127.  
  128.     return true;
  129. }
  130.  
  131. int main() {
  132.   Matrix one;
  133.   Matrix two;
  134.  
  135.   cin >> one >> two;
  136.   cout << one + two << endl;
  137.   return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement