Advertisement
Guest User

matrix_sum

a guest
Jul 20th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdexcept>
  4. #include <vector>
  5. #include <time.h>
  6.  
  7. using namespace std;
  8.  
  9. // Реализуйте здесь
  10. // * класс Matrix
  11.  
  12. class Matrix
  13. {
  14. public:
  15.     Matrix() {
  16.         data = {};
  17.     }
  18.     Matrix(const int& num_rows, const int& num_cols) {
  19.         Reset(num_rows, num_cols);
  20.     }
  21.     void Reset(const int& num_rows, const int& num_cols) {
  22.         if(num_rows >= 0 && num_cols >=0)
  23.         {
  24.             if(num_rows == 0 || num_cols == 0)
  25.             {
  26.                 data = {};
  27.             }else
  28.             {
  29.                 data.assign(num_rows, vector<int>(num_cols));
  30.                 row_size = num_rows;
  31.                 col_size = num_cols;
  32.             }
  33.         }else
  34.         {
  35.             throw out_of_range("negative " + to_string(num_rows) + " " + to_string(num_cols));
  36.         }
  37.     }
  38.     const int& At(const int& row, const int& col) const {
  39.         if(row >=0 && col >= 0 && row < row_size && col < col_size)
  40.         {
  41.             return data[row][col];
  42.         }else
  43.         {
  44.             throw out_of_range("const At " + to_string(row) + " " + to_string(col));
  45.         }
  46.     }
  47.     int& At(const int& row, const int& col) {
  48.         if(row >=0 && col >= 0 && row < row_size && col < col_size)
  49.         {
  50.             return data[row][col];
  51.         }else
  52.         {
  53.             throw out_of_range("At " + to_string(row) + " " + to_string(col));
  54.         }
  55.     }
  56.     int GetNumRows() const {
  57.         return data.size();
  58.     }
  59.     int GetNumColumns() const {
  60.         if(!data.empty())
  61.         {
  62.             return data[0].size();
  63.         } else
  64.         {
  65.             return 0;
  66.         }
  67.     }
  68. private:
  69.     int row_size = 0;
  70.     int col_size = 0;
  71.     vector<vector<int>> data;
  72. };
  73.  
  74.  
  75. // * оператор ввода для класса Matrix из потока istream
  76. istream& operator>>(istream& stream, Matrix& mat) {
  77.     int R,C;
  78.     cin >> R >> C;
  79.     mat.Reset(R, C);
  80.     for(int i = 0; i < R; i++)
  81.     {
  82.         for(int j = 0; j < C; j++)
  83.         {
  84.             stream >> mat.At(i, j);
  85.         }
  86.     }
  87.     return stream;
  88. }
  89.  
  90. // * оператор вывода класса Matrix в поток ostream
  91. ostream& operator<<(ostream& stream,const Matrix& mat) {
  92.     int R = mat.GetNumRows();
  93.     int C = mat.GetNumColumns();
  94.     stream << R << " " << C;
  95.     if(R == 0)
  96.     {
  97.         return stream;
  98.     }
  99.     stream << endl;
  100.     for(int i = 0; i < R; i++)
  101.     {
  102.         for(int j = 0; j < C; j++)
  103.         {
  104.             stream << mat.At(i, j) << " ";
  105.         }
  106.         stream << endl;
  107.     }
  108.     return stream;
  109. }
  110.  
  111. // * оператор проверки на равенство двух объектов класса Matrix
  112. bool operator== (const Matrix& lhs, const Matrix& rhs) {
  113.     int Rl = lhs.GetNumRows();
  114.     int Cl = lhs.GetNumColumns();
  115.     int Rr = rhs.GetNumRows();
  116.     int Cr = rhs.GetNumColumns();
  117.     if(Rl == Rr && Cl == Cr)
  118.     {
  119.         for(int i = 0; i < Rr; i++)
  120.         {
  121.             for(int j = 0; j < Cr; j++)
  122.             {
  123.                 if(rhs.At(i, j) != lhs.At(i, j))
  124.                 {
  125.                     return false;
  126.                 }
  127.             }
  128.         }
  129.         return true;
  130.     }else
  131.     {
  132.         return false;
  133.     }
  134. }
  135.  
  136. // * оператор сложения двух объектов класса Matrix
  137. Matrix operator +(const Matrix& lhs, const Matrix& rhs) {
  138.     int Rl = lhs.GetNumRows();
  139.     int Cl = lhs.GetNumColumns();
  140.     int Rr = rhs.GetNumRows();
  141.     int Cr = rhs.GetNumColumns();
  142.     if(Rl == Rr && Cl == Cr)
  143.     {
  144.         Matrix res(Rr, Cr);
  145.         for(int i = 0; i < Rr; i++)
  146.         {
  147.             for(int j = 0; j < Cr; j++)
  148.             {
  149.                 res.At(i, j) = lhs.At(i, j) + rhs.At(i, j);
  150.             }
  151.         }
  152.         return res;
  153.  
  154.     } else
  155.     {
  156.         throw invalid_argument("");
  157.     }
  158. }
  159.  
  160. int main() {
  161.     Matrix one;
  162.     Matrix two;
  163.  
  164.     cin >> one >> two;
  165.     cout << one + two << endl;
  166.     return 0;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement