Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Лабораторная работа №13
- Шильцин Влад
- Вариант-25.
- Во входном файле записан русский текст.
- Найти в тексте слова, в которые входят не менее пяти из шести
- наиболее распространенных букв текста, записать их заглавными буквами
- и указать после каждого такого слова в скобках найденные буквы.
- Полученный текст записать в выходной файл.
- Весь текст, кроме найденных слов, должен
- остаться неизменным, включая и знаки препинания.
- */
- #include <iostream>
- #include <Windows.h>
- #include <limits.h>
- using namespace std;
- void m_swap(int& a, int& b)
- {
- int t = b;
- b = a;
- a = t;
- }
- int get_index_of_the_max(int* a, const int size)
- {
- int index_of_the_max = 0;
- int max = INT_MIN;
- for (int i = 0; i < size; i++)
- if (a[i] > max)
- {
- max = a[i];
- index_of_the_max = i;
- }
- a[index_of_the_max] = 0;
- return index_of_the_max;
- }
- void get_common_letters(int* a, char* b, const int size_a = 33, const int size_b = 6)
- {
- int index = 0;
- for (int i = 0; i < size_b; i++)
- {
- index = get_index_of_the_max(a, size_a);
- if (index == 32)
- b[i] = 'ё';
- else
- b[i] = (char)(index + 224);
- }
- }
- int get_count_of_letters(char* word, const int size_word, char* b, const int size_b = 6)
- {
- int counter = 0;
- for (int i = 0; i < size_word; i++)
- for (int j = 0; j < size_b; j++)
- if (tolower(word[i]) == tolower(b[j]))
- {
- counter++;
- break;
- }
- return counter;
- }
- void print_word(char* word, const int size)
- {
- for (int i = 0; i < size; i++)
- cout << word[i];
- }
- char m_toupper(char c)
- {
- if (c > 'Я' && c <= 'я')
- return (char)(c - 32);
- if (c == 'ё')
- c = 'Ё';
- return c;
- }
- void toupper_word(char* word, const int size)
- {
- for (int i = 0; i < size; i++)
- word[i] = m_toupper(word[i]);
- }
- bool char_in_word(char c, char* letters, const int size = 6)
- {
- for (int i = 0; i < size; i++)
- if (c == m_toupper(letters[i]))
- return true;
- return false;
- }
- void free_arr(char* arr, const int size)
- {
- for (int i = 0; i < size; i++)
- arr[i] = 0;
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- const int SIZE = 6;
- const int ALPHABET_SIZE = 33;
- FILE* stream = NULL;
- FILE* stream2 = NULL;
- int* letters = new int[ALPHABET_SIZE];
- char* common_letters = new char[SIZE];
- char* word = new char[100];
- freopen_s(&stream, "input.txt", "r", stdin);
- int chars_count = 0;
- int ch = 0;
- for (int i = 0; i < ALPHABET_SIZE; i++)
- letters[i] = 0;
- while ((ch = getchar()) >= 0)
- while ((((char)ch >= 'А' && (char)ch <= 'я' || (char)ch == 'ё' || (char)ch == 'Ё') && ch >= 0))
- {
- int index = 0;
- if ((char)ch == 'Ё' || (char)ch == 'ё')
- index = 32;
- else
- index = tolower(ch) - 224;
- letters[index]++;
- ch = getchar();
- }
- if (stream != 0)
- fseek(stream, 0, SEEK_SET);
- free_arr(common_letters, SIZE);
- get_common_letters(letters, common_letters);
- freopen_s(&stream2, "output.txt", "w", stdout);
- free_arr(word, 100);
- ch = getchar();
- while (true)
- {
- if (ch < 0) break;
- while (((char)ch < 'А' || (char)ch > 'я') && (char)ch != 'ё' && (char)ch != 'Ё' && ch >= 0)
- {
- cout << (char)ch;
- ch = getchar();
- }
- while ((((char)ch >= 'А' && (char)ch <= 'я'|| (char)ch == 'ё' || (char)ch == 'Ё') && ch >= 0))
- {
- word[chars_count] = (char)ch;
- ch = getchar();
- chars_count++;
- }
- if (get_count_of_letters(word, chars_count, common_letters) >= 5 && chars_count > 0)
- {
- char* temp_letters = new char[SIZE];
- free_arr(temp_letters, SIZE);
- int count = 0;
- toupper_word(word, chars_count);
- print_word(word, chars_count);
- for (int i = 0; i < chars_count; i++)
- if (char_in_word(word[i], common_letters) && !char_in_word(word[i], temp_letters))
- temp_letters[count++] = word[i];
- cout << "(";
- for (int i = 0; i < count; i++)
- cout << temp_letters[i];
- cout << ")";
- }
- else
- print_word(word, chars_count);
- free_arr(word, chars_count);
- chars_count = 0;
- }
- if (stream2 != NULL)
- {
- fclose(stream2);
- }
- if (stream != NULL)
- fclose(stream);
- delete[] letters;
- delete[] common_letters;
- delete[] word;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement