Advertisement
Kolyach

Инфа

Dec 28th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.08 KB | None | 0 0
  1.  
  2. #include "pch.h"
  3. #include <Windows.h>
  4. #include <iostream>
  5. #include <string>
  6. #include <ctime>
  7.  
  8. using namespace std;
  9.  
  10. int i, j;
  11.  
  12. int main()
  13. {
  14.     SetConsoleCtrlHandler(NULL, true);
  15.     setlocale(LC_ALL, "russian");
  16.     srand(time(NULL));
  17.  
  18.     cout << "Введите порядок (от 2 до 5) квадратной матрицы: ";
  19.     string por;
  20.     char por1 = 0;
  21.     cin >> por;
  22.     while (por.length() != 1) {
  23.         cin.clear();
  24.         cout << "Ошибка. Введите число от 2 до 5: ";
  25.         cin >> por;
  26.     }
  27.     por1 = por[0];
  28.     while (por1 < '2' || por1 > '5') {
  29.         cin.clear();
  30.         cout << "Ошибка. Введите число от 2 до 5: ";
  31.         cin >> por;
  32.         por1 = por[0];
  33.     }
  34.  
  35.     int p = int(por1) - 48;
  36.  
  37.     string **words = new string*[p];
  38.     for (int i = 0; i < p; i++) {
  39.         words[i] = new string[p];
  40.     }
  41.  
  42.  
  43.     cout << "Заполнить матрицу:" << endl << "1) Вручную" << endl << "2) Случайно" << endl << "-> ";
  44.     string c2;
  45.     char c1;
  46.     cin >> c2;
  47.     while (c2.length() != 1) {
  48.         cout << "Ошибка. Введите 1 или 2: ";
  49.         cin >> c2;
  50.         cin.clear();
  51.     }
  52.     c1 = c2[0];
  53.     while (c1 != '1' && c1 != '2') {
  54.         por1 = por[0];
  55.         cout << "Ошибка. Введите 1 или 2: ";
  56.         cin >> por;
  57.     }
  58.     int c = int(c1) - 48;
  59.     string word;
  60.     switch (c) {
  61.     case 1:
  62.         int i, j, n;
  63.         cout << "Введите " << p * p << " слов из четырёх букв английского алфавита" << endl;
  64.         char ch;
  65.         bool check;
  66.         for (i = 0; i < p; i++) {
  67.             for (j = 0; j < p; j++) {
  68.                 check = false;
  69.                 cout << endl << "Введите элемент: ";
  70.                 while (check == false) {
  71.                     cin >> word;
  72.                     while (word.length() != 4) {
  73.                         cout << "Неверная длина слова. Введите слово из 4-х букв: ";
  74.                         cin >> word;
  75.                     }
  76.                     for (int n = 0; n < 4; n++) {
  77.                         ch = word[n];
  78.                         if (((ch >= 'A') && (ch <= 'Z')) || ((ch >= 'a') && (ch <= 'z')))
  79.                             check = true;
  80.                         else {
  81.                             check = false;
  82.                             cout << "Слово должно состоять только из букв английского алфавита. Ввведите новое: ";
  83.                             break;
  84.                         }
  85.                     }
  86.                 }
  87.                 cout << "Принято";
  88.                 words[i][j] = word;
  89.             }
  90.         }
  91.         break;
  92.     case 2:
  93.         string word(4, 0);
  94.         int tmp = 0;
  95.         for (int i = 0; i < p; i++)
  96.             for (int j = 0; j < p; j++) {
  97.                 for (int n = 0; n < 4; n++) {
  98.                     while (tmp < 97 || tmp>122)
  99.                         tmp = rand() % 205 + 50;
  100.                     word[n] = tmp;
  101.                     tmp = 0;
  102.                 }
  103.                 words[i][j] = word;
  104.             }
  105.  
  106.         cout << endl << "Сгенерированный массив:";
  107.         for (i = 0; i < p; i++) {
  108.             cout << endl << endl;
  109.             for (j = 0; j < p; j++)
  110.                 cout << words[i][j] << "\t";
  111.         }
  112.     }
  113.  
  114.  
  115.     char ch;
  116.     for (i = 0; i < p; i++) {
  117.         for (j = 0; j < p; j++) {
  118.             word = words[i][j];
  119.             int n = 0;
  120.             char ch = '0';
  121.             while (n < word.length()) {
  122.                 ch = word[n]; //A E I O U Y
  123.                 n++;
  124.                 if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U' || ch == 'Y' || ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'y') {
  125.                     n--;
  126.                     word.erase(n, 1);
  127.                 }
  128.             }
  129.             if (word.length() == 0)
  130.                 word = "*";
  131.             words[i][j] = word;
  132.         }
  133.  
  134.     }
  135.     cout << endl << endl << "Обработанный массив:";
  136.     for (i = 0; i < p; i++) {
  137.         cout << endl << endl;
  138.         for (j = 0; j < p; j++)
  139.             cout << words[i][j] << "\t";
  140.     }
  141.  
  142.     int *leng = new int[p*p];
  143.  
  144.     cout << endl << endl;
  145.  
  146.     int n = 0;
  147.     for (i = 0; i < p; i++) {
  148.         for (j = 0; j < p; j++) {
  149.             word = words[i][j];
  150.             if (word.find('*') != -1)
  151.                 leng[n] = 0;
  152.             else
  153.                 leng[n] = word.length();
  154.             n++;
  155.         }
  156.     }
  157.  
  158.  
  159.     int temp;
  160.     for (int i = 0; i < p*p - 1; i++) {
  161.         for (int j = 0; j < p*p - i - 1; j++) {
  162.             if (leng[j] < leng[j + 1]) {
  163.                 temp = leng[j];
  164.                 leng[j] = leng[j + 1];
  165.                 leng[j + 1] = temp;
  166.             }
  167.         }
  168.     }
  169.  
  170.     cout << endl << "Массив из размеров строк матрицы:" << endl;
  171.  
  172.     for (int n = 0; n < p*p; n++)
  173.         cout << leng[n] << " ";
  174.     cout << endl;
  175.  
  176.     cout << endl;
  177.  
  178.     system("pause");
  179.     return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement