Advertisement
coloriot

HA22_1

Sep 5th, 2024
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. // Задача матрица
  6.  
  7. class Matrix {
  8. private:
  9.     int** matrix;
  10.     int rows;
  11.     int cols;
  12.  
  13. public:
  14.     Matrix(int new_rows, int new_cols) {
  15.         this->rows = new_rows;
  16.         this->cols = new_cols;
  17.         this->matrix = new int*[new_rows];
  18.         for (int i = 0; i < this->rows; ++i) {
  19.             matrix[i] = new int[new_cols];
  20.         }
  21.         for (int i = 0; i < this->rows; ++i) {
  22.             for (int j = 0; j < this->cols; ++j) {
  23.                 matrix[i][j] = 0;
  24.             }
  25.         }
  26.     }
  27.  
  28.     // Конструктор копирования
  29.     Matrix(const Matrix& other) {
  30.         rows = other.rows;
  31.         cols = other.cols;
  32.         matrix = new int*[rows];
  33.         for (int i = 0; i < rows; ++i) {
  34.             matrix[i] = new int[cols];
  35.             for (int j = 0; j < cols; ++j) {
  36.                 matrix[i][j] = other.matrix[i][j];
  37.             }
  38.         }
  39.     }
  40.    
  41.     ~Matrix() {
  42.         for (int i = 0; i < this->rows; ++i) {
  43.             delete[] this->matrix[i];
  44.         }
  45.         delete[] this->matrix;
  46.     }
  47.    
  48.     Matrix operator+ (const Matrix& other) {
  49.         if (this->rows == other.rows && this->cols == other.cols) {
  50.             Matrix new_matrix(this->rows, this->cols);
  51.             for (int i = 0; i < this->rows; ++i) {
  52.                 for (int j = 0; j < this->cols; ++j) {
  53.                     new_matrix.matrix[i][j] = this->matrix[i][j] + other.matrix[i][j];
  54.                 }
  55.             }
  56.             return new_matrix;
  57.         } else {
  58.             cout << "Разные размерности!" << endl;
  59.             return Matrix(0, 0);
  60.         }
  61.     }
  62.  
  63.     // Оператор умножения
  64.     Matrix operator* (const Matrix& other) {
  65.         if (this->cols != other.rows) {
  66.             cout << "Разные размерности!" << endl;
  67.             return Matrix(0, 0);
  68.         }
  69.         Matrix result(this->rows, other.cols);
  70.         for (int i = 0; i < this->rows; ++i) {
  71.             for (int j = 0; j < other.cols; ++j) {
  72.                 result.matrix[i][j] = 0; // Initialize result element to 0
  73.                 for (int k = 0; k < this->cols; ++k) {
  74.                     result.matrix[i][j] += this->matrix[i][k] * other.matrix[k][j];
  75.                 }
  76.             }
  77.         }
  78.         return result;
  79.     }
  80.    
  81.     void print() const {
  82.         for (int i = 0; i < this->rows; ++i) {
  83.             for (int j = 0; j < this->cols; ++j) {
  84.                 cout << matrix[i][j] << ' ';
  85.             }
  86.             cout << '\n';
  87.         }
  88.     }
  89. };
  90.  
  91. // Код для проверки
  92. int main() {
  93.     Matrix mat1(2, 2);
  94.     Matrix mat2(2, 2);
  95.     Matrix sum = mat1 + mat2;
  96.     sum.print();
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement