Advertisement
debosch

lab13

Dec 9th, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.48 KB | None | 0 0
  1. /*
  2.     Лабораторная работа №13
  3.     Шильцин Влад
  4.     Вариант-25.
  5.     Во входном файле записан русский текст.
  6.     Найти в тексте слова, в которые входят не менее пяти из шести
  7.     наиболее распространенных букв текста, записать их заглавными буквами
  8.     и указать после каждого такого слова в скобках найденные буквы.
  9.     Полученный текст записать в выходной файл.
  10.     Весь текст, кроме найденных слов, должен
  11.     остаться неизменным, включая и знаки препинания.
  12. */
  13.  
  14. #include <iostream>
  15. #include <Windows.h>
  16. #include <limits.h>
  17.  
  18. using namespace std;
  19.  
  20. void m_swap(int& a, int& b)
  21. {
  22.     int t = b;
  23.     b = a;
  24.     a = t;
  25. }
  26.  
  27. int get_index_of_the_max(int* a, const int size)
  28. {
  29.     int index_of_the_max = 0;
  30.     int max = INT_MIN;
  31.     for (int i = 0; i < size; i++)
  32.         if (a[i] > max)
  33.         {
  34.             max = a[i];
  35.             index_of_the_max = i;
  36.         }
  37.  
  38.     a[index_of_the_max] = 0;
  39.  
  40.     return index_of_the_max;
  41. }
  42.  
  43. void get_common_letters(int* a, char* b, const int size_a = 33, const int size_b = 6)
  44. {
  45.     int index = 0;
  46.     for (int i = 0; i < size_b; i++)
  47.     {
  48.         index = get_index_of_the_max(a, size_a);
  49.         if (index == 32)
  50.             b[i] = 'ё';
  51.         else
  52.             b[i] = (char)(index + 224);
  53.     }
  54. }
  55.  
  56. int get_count_of_letters(char* word, const int size_word, char* b, const int size_b = 6)
  57. {
  58.     int counter = 0;
  59.     for (int i = 0; i < size_word; i++)
  60.         for (int j = 0; j < size_b; j++)
  61.             if (tolower(word[i]) == tolower(b[j]))
  62.             {
  63.                 counter++;
  64.                 break;
  65.             }
  66.  
  67.     return counter;
  68. }
  69.  
  70. void print_word(char* word, const int size)
  71. {
  72.     for (int i = 0; i < size; i++)
  73.         cout << word[i];
  74. }
  75.  
  76. char m_toupper(char c)
  77. {
  78.     if (c > 'Я' && c <= 'я')
  79.         return (char)(c - 32);
  80.     if (c == 'ё')
  81.         c = 'Ё';
  82.  
  83.     return c;
  84. }
  85.  
  86. void toupper_word(char* word, const int size)
  87. {
  88.     for (int i = 0; i < size; i++)
  89.         word[i] = m_toupper(word[i]);
  90. }
  91.  
  92. bool char_in_word(char c, char* letters, const int size = 6)
  93. {
  94.     for (int i = 0; i < size; i++)
  95.         if (c == m_toupper(letters[i]))
  96.             return true;
  97.  
  98.     return false;
  99. }
  100.  
  101. void free_arr(char* arr, const int size)
  102. {
  103.     for (int i = 0; i < size; i++)
  104.         arr[i] = 0;
  105. }
  106.  
  107. int main()
  108. {
  109.     setlocale(LC_ALL, "Russian");
  110.  
  111.     const int SIZE = 6;
  112.     const int ALPHABET_SIZE = 33;
  113.  
  114.     FILE* stream = NULL;
  115.     FILE* stream2 = NULL;
  116.  
  117.     int* letters = new int[ALPHABET_SIZE];
  118.     char* common_letters = new char[SIZE];
  119.     char* word = new char[100];
  120.  
  121.     freopen_s(&stream, "input.txt", "r", stdin);
  122.  
  123.     int chars_count = 0;
  124.     int ch = 0;
  125.  
  126.     for (int i = 0; i < ALPHABET_SIZE; i++)
  127.         letters[i] = 0;
  128.  
  129.     while ((ch = getchar()) >= 0)
  130.         while ((((char)ch >= 'А' && (char)ch <= 'я' || (char)ch == 'ё' || (char)ch == 'Ё') && ch >= 0))
  131.         {
  132.             int index = 0;
  133.             if ((char)ch == 'Ё' || (char)ch == 'ё')
  134.                 index = 32;
  135.             else
  136.                 index = tolower(ch) - 224;
  137.  
  138.             letters[index]++;
  139.             ch = getchar();
  140.         }
  141.  
  142.     if (stream != 0)
  143.         fseek(stream, 0, SEEK_SET);
  144.  
  145.     free_arr(common_letters, SIZE);
  146.  
  147.     get_common_letters(letters, common_letters);
  148.  
  149.     freopen_s(&stream2, "output.txt", "w", stdout);
  150.  
  151.     free_arr(word, 100);
  152.  
  153.     ch = getchar();
  154.     while (true)
  155.     {
  156.         if (ch < 0) break;
  157.  
  158.         while (((char)ch < 'А' || (char)ch > 'я') && (char)ch != 'ё' && (char)ch != 'Ё' && ch >= 0)
  159.         {
  160.             cout << (char)ch;
  161.             ch = getchar();
  162.         }
  163.  
  164.         while ((((char)ch >= 'А' && (char)ch <= 'я'|| (char)ch == 'ё' || (char)ch == 'Ё') && ch >= 0))
  165.         {
  166.             word[chars_count] = (char)ch;
  167.             ch = getchar();
  168.             chars_count++;
  169.         }
  170.  
  171.         if (get_count_of_letters(word, chars_count, common_letters) >= 5 && chars_count > 0)
  172.         {
  173.             char* temp_letters = new char[SIZE];
  174.             free_arr(temp_letters, SIZE);
  175.             int count = 0;
  176.  
  177.             toupper_word(word, chars_count);
  178.             print_word(word, chars_count);
  179.  
  180.             for (int i = 0; i < chars_count; i++)
  181.                 if (char_in_word(word[i], common_letters) && !char_in_word(word[i], temp_letters))
  182.                     temp_letters[count++] = word[i];
  183.  
  184.             cout << "(";
  185.             for (int i = 0; i < count; i++)
  186.                 cout << temp_letters[i];
  187.             cout << ")";
  188.         }
  189.         else
  190.             print_word(word, chars_count);
  191.  
  192.         free_arr(word, chars_count);
  193.         chars_count = 0;
  194.     }
  195.  
  196.     if (stream2 != NULL)
  197.     {
  198.         fclose(stream2);
  199.     }
  200.  
  201.     if (stream != NULL)
  202.         fclose(stream);
  203.  
  204.     delete[] letters;
  205.     delete[] common_letters;
  206.     delete[] word;
  207.  
  208.     return 0;
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement