Nelogeek

Untitled

Oct 24th, 2021
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 19.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <locale.h>
  5. #include <fstream>
  6. #include <string>
  7.  
  8. using namespace std;
  9.  
  10. int NumberElementN(int N)
  11. {
  12.     cout << "Количество cтрок (не более 10): ";
  13.     cin >> N;
  14.     cout << endl;
  15.     if (N <= 0 || N > 10)
  16.     {
  17.         cout << "Количество cтрок не должно быть меньше 1 или больше 10" << endl;
  18.         return -1;
  19.     }
  20.     else
  21.         return N;
  22. }
  23. int NumberElementM(int M)
  24. {
  25.     cout << "Количество столбцов (не более 10): ";
  26.     cin >> M;
  27.     cout << endl;
  28.     if (M <= 0 || M > 10)
  29.     {
  30.         cout << "Количество столбцов не должно быть меньше 1 или больше 10" << endl;
  31.         return -1;
  32.     }
  33.     else
  34.         return M;
  35. }
  36. void GetArray(int N, int M, int** mas, int a, int b)
  37. {
  38.     for (int i = 0; i < N; i++)
  39.         for (int j = 0; j < M; j++)
  40.             mas[i][j] = a + rand() % (b - a + 1);
  41. }
  42. void PutTwoDismensArray(int N, int M, int** mas)
  43. {
  44.     for (int i = 0; i < N; i++)
  45.     {
  46.         for (int j = 0; j < M; j++)
  47.         {
  48.             if (mas[i][j] < -9)
  49.                 cout << mas[i][j] << " ";
  50.             if (mas[i][j] >= -9 && mas[i][j] < 0)
  51.                 cout << " " << mas[i][j] << " ";
  52.             if (mas[i][j] > 0 && mas[i][j] < 9)
  53.                 cout << "  " << mas[i][j] << " ";
  54.             if (mas[i][j] > 9)
  55.                 cout << " " << mas[i][j] << " ";
  56.         }
  57.         cout << endl;
  58.     }
  59. }
  60. void InOneDimensional(int** mas, int* mas1, int n, int m)
  61. {
  62.     int k = 0;
  63.     for (int i = 0; i < n; i++)
  64.         for (int j = 0; j < m; j++)
  65.         {
  66.             mas1[k] = mas[i][j];
  67.             k++;
  68.         }
  69. }
  70. void InlDismMinElements(int** mas, int* mas1, int N, int M, int row, int column, char value)
  71. {
  72.     switch (value)
  73.     {
  74.     case 1:
  75.         for (size_t i = 0; i < M; i++)
  76.             mas1[i] = mas[row - 1][i];
  77.         break;
  78.     case 2:
  79.         for (size_t i = 0; i < N; i++)
  80.             mas1[i] = mas[i][column - 1];
  81.         break;
  82.     case 3:
  83.         for (int i = 0; i < N; i++)
  84.             for (int j = 0; j < M; j++)
  85.                 if (i == j)
  86.                     mas1[i] = mas[i][j];
  87.         break;
  88.     case 4:
  89.         for (int i = 0; i < N; i++)
  90.             for (int j = 0; j < M; j++)
  91.             {
  92.                 if (M >= N)
  93.                     if (i + j == N - 1)
  94.                         mas1[i] = mas[i][j];
  95.                     else {}
  96.                 else
  97.                     if (i + j == M - 1)
  98.                         mas1[i] = mas[i][j];
  99.             }
  100.         break;
  101.     }
  102. }
  103. void InTwoDimensional(int** mas, int* mas1, int n, int m)
  104. {
  105.     int k = 0;
  106.     for (int i = 0; i < n; i++)
  107.         for (int j = 0; j < m; j++)
  108.         {
  109.             mas[i][j] = mas1[k];
  110.             k++;
  111.         }
  112. }
  113. void PutOneDismensArray(int mas[], const int n, const int m)
  114. {
  115.     cout << endl;
  116.     for (int i = 0; i < (n * m); i++)
  117.         cout << mas[i] << " ";
  118.     cout << endl;
  119. }
  120. void SumElements(int** mas, int N, int M, int row, int column, int SumRow, int SumColumn, long int MultyRow, long int MultyColumn)
  121. {
  122.     MultyRow = 1; MultyColumn = 1;
  123.     SumRow = 0; SumColumn = 0;
  124.     for (int i = 0; i < M; i++)
  125.     {
  126.         SumRow += mas[row - 1][i];
  127.         MultyRow *= mas[row - 1][i];
  128.     }
  129.     for (int i = 0; i < N; i++)
  130.     {
  131.         SumColumn += mas[i][column - 1];
  132.         MultyColumn *= mas[i][column - 1];
  133.     }
  134.     cout << "Сумма строки " << row << " равна " << SumRow << " -------- Произведение строки равна => " << MultyRow << endl;
  135.     cout << "Сумма столбца " << column << " равна " << SumColumn << " -------- Произведение столбца => " << MultyColumn << endl;
  136. }
  137. void SumElementsDiagonal(int** mas, int N, int M, int SumMainDiagonal, int SumSecondaryDiagonal, long int MultyMain, long int MultySecond)
  138. {
  139.     MultyMain = 1; MultySecond = 1;
  140.     SumMainDiagonal = 0; SumSecondaryDiagonal = 0;
  141.     for (int i = 0; i < N; i++)
  142.         for (int j = 0; j < M; j++)
  143.         {
  144.             if (i == j)
  145.             {
  146.                 SumMainDiagonal += mas[i][j];
  147.                 MultyMain *= mas[i][j];
  148.             }
  149.             if (M >= N)
  150.                 if (i + j == N - 1)
  151.                 {
  152.                     SumSecondaryDiagonal += mas[i][j];
  153.                     MultySecond *= mas[i][j];
  154.                 }
  155.                 else {}
  156.             else
  157.                 if (i + j == M - 1)
  158.                 {
  159.                     SumSecondaryDiagonal += mas[i][j];
  160.                     MultySecond *= mas[i][j];
  161.                 }
  162.         }
  163.     cout << "Сумма главной диагонали равна " << SumMainDiagonal << " ---- Произведение главной => " << MultyMain << endl;
  164.     cout << "Сумма побочной строки равна " << SumSecondaryDiagonal << " ----- Произведение побочной => " << MultySecond << endl;
  165. }
  166. void MaxElement(int mas[], int n)
  167. {
  168.     int indexMax = 0;
  169.     int max = mas[0];
  170.     for (int i = 1; i < n; i++)
  171.         if (mas[i] > max)
  172.         {
  173.             max = mas[i];
  174.             indexMax = i;
  175.         }
  176.     cout << "Максимальный элемент " << max << endl;
  177. }
  178. void MaxAndMinElementDiagonal(int mas[], int m, int n)
  179. {
  180.     int indexMax = 0;
  181.     int max = mas[0];
  182.     int indexMin = 0;
  183.     int min = mas[0];
  184.     if (m >= n)
  185.         for (int i = 1; i < n; i++)
  186.             if (mas[i] > max)
  187.             {
  188.                 max = mas[i];
  189.                 indexMax = i;
  190.             }
  191.             else {}
  192.     else {
  193.         for (int i = 1; i < m; i++)
  194.             if (mas[i] < min)
  195.             {
  196.                 min = mas[i];
  197.                 indexMin = i;
  198.             }
  199.             else {}
  200.     }
  201.     cout << "Максимальный элемент " << max << endl;
  202.     cout << "Минимальный элемент " << min << endl;
  203. }
  204. void MinElement(int mas[], int n)
  205. {
  206.     int indexMin = 0;
  207.     int min = mas[0];
  208.     for (int i = 1; i < n; i++)
  209.         if (mas[i] < min)
  210.             min = mas[i];
  211.     cout << "Минимальный элемент " << min << endl;
  212. }
  213. void bubbleSort(int mas[], int n)
  214. {
  215.     int tmp = 0;
  216.     for (int i = 0; i < n; i++)
  217.     {
  218.         for (int j = (n - 1); j >= (i + 1); j--)
  219.         {
  220.             if (mas[j] < mas[j - 1])
  221.             {
  222.                 tmp = mas[j];
  223.                 mas[j] = mas[j - 1];
  224.                 mas[j - 1] = tmp;
  225.             }
  226.         }
  227.     }
  228. }
  229. void searchFunct(int** mas, int n, int m)
  230. {
  231.     int key, verify = 0, number; char value;
  232.     cout << "Введите элемент для поиска: ";
  233.     cin >> key;
  234.     cout << "Выберите условие поиска\n> Весь массив - введите число '1'\n> Относительно строки - '2'\n";
  235.     cout << "> Относительно столбца - '3'\n> Относительно главной диагонали - '4'\n> Относительно побочной диагонали - '5'\n";
  236.     cout << "> Относительно верхнего треугольника - '6'\n> Относительно нижнего треугольника - '7'\n";
  237.     cin >> value;
  238.     switch (value)
  239.     {
  240.     case '1':
  241.         for (int i = 0; i < m; i++)
  242.             for (int j = 0; j < n; j++)
  243.                 if (mas[i][j] == key)
  244.                 {
  245.                     cout << "Элемент был найден в " << i + 1 << " строке, " << j + 1 << " столбце" << endl;
  246.                     verify++;
  247.                 }
  248.         break;
  249.     case '2':
  250.         cout << "Введите номер строки: "; cin >> number;
  251.         if (number < 1 || number > m)
  252.         {
  253.             cout << "Этой строки нет";
  254.             break;
  255.         }
  256.         for (int i = 0; i < m; i++)
  257.             if (mas[number - 1][i] == key)
  258.             {
  259.                 cout << "Элемент был найден в " << i + 1 << " столбце" << endl;
  260.                 verify++;
  261.             }
  262.         break;
  263.     case '3':
  264.         cout << "Введите номер столбца: "; cin >> number;
  265.         if (number < 1 || number > n)
  266.         {
  267.             cout << "Этого столбца нет";
  268.             break;
  269.         }
  270.         for (int i = 0; i < n; i++)
  271.             if (mas[i][number - 1] == key)
  272.             {
  273.                 cout << "Элемент был найден в " << i + 1 << " строке" << endl;
  274.                 verify++;
  275.             }
  276.         break;
  277.     case '4':
  278.         for (int i = 0; i < m; i++)
  279.             for (int j = 0; j < n; j++)
  280.                 if (i == j)
  281.                     if (mas[i][j] == key)
  282.                     {
  283.                         cout << "Элемент был найден в " << i + 1 << " строке, " << j + 1 << " столбце" << endl;
  284.                         verify++;
  285.                     }
  286.         break;
  287.     case '5':
  288.         for (int i = 0; i < m; i++)
  289.             for (int j = 0; j < n; j++)
  290.                 if (n >= m)
  291.                     if (i + j == n - 1)
  292.                         if (mas[i][j] == key)
  293.                         {
  294.                             cout << "Элемент был найден в " << i + 1 << " строке, " << j + 1 << " столбце" << endl;
  295.                             verify++;
  296.                         }
  297.                         else {}
  298.                     else
  299.                         if (i + j == m - 1)
  300.                             if (mas[i][j] == key)
  301.                             {
  302.                                 cout << "Элемент был найден в " << i + 1 << " строке, " << j + 1 << " столбце" << endl;
  303.                                 verify++;
  304.                             }
  305.         break;
  306.     case '6':
  307.         for (int i = 0; i < m; i++)
  308.             for (int j = 0; j < n; j++)
  309.                 if (i >= j)
  310.                     if (mas[j][i] == key)
  311.                     {
  312.                         cout << "Элемент был найден в " << j + 1 << " строке, " << i + 1 << " столбце" << endl;
  313.                         verify++;
  314.                     }
  315.         break;
  316.     case '7':
  317.         for (int i = 0; i < m; i++)
  318.             for (int j = 0; j < n; j++)
  319.                 if (j >= i)
  320.                     if (mas[j][i] == key)
  321.                     {
  322.                         cout << "Элемент был найден в " << j + 1 << " строке, " << i + 1 << " столбце" << endl;
  323.                         verify++;
  324.                     }
  325.         break;
  326.     }
  327.     if (verify < 1)
  328.     {
  329.         cout << "Элемент не был найден" << endl;
  330.     }
  331. }
  332. void countTriangle(int n, int m)
  333. {
  334.     int i, j;
  335.     int upper_count = 0;
  336.     int lower_count = 0;
  337.     for (i = 0; i < m; i++)
  338.         for (j = 0; j < n; j++)
  339.             if (i >= j)
  340.                 upper_count++;
  341.  
  342.     printf("Верхняя сумма равна %d\n", upper_count);
  343.  
  344.     for (i = 0; i < m; i++)
  345.         for (j = 0; j < n; j++)
  346.             if (j >= i)
  347.                 lower_count++;
  348.  
  349.     printf("Нижняя сумма равна %d", lower_count);
  350. }
  351. void sortRow(int** Matrix, int n, int m)
  352. {
  353.     int row;
  354.     cout << "Сортируемая строка: "; cin >> row;
  355.     int tmp = 0;
  356.     for (int i = 0; i < m; i++)
  357.     {
  358.         for (int j = (m - 1); j >= (i + 1); j--)
  359.         {
  360.             if (Matrix[row - 1][j] < Matrix[row - 1][j - 1])
  361.             {
  362.                 tmp = Matrix[row - 1][j];
  363.                 Matrix[row - 1][j] = Matrix[row - 1][j - 1];
  364.                 Matrix[row - 1][j - 1] = tmp;
  365.             }
  366.         }
  367.     }
  368. }
  369. void sortColumn(int** Matrix, int n, int m)
  370. {
  371.     int column;
  372.     cout << "Сортируемый столбец: "; cin >> column;
  373.     int tmp = 0;
  374.     for (int i = 0; i < n; i++)
  375.     {
  376.         for (int j = (n - 1); j >= (i + 1); j--)
  377.         {
  378.             if (Matrix[j][column - 1] < Matrix[j - 1][column - 1])
  379.             {
  380.                 tmp = Matrix[j][column - 1];
  381.                 Matrix[j][column - 1] = Matrix[j - 1][column - 1];
  382.                 Matrix[j - 1][column - 1] = tmp;
  383.             }
  384.         }
  385.     }
  386. }
  387. void transposition(int** Matrix, const int n, const int m)
  388. {
  389.     int** arr = new int* [m];
  390.     for (int i = 0; i < m; i++)
  391.         arr[i] = new int[n];
  392.  
  393.     for (int i = 0; i < n; i++)
  394.         for (int j = 0; j < m; j++)
  395.             arr[j][i] = Matrix[i][j];
  396.  
  397.     cout << endl;
  398.     for (int i = 0; i < m; i++)
  399.     {
  400.         for (int j = 0; j < n; j++)
  401.         {
  402.             if (arr[i][j] < -9)
  403.                 cout << arr[i][j] << " ";
  404.             if (arr[i][j] >= -9 && arr[i][j] < 0)
  405.                 cout << " " << arr[i][j] << " ";
  406.             if (arr[i][j] > 0 && arr[i][j] < 9)
  407.                 cout << "  " << arr[i][j] << " ";
  408.             if (arr[i][j] > 9)
  409.                 cout << " " << arr[i][j] << " ";
  410.         }
  411.         cout << endl;
  412.     }
  413.     for (int i = 0; i < m; i++)
  414.         delete[] arr[i];
  415.     delete[] arr;
  416. }
  417. int** multyMatrix(int** Matrix, const int n, const int m)
  418. {
  419.     int number;
  420.     cout << "Введите число для умножение матрицы на число\n";
  421.     cin >> number;
  422.     for (int i = 0; i < n; i++)
  423.         for (int j = 0; j < m; j++)
  424.         {
  425.             Matrix[i][j] *= number;
  426.         }
  427.     return Matrix;
  428. }
  429. void firstColumn(int** Matrix, int n, int m)
  430. {
  431.     int result(m + 1);
  432.     for (int i(0); i < n; i++)
  433.     {
  434.         for (int j(0); j < m; j++)
  435.         {
  436.             if (Matrix[i][j] == 0)
  437.                 if (j < result)
  438.                     result = j;
  439.         }
  440.     }
  441.     if (result < m + 1)
  442.         cout << "Номер первого столбца содержащего нулевой элемент: " << result + 1 << endl;
  443.     else
  444.         cout << "Нулевой элемент в массиве не найден\n";
  445. }
  446. void descendingSort(int* Arr, const int n)
  447. {
  448.     int tmp = 0;
  449.     for (int i = 0; i < n; i++)
  450.     {
  451.         for (int j = (n - 1); j >= (i + 1); j--)
  452.         {
  453.             if (Arr[j] > Arr[j - 1])
  454.             {
  455.                 tmp = Arr[j];
  456.                 Arr[j] = Arr[j - 1];
  457.                 Arr[j - 1] = tmp;
  458.             }
  459.         }
  460.     }
  461. }
  462.  
  463.  
  464.  
  465. int main()
  466. {
  467.     setlocale(LC_ALL, "Russian");
  468.  
  469.     int n = 0, m = 0; string value;
  470.     int selectRow = 0, selectColumn = 0;
  471.     n = NumberElementN(n);
  472.     if (n == -1)
  473.         return 0;
  474.     m = NumberElementM(m);
  475.     if (m == -1)
  476.         return 0;
  477.     int** Matrix = new int* [n];
  478.     for (int i = 0; i < n; i++)
  479.         Matrix[i] = new int[m];
  480.     GetArray(n, m, Matrix, -100, 100);
  481.     PutTwoDismensArray(n, m, Matrix);
  482.     int* Array = new int[n * m];
  483.     cout << "\nХотите преобразовать матрицу в одномерный массив?\n1) Да\n2) Нет\n" << endl;
  484.     cin >> value;
  485.     InOneDimensional(Matrix, Array, n, m);
  486.     if (value == "1")
  487.     {
  488.         PutOneDismensArray(Array, n, m);
  489.         cout << endl;
  490.     }
  491.     cout << "Хотите отсортировать матрицу?\n1) Да\n2) Нет\n" << endl;
  492.     cin >> value;
  493.     cout << endl;
  494.     if (value == "1")
  495.     {
  496.         cout << "Всю матрицу?\n1) Да\n2) Только строку\n3) Только столбец\n" << endl;
  497.         cin >> value;
  498.         if (value == "2")
  499.             sortRow(Matrix, n, m);
  500.         else
  501.             if (value == "3")
  502.                 sortColumn(Matrix, n, m);
  503.             else {
  504.                 bubbleSort(Array, n * m);
  505.                 InTwoDimensional(Matrix, Array, n, m);
  506.             }
  507.         cout << "Отсортированная матрица: " << endl;
  508.         PutTwoDismensArray(n, m, Matrix);
  509.         cout << endl;
  510.     }
  511.     cout << "Выберите строку: "; cin >> selectRow;
  512.     if (selectRow <= 1 || selectRow >= n)
  513.     {
  514.         cout << "Этой строки нет\n" << endl;
  515.     }
  516.     long int MultyRow = 1, MultyColumn = 1, MultyMain = 1, MultySecond = 1;
  517.     int SumRow = 0, SumColumn = 0, SumMainDiagonal = 0, SumSecondaryDiagonal = 0;
  518.     cout << "Выберите столбец: "; cin >> selectColumn;
  519.     if (selectColumn >= 1 || selectColumn <= m)
  520.     {
  521.         SumElements(Matrix, n, m, selectRow, selectColumn, SumRow, SumColumn, MultyRow, MultyColumn);
  522.         cout << "Сумма строки " << selectRow << " равна ";
  523.         cout << SumRow << " , Произведение строки равна = " << MultyRow << endl;
  524.         cout << "Сумма столбца " << selectColumn << " = ";
  525.         cout << SumColumn << " , Произведение столбца = " << MultyColumn << endl;
  526.     }
  527.     else {
  528.         cout << "Этого столбца нет\n" << endl;
  529.     }
  530.     SumElementsDiagonal(Matrix, n, m, SumMainDiagonal, SumSecondaryDiagonal, MultyMain, MultySecond);
  531.     cout << "Сумма главной диагонали = " << SumMainDiagonal << " , Произведение главной = " << MultyMain << endl;
  532.     cout << "Сумма побочной строки = " << SumSecondaryDiagonal << " , Произведение побочной = " << MultySecond << endl;
  533.     cout << "\nХотите найти минимальный и максимальный элемент?\n1) Да\n2) Нет\n";
  534.     cin >> value;
  535.     InOneDimensional(Matrix, Array, n, m);
  536.     if (value == "1")
  537.     {
  538.         int num = 0;
  539.         if (m >= n)
  540.             num = m;
  541.         else
  542.             num = n;
  543.         int* selectedRowOrColumn = new int[num];
  544.  
  545.         cout << "Выберите строку: "; cin >> selectRow;
  546.         cout << "Выберите столбец: "; cin >> selectColumn;
  547.         cout << "\n\n";
  548.         cout << "-< Матрица >-" << endl;
  549.         MinElement(Array, n * m);
  550.         MaxElement(Array, n * m);
  551.         cout << "-< В строке " << selectRow << " >- " << endl;
  552.         InlDismMinElements(Matrix, selectedRowOrColumn, n, m, selectRow, selectColumn, 1);
  553.         MaxElement(selectedRowOrColumn, m);
  554.         MinElement(selectedRowOrColumn, m); cout << endl;
  555.         cout << "-< В столбце " << selectColumn << " >- " << endl;
  556.         InlDismMinElements(Matrix, selectedRowOrColumn, n, m, selectRow, selectColumn, 2);
  557.         MaxElement(selectedRowOrColumn, n);
  558.         MinElement(selectedRowOrColumn, n); cout << endl;
  559.         cout << "-< В главной диагонали >-" << endl;
  560.         InlDismMinElements(Matrix, selectedRowOrColumn, n, m, selectRow, selectColumn, 3);
  561.         MaxAndMinElementDiagonal(selectedRowOrColumn, n, m); cout << endl;
  562.         cout << "-< В побочной диагонали >-" << endl;
  563.         InlDismMinElements(Matrix, selectedRowOrColumn, n, m, selectRow, selectColumn, 4);
  564.         MaxAndMinElementDiagonal(selectedRowOrColumn, n, m);
  565.     }
  566.     cout << "\nХотите найти элемент в массиве?\n1) Да\n2) Нет\n";
  567.     cin >> value;
  568.     if (value == "1")
  569.         searchFunct(Matrix, n, m);
  570.     countTriangle(n, m);
  571.     cout << "\nХотите транспонировать матрицу?\n1) Да\n2) Нет\n";
  572.     cin >> value;
  573.     if (value == "1")
  574.         transposition(Matrix, n, m);
  575.     cout << "\nХотите умножить матрицу на число?\n1) Да\n2) Нет\n";
  576.     cin >> value;
  577.     if (value == "1")
  578.     {
  579.         Matrix = multyMatrix(Matrix, n, m);
  580.         PutTwoDismensArray(n, m, Matrix);
  581.     }
  582.     cout << "Поиск нулевого элемента:" << endl;
  583.     firstColumn(Matrix, n, m); cout << endl;
  584.     InOneDimensional(Matrix, Array, n, m);
  585.     descendingSort(Array, n * m);
  586.     InTwoDimensional(Matrix, Array, n, m);
  587.     PutTwoDismensArray(n, m, Matrix);
  588. }
Advertisement
Add Comment
Please, Sign In to add comment