Advertisement
Bisus

classMatrix

Aug 27th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <memory>
  3. using namespace std;
  4.  
  5. template<typename T>
  6. class Matrix{
  7. public:
  8.     // Конструктор
  9.     Matrix(int m, int n) :elem{new T[m*n]}, numRows{m}, numColumns{n} { }
  10.  
  11.     // Доступ к элементам по индексам
  12.     T& operator()(int m, int n) { return elem[m*numColumns + n]; }
  13.  
  14.     // Присвоение
  15.     Matrix& operator=(const Matrix& other) {
  16.         if( (numRows==other.numRows)&&(numColumns==other.numColumns) )
  17.             for(int i = 0; i!=numRows; ++i)
  18.                 for(int j = 0; j!=numColumns; ++j)
  19.                     elem[i*numColumns + j] = other.elem[i*numColumns + j];
  20.  
  21.         return *this;
  22.     }
  23.  
  24.     // Сложение
  25.     Matrix& operator+(const Matrix& other) {
  26.         if( (numRows==other.numRows)&&(numColumns==other.numColumns) )
  27.             for(int i = 0; i!=numRows; ++i)
  28.                 for(int j = 0; j!=numColumns; ++j)
  29.                     elem[i*numColumns + j] += other.elem[i*numColumns + j];
  30.  
  31.         return *this;
  32.     }
  33.  
  34.     void scan() {
  35.         for(int i = 0; i!=numRows; ++i)
  36.             for(int j = 0; j!=numColumns; ++j) {
  37.                 cout  << "(" << i << ", " << j << ")=";
  38.                 cin >> elem[i*numColumns + j];
  39.             }
  40.     }
  41.  
  42.     void print() {
  43.         for(int i = 0; i!=numRows; ++i) {
  44.             for(int j = 0; j!=numColumns; ++j)
  45.                 cout << elem[i*numColumns + j] << '\t';
  46.             cout << endl;
  47.         }
  48.     }
  49.  
  50. private:
  51.     unique_ptr<T[]> elem;   // "Умный" указатель на элементы
  52.     int numRows;        // Число строк матрицы
  53.     int numColumns;     // Число столбцов матрицы
  54. };
  55.  
  56. int main() {
  57.     int numRows, numColumns;
  58.  
  59.     cout << "iMatr:" << endl << "Число строк матрицы:\t";
  60.     cin >> numRows;
  61.     cout << "Число столбцов матрицы:\t";
  62.     cin >> numColumns;
  63.     Matrix<int> iMatr(numRows, numColumns);
  64.     iMatr.scan();
  65.     iMatr.print();
  66.  
  67.     cout << "iMatr2:" << endl << "Число строк матрицы:\t";
  68.     cin >> numRows;
  69.     cout << "Число столбцов матрицы:\t";
  70.     cin >> numColumns;
  71.     Matrix<int> iMatr2(numRows, numColumns);
  72.     iMatr2.scan();
  73.     iMatr2.print();
  74.  
  75.     cout << "iMatr3:" << endl << "Число строк матрицы:\t";
  76.     cin >> numRows;
  77.     cout << "Число столбцов матрицы:\t";
  78.     cin >> numColumns;
  79.     Matrix<int> iMatr3(numRows, numColumns);
  80.  
  81.     iMatr3 = iMatr + iMatr2;
  82.  
  83.     cout << "iMatr3:" << endl;
  84.     iMatr3.print();
  85.  
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement