Advertisement
soss_nom

infa

Dec 15th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <ctime>
  4. #include <windows.h>
  5. using namespace std;
  6. BOOL WINAPI CtrlHandler(DWORD fdwCtrlType)
  7. {
  8.     switch (fdwCtrlType)
  9.     {
  10.     case CTRL_C_EVENT:
  11.         return TRUE;
  12.     case CTRL_CLOSE_EVENT:
  13.         return TRUE;
  14.     case CTRL_BREAK_EVENT:
  15.         return FALSE;
  16.     case CTRL_LOGOFF_EVENT:
  17.         return FALSE;
  18.     case CTRL_SHUTDOWN_EVENT:
  19.         return FALSE;
  20.     default:
  21.         return FALSE;
  22.     }
  23. }
  24. int checktemp(string s)//Перевод string в int
  25. {
  26.     int temp = 0;
  27.     int discharge = 1;
  28.     for (int i = s.length() - 1; i >= 0; i--)
  29.     {
  30.         if (s[i] < '0' || s[i] > '9')
  31.             return -1;
  32.         else
  33.             temp += (s[i] - '0') * discharge;
  34.         discharge *= 10;
  35.     }
  36.     return temp;
  37. }
  38. void print(int** matrix, int m)//Вывод матрицы
  39. {
  40.     for (int i = 0; i < m; i++)
  41.     {
  42.         for (int j = 0; j < m; j++)
  43.         {
  44.             cout << matrix[i][j] << "\t";
  45.         }
  46.         cout << endl;
  47.     }
  48. }
  49. void sort(int** matrix, int m)//Сортировка строк матрицы по убыванию
  50. {
  51.     int temp;
  52.     for (int i = 0; i < m; i++)
  53.     {
  54.         for (int j = 0; j < m; j++)
  55.         {
  56.             for (int k = j + 1; k < m; k++)
  57.             {
  58.                 if (matrix[i][j] > matrix[i][k])
  59.                 {
  60.                     temp = matrix[i][j];
  61.                     matrix[i][j] = matrix[i][k];
  62.                     matrix[i][k] = temp;
  63.                 }
  64.             }
  65.         }
  66.     }
  67. }
  68. /*
  69.  
  70. */
  71. int main()
  72. {
  73.     setlocale(LC_ALL, "rus");
  74.     srand(time(NULL));
  75.     int m, r, n, s;//m - размер матрицы; r - режим вывода матрицы; n - номер минимума из строчки; s - номер строчки;
  76.     string temp;
  77.     cout << "Задание: " << endl;
  78.     cout << "Создать квадратную матрицу размера MxM, где M является целым числом из диапазона" << endl;
  79.     cout << "[2,5]. Конкретный размер матрицы задается пользователем. Матрица содержит только" << endl;
  80.     cout << "целые числа из диапазона [0, 100], которые могут быть как случайными, так и " << endl;
  81.     cout << "вводиться пользователем. Отсортировать каждую строку матрицы по возрастанию, т.е." << endl;
  82.     cout << "минимальный элемент должен находиться в начале строки, максимальный – в конце. " << endl;
  83.     cout << "После сортировки найти N-ный минимум в заданной строке, где номер строки и N " << endl;
  84.     cout << "задается пользователем. Результаты обработки матрицы вывести на экран." << endl << endl;
  85.     cout << "Введите размер матрицы [2,5]: ";
  86.     SetConsoleCtrlHandler(CtrlHandler, TRUE);
  87.     cin.clear();
  88.     getline(cin, temp);
  89.     m = checktemp(temp);
  90.     while (m < 2 || m > 5)
  91.     {
  92.         cout << "Некорректное значение!\nПовторите ввод: ";
  93.         cin.clear();
  94.         getline(cin, temp);
  95.         m = checktemp(temp);
  96.     }
  97.     cout << "\nВведите режим заполнения\n(1 - рандомные числа, 2 - ввод от руки (числа [0,100])): ";
  98.     cin.clear();
  99.     getline(cin, temp);
  100.     r = checktemp(temp);
  101.     while (r != 1 && r != 2)
  102.     {
  103.         cout << "Некорректное значение!\nПовторите ввод: ";
  104.         cin.clear();
  105.         getline(cin, temp);
  106.         r = checktemp(temp);
  107.     }
  108.     int** matrix = new int* [m];//Выделение памяти под матрицу
  109.     for (int i = 0; i < m; i++)
  110.         matrix[i] = new int[m];
  111.     if (r == 2)
  112.         cout << "Начинайте вводить значения:\n";
  113.     for (int i = 0; i < m; i++)//Заполнение матрицы
  114.     {
  115.         for (int j = 0; j < m; j++)
  116.         {
  117.             if (r == 1)
  118.                 matrix[i][j] = rand() % 101;
  119.             else
  120.             {
  121.                 cout << "matrix[" << i << "][" << j << "] = ";
  122.                 cin.clear();
  123.                 getline(cin, temp);
  124.                 matrix[i][j] = checktemp(temp);
  125.                 while (matrix[i][j] < 0 || matrix[i][j] > 100)
  126.                 {
  127.                     cout << "Некорректное значение!\n";
  128.                     cout << "matrix[" << i << "][" << j << "] = ";
  129.                     getline(cin, temp);
  130.                     matrix[i][j] = checktemp(temp);
  131.                 }
  132.             }
  133.         }
  134.     }
  135.     cout << "\nПолученная матрица:\n";
  136.     print(matrix, m);
  137.     sort(matrix, m);
  138.     cout << "\nОтсортированная матрица:\n";
  139.     print(matrix, m);
  140.     cout << "\nВведите номер строчки[1,m]: ";
  141.     cin.clear();
  142.     getline(cin, temp);
  143.     s = checktemp(temp);
  144.     while (s<1 || s>m)
  145.     {
  146.         cout << "Некорректное значение!\nПовторите ввод: ";
  147.         cin.clear();
  148.         getline(cin, temp);
  149.         s = checktemp(temp);
  150.     }
  151.     cout << "\nВведите номер минимума N[1,m]: ";
  152.     cin.clear();
  153.     getline(cin, temp);
  154.     n = checktemp(temp);
  155.     while (n<1 || n>m)
  156.     {
  157.         cout << "Некорректное значение!\nПовторите ввод: ";
  158.         cin.clear();
  159.         getline(cin, temp);
  160.         n = checktemp(temp);
  161.     }
  162.     s--; n--;
  163.     cout << "\nНайденный минимум: " << matrix[s][n];
  164.     return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement