Advertisement
daniil_mironoff

Lab 3 for Best Nastya with (black) <3

Apr 27th, 2019
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. //#include "stdafx.h"
  4.  
  5. using namespace std;
  6.  
  7. // Класс Квадратная Матрица
  8. class Matrix_Square {
  9.     protected:
  10.         int size = 1;
  11.         int ** matrix;
  12.  
  13.     public:
  14.         // Метод. Вывод Матрицы
  15.         void printValues() {
  16.             cout << "Матрица:" << endl;
  17.             for (int i = 0; size > i; i++) {
  18.                 for (int j = 0; size > j; j++) {
  19.                     cout << matrix[i][j] << " ";
  20.                 }
  21.                
  22.                 cout << endl;
  23.             }
  24.            
  25.             cout << endl;
  26.         }
  27.    
  28.         //  Метод. Решение Задачи
  29.         void algorithm() {
  30.             int max = matrix[0][0];
  31.             int max_x = 0;
  32.             int max_y = 0;
  33.            
  34.             // Поиск максимального элемента
  35.             for (int y = 0; size > y; y++) {
  36.                 for (int x = 0; size > x; x++) {
  37.                    
  38.                     // Если нашел больше предыдущего максимального
  39.                     if (matrix[y][x] > max) {
  40.                         max = matrix[y][x];
  41.                         max_y = y;
  42.                         max_x = x;
  43.                     }
  44.                 }
  45.             }
  46.            
  47.             if (max_y < max_x) {
  48.                 // Если макс. элемент выше диагонали то
  49.                 // то отзеркаливаем
  50.                
  51.                 for (int y = 0; size > y; y++) {
  52.                     for (int x = 0; size > x; x++) {
  53.                        
  54.                         // Доходя до диагонали - переходим к другой строке
  55.                         if (x == y) {
  56.                             break;
  57.                         }
  58.                        
  59.                         // Обмен значениями с помощью переменной-буфер
  60.                         int buff = matrix[y][x];
  61.                         matrix[y][x] = matrix[x][y];
  62.                         matrix[x][y] = buff;
  63.                     }
  64.                 }
  65.                
  66.                 cout << "Транспортированая матрица:" << endl;
  67.                 printValues();
  68.                
  69.             } else {
  70.                 // Если макс. элемент ниже или на диагонали то
  71.                 // находим сумму элементов строки и столбца
  72.                
  73.                 int sum = -max; // Вычитаем сразу макс элемент, т.к.
  74.                                 // будет входить в сумму два раза
  75.                
  76.                 // Находим сумму
  77.                 for (int y = 0; size > y; y++) {
  78.                     sum += matrix[y][max_x];
  79.                 }
  80.                
  81.                 for (int x = 0; size > x; x++) {
  82.                     sum += matrix[max_y][x];
  83.                 }
  84.                
  85.                 cout << "Сумма элементов строки и столбца, на которых находится макс. элемент: " << sum << endl;
  86.             }
  87.         }
  88.    
  89. };
  90.  
  91. // Класс Вводимая [Квадратная Матрица]
  92. class MatrixInput : public Matrix_Square {
  93.     public:
  94.    
  95.         // Конструктор
  96.         MatrixInput(int size_pr = 5) {
  97.             size = size_pr;
  98.             matrix = new int * [size];
  99.            
  100.             for (int i = 0; size > i; i++) {
  101.                 matrix[i] = new int [size];
  102.                
  103.                 for (int j = 0; size > j; j++) {
  104.                     cout << "Введите значение для " << i + 1 << " строки " << j + 1 << " столбца: ";
  105.                     cin >> matrix[i][j];
  106.                 }
  107.             }
  108.            
  109.             cout << endl;
  110.         }
  111. };
  112.  
  113. // Класс Случайная [Квадратная Матрица]
  114. class MatrixRandom : public Matrix_Square {
  115.     public:
  116.    
  117.         // Конструктор
  118.         MatrixRandom(int size_pr = 5) {
  119.             size = size_pr;
  120.             matrix = new int * [size];
  121.  
  122.             for (int i = 0; size > i; i++) {
  123.                 matrix[i] = new int [size];
  124.  
  125.                 for (int j = 0; size > j; j++) {
  126.                     matrix[i][j] = rand() % 10;
  127.                 }
  128.             }
  129.            
  130.             cout << endl;
  131.         }
  132. };
  133.  
  134. int main() {
  135.     srand(time(NULL));
  136.     setlocale(LC_ALL, "Russian");
  137.    
  138.     MatrixRandom testA(5);
  139.     testA.printValues();
  140.     testA.algorithm();
  141.    
  142.     MatrixInput testB(3);
  143.     testB.printValues();
  144.     testB.algorithm();
  145.  
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement