Advertisement
Gistrec

Политкорректная матрица

Dec 8th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 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.     int _num_rows;
  10.     int _num_cols;
  11.  
  12.     std::vector<std::vector<int>> _matrix;
  13.  
  14. public:
  15.     Matrix(): _num_rows(0), _num_cols(0) {}
  16.  
  17.     Matrix(int num_rows, int num_cols) {
  18.         Reset(num_rows, num_cols);
  19.     }
  20.  
  21.     void Reset(int num_rows, int num_cols) {
  22.         if (num_rows < 0 || num_cols < 0) throw std::out_of_range("out_of_range");
  23.         _num_rows = num_rows;
  24.         _num_cols = num_cols;
  25.         _matrix.resize(num_rows);
  26.         for (auto &column : _matrix) {
  27.             column.resize(num_cols);
  28.             for (auto &elem : column) {
  29.                 elem = 0;
  30.             }
  31.         }
  32.     }
  33.  
  34.     int At(int num_rows, int num_cols) const {
  35.         if (num_rows < 0 || num_cols < 0 || num_rows >= _num_rows || num_cols >= _num_cols) {
  36.             throw std::out_of_range("");
  37.         }
  38.         return _matrix[num_rows][num_cols];
  39.     }
  40.  
  41.     int& At(int num_rows, int num_cols) {
  42.         if (num_rows < 0 || num_cols < 0 || num_rows >= _num_rows || num_cols >= _num_cols) {
  43.             throw std::out_of_range("");
  44.         }
  45.         return _matrix[num_rows][num_cols];
  46.     }
  47.  
  48.     int GetNumRows() const {
  49.         return _num_rows;
  50.     }
  51.  
  52.     int GetNumColumns() const {
  53.         return _num_cols;
  54.     }
  55.  
  56.  
  57.     friend std::istream& operator >> (std::istream &file, Matrix &matrix) {
  58.         int num_rows, num_cols;
  59.         file >> num_rows >> num_cols;
  60.  
  61.         matrix.Reset(num_rows, num_cols);
  62.  
  63.         for (int row = 0; row < num_rows; row++) {
  64.             for (int col = 0; col < num_cols; col++) {
  65.                 int value;
  66.                 file >> value;
  67.                 matrix.At(row, col) = value;
  68.             }
  69.         }
  70.  
  71.         return file;
  72.     }
  73.  
  74.  
  75. };
  76.  
  77. bool operator == (Matrix const &first, Matrix const &second) {
  78.     if (first.GetNumRows() != second.GetNumRows()) return false;
  79.     if (first.GetNumColumns() != second.GetNumColumns()) return false;
  80.  
  81.     for (int row = 0; row < first.GetNumRows(); row++) {
  82.         for (int col = 0; col < first.GetNumColumns(); col++) {
  83.             if (first.At(row, col) != second.At(row, col)) return false;
  84.         }
  85.     }
  86.     return true;
  87.  
  88. }
  89.  
  90.  
  91. Matrix operator + (Matrix const &first, Matrix const &second) {
  92.     if (first.GetNumRows() != second.GetNumRows()) throw std::invalid_argument("");
  93.     if (first.GetNumColumns() != second.GetNumColumns()) throw std::invalid_argument("");
  94.  
  95.     Matrix result(first.GetNumRows(), first.GetNumColumns());
  96.     for (int row = 0; row < first.GetNumRows(); row++) {
  97.         for (int col = 0; col < first.GetNumColumns(); col++) {
  98.             result.At(row, col) = first.At(row, col) + second.At(row, col);
  99.         }
  100.     }
  101.     return result;
  102. }
  103.  
  104. std::ostream& operator << (std::ostream &file, Matrix const &matrix) {
  105.     file << matrix.GetNumRows() << " " << matrix.GetNumColumns() << std::endl;
  106.  
  107.     for (int row = 0; row < matrix.GetNumRows(); row++) {
  108.         for (int col = 0; col < matrix.GetNumColumns(); col++) {
  109.             file << matrix.At(row, col);
  110.             if ((col - 1) != matrix.GetNumColumns()) file << " ";
  111.         }
  112.         file << std::endl;
  113.     }
  114.     return file;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement