Filage

Templates

Mar 19th, 2024
622
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. int takeNum(int Min, int Max) {
  7.     const string error = "Проверьте корректность введенных данных!\n";
  8.     bool isIncorrect;
  9.     int num;
  10.     do {
  11.         isIncorrect = false;
  12.         cin >> num;
  13.         if (cin.fail()) {
  14.             isIncorrect = true;
  15.             cout << error;
  16.             cin.clear();
  17.             while (cin.get() != '\n');
  18.         }
  19.         if (!isIncorrect && cin.get() != '\n') {
  20.             cin.clear();
  21.             while (cin.get() != '\n');
  22.             cout << error;
  23.             isIncorrect = true;
  24.         }
  25.         if (!isIncorrect && (num < Min || num > Max)) {
  26.             isIncorrect = true;
  27.             cout << error;
  28.         }
  29.     } while (isIncorrect);
  30.     return num;
  31. }
  32.  
  33. template <typename T>
  34. T** add(T** first_matrix, T** second_matrix, int height, int length) {
  35.     T** temp_matrix = new T * [height];
  36.     for (int i = 0; i < height; i++)
  37.         temp_matrix[i] = new T[length];
  38.  
  39.     for (int i = 0; i < height; i++)
  40.         for (int j = 0; j < length; j++)
  41.             temp_matrix[i][j] = first_matrix[i][j] + second_matrix[i][j];
  42.     return temp_matrix;
  43. }
  44.  
  45. template <typename T>
  46. T search(T** matrix, int height, int length) {
  47.     T max_negative = INT_MIN;
  48.     for (int i = 0; i < height; i++)
  49.         for (int j = 0; j < length; j++)
  50.             if (matrix[i][j] < 0 && matrix[i][j] > max_negative)
  51.                 max_negative = matrix[i][j];
  52.  
  53.     return max_negative;
  54. }
  55.  
  56. template <typename T>
  57. void replace(T** matrix , int height, int length) {
  58.     for (int i = 0; i < height; i++)
  59.         for (int j = 0; j < length; j++)
  60.             if (matrix[i][j] < 0)
  61.                 matrix[i][j] = abs(matrix[i][j]);
  62. }
  63.  
  64. template <typename T>
  65. void sort(T** matrix, int height, int length) {
  66.     T max = matrix[0][0];
  67.     int index = 0;
  68.     for (int i = 0; i < height; i++)
  69.         for (int j = 0; j < length; j++)
  70.             if (matrix[i][j] > max) {
  71.                 max = matrix[i][j];
  72.                 index = j;
  73.             }
  74.     for (int i = 0; i < height - 1; i++)
  75.         for (int j = 0; j < height - i - 1; j++)
  76.             if (matrix[j][index] < matrix[j + 1][index]) {
  77.                     T temp = matrix[j][index];
  78.                     matrix[j][index] = matrix[j + 1][index];
  79.                     matrix[j + 1][index] = temp;
  80.             }
  81. }
  82.  
  83. void createMatrix(int** matrix, int length, int height) {
  84.     cout << "Введите значения элементов в матрице: \n";
  85.     for (int i = 0; i < height; i++)
  86.         for (int j = 0; j < length; j++) {
  87.             cout << "a[" << i << "][" << j << "] = ";
  88.             cin >> matrix[i][j];
  89.         }
  90. }
  91.  
  92. void getSize(int& height, int& length) {
  93.     cout << "Введите количество строк в матрице: ";
  94.     cin >> height;
  95.     cout << "Введите количество столбцов в матрице: ";
  96.     cin >> length;
  97. }
  98.  
  99. void outputMatrix(int** matrix, int& length, int& height) {
  100.     cout << "Ваша матрица: \n";
  101.     for (int i = 0; i < height; i++)
  102.         for (int j = 0; j < length; j++)
  103.             cout << matrix[i][j] << " ";
  104.         cout << endl;
  105. }
  106.  
  107. void menu_program() {
  108.     setlocale(LC_ALL, "Rus");
  109.  
  110.     int length = 0, height = 0;
  111.     int** matrix = nullptr;
  112.     int** tempMatrix = nullptr;
  113.     int min = 0;
  114.     while (true) {
  115.         int choice;
  116.         cout << "Текстовое меню:\n";
  117.         cout << "1. Создание матрицы и ввод в нее значений\n";
  118.         cout << "2. Вывести матрицу\n";
  119.         cout << "3. Сложение матриц\n";
  120.         cout << "4. Поиск максимального отрицательного элемента в матрице\n";
  121.         cout << "5. Замена всех отрицательных элементов в матрице им противоположными\n";
  122.         cout << "6. Сортировка столбцов, содержащих максимальный элемент матрицы, по убыванию\n";
  123.         cout << "7. Выход\n";
  124.         cout << "Введите номер выбранного действия: ";
  125.         choice = takeNum(1, 2147483647);
  126.         switch (choice) {
  127.         case 1:
  128.             getSize(height, length);
  129.             matrix = new int* [height];
  130.             for (int i = 0; i < height; i++)
  131.                 matrix[i] = new int[length];
  132.             createMatrix(matrix, length, height);
  133.             break;
  134.         case 2:
  135.             system("cls");
  136.             outputMatrix(matrix, length, height);
  137.             break;
  138.         case 3:
  139.             tempMatrix = new int* [height];
  140.             for (int i = 0; i < height; i++)
  141.                 tempMatrix[i] = new int[length];
  142.             createMatrix(tempMatrix, length, height);
  143.             matrix = add(matrix, tempMatrix, height, length);
  144.             for (int i = 0; i < height; i++)
  145.                 delete[] tempMatrix[i];
  146.             delete[] tempMatrix;
  147.             break;
  148.         case 4:
  149.             min = search(matrix, height, length);
  150.             cout << "Максимальный отрицательный элемент это - " << min << endl;
  151.             break;
  152.         case 5:
  153.             replace(matrix, height, length);
  154.             cout << "Замена прошла успешно!" << endl;
  155.             break;
  156.         case 6:
  157.             sort(matrix, height, length);
  158.             cout << "Сортировка прошла успешно!" << endl;
  159.             break;
  160.         case 7:
  161.             cout << "Программа завершена!" << endl;
  162.             for (int i = 0; i < height; i++)
  163.                 delete[] matrix[i];
  164.             delete[] matrix;
  165.             return;
  166.         default:
  167.             cout << "Ошибка ввода! Введите то число, которое есть в меню\n";
  168.             break;
  169.         }
  170.     }
  171. }
  172.    
  173. int main() {
  174.     menu_program();
  175. }
  176.  
Advertisement
Add Comment
Please, Sign In to add comment