Advertisement
niks32

NikitaLearn4.1.1

Nov 22nd, 2022
617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Matrix
  6. {
  7. public:
  8.     Matrix(int countRow, int countCol)
  9.     {
  10.         this->countCol = countCol;
  11.         this->countRow = countRow;
  12.  
  13.         arr = new double* [countRow];
  14.         for (int i = 0; i < countRow; i++)
  15.         {
  16.             arr[i] = new double[countCol];
  17.             //сразу обнуляем значения;
  18.             for (int j = 0; j < countCol; j++)
  19.             {
  20.                 arr[i][j] = 0;
  21.             }
  22.         }      
  23.     }
  24.  
  25.     double getElement(int rowIndex, int colIndex)
  26.     {
  27.         return arr[rowIndex][colIndex];
  28.     }
  29.  
  30.     void setElement(int rowIndex, int colIndex, double value)
  31.     {
  32.         arr[rowIndex][colIndex] = value;
  33.     }
  34.  
  35.     void printArr()
  36.     {
  37.         for (int i = 0; i < countRow; i++)
  38.         {
  39.             for (int j = 0; j < countCol; j++)
  40.             {
  41.                 cout << arr[i][j] << "\t";
  42.             }
  43.             cout << endl;
  44.         }
  45.     }
  46.    
  47.     void addition(Matrix &arr2, Matrix &result) //сложние двух матриц (нету проверки на размерность)
  48.     {
  49.         double tmp;
  50.         for (int i = 0; i < countRow; i++)
  51.         {
  52.             for (int j = 0; j < countCol; j++)
  53.             {
  54.                 tmp = this->arr[i][j] + (arr2.getElement(i, j));
  55.                 result.setElement(i, j, tmp);
  56.             }
  57.         }
  58.     }
  59.  
  60.     void multiplication(double num, Matrix& result)
  61.     {
  62.         double tmp;
  63.         for (int i = 0; i < countRow; i++)
  64.         {
  65.             for (int j = 0; j < countCol; j++)
  66.             {
  67.                 tmp = this->arr[i][j] * num;
  68.                 result.setElement(i, j, tmp);
  69.             }
  70.         }
  71.     }
  72.  
  73.     ~Matrix()
  74.     {
  75.         for (int i = 0; i < countCol; i++)
  76.         {
  77.             delete[] this->arr[i];
  78.         }
  79.         cout << "Сработал деструктор" << endl;
  80.     }
  81.        
  82. private:
  83.     double** arr;
  84.     int countRow;
  85.     int countCol;
  86. };
  87.  
  88. void setTestData1(Matrix& my_matrix1)
  89. {
  90.     int counter = 0;
  91.     for (int i = 0; i < 3; i++)
  92.     {
  93.         for (int j = 0; j < 3; j++)
  94.         {
  95.             my_matrix1.setElement(i, j, counter);
  96.             counter++;
  97.         }
  98.     }
  99. }
  100.  
  101. void setTestData2(Matrix& my_matrix1)
  102. {
  103.     int counter = 99;
  104.     for (int i = 0; i < 3; i++)
  105.     {
  106.         for (int j = 0; j < 3; j++)
  107.         {
  108.             my_matrix1.setElement(i, j, counter);
  109.             counter--;
  110.         }
  111.     }
  112. }
  113.  
  114. int main()
  115. {
  116.     setlocale(LC_ALL, "Ru");
  117.  
  118.     Matrix my_matrix1 = Matrix(3, 3);
  119.     Matrix my_matrix2 = Matrix(3, 3);
  120.     Matrix result = Matrix(3, 3);
  121.  
  122.     //delete[] my_matrix2;
  123.  
  124.     cout << "setTestData1(my_matrix1)-----------------------" << endl;
  125.     setTestData1(my_matrix1);
  126.     my_matrix1.printArr();
  127.  
  128.     /*cout << "setTestData1(my_matrix2)-----------------------" << endl;
  129.     setTestData2(my_matrix2);
  130.     my_matrix2.printArr();
  131.  
  132.     my_matrix1.addition(my_matrix2, result);
  133.     cout << "setTestData1(my_matrix2)-----------------------" << endl;
  134.     result.printArr();*/
  135.  
  136.     my_matrix1.multiplication(2, result);
  137.     cout << "result.printArr()-----------------------" << endl;
  138.     result.printArr();  
  139. }
  140.  
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement