Advertisement
fferum

Untitled

May 28th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.99 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <string.h>
  4. #include<locale.h>
  5.  
  6.  
  7. void text_input(char*** sentences, int* amount_of_sentences);
  8. int asking();
  9. void first(char*** sentences, int* amount_of_sentences);
  10. void second(char*** sentences, int* amount_of_sentences);
  11. void text_to_sentences(char*** sentences, char* text, int* amount_of_sentences, int size_of_text);
  12. char* insert_sentence_in_array(char* buffer, int amount_of_characters_in_sentence, int current_sentence);
  13. char** sentences_to_words(char** sentences, int* amount_of_sentences, int* amount_of_words);
  14. void print_sentences(char** sentences, int* amount_of_sentences);
  15. void sentence_input(char** sentence);
  16. void print_words(char** words, int* amount_of_words);
  17. char** sort_words(char** words, int* amount_of_words);
  18. bool need_to_change(char* word1, char* word2);
  19. char to_lower_case(char letter);
  20. int main() {
  21.     setlocale(LC_ALL, "Russian");
  22.     // Создаем массив предложений
  23.     char** sentences = nullptr;
  24.     char*** sent_ptr = &sentences;
  25.     // Создаем переменную хранящую количество предложений
  26.     int* amount_of_sentences = new int;
  27.     // Ввод предложений
  28.     text_input(sent_ptr, amount_of_sentences);
  29.     // Вывод предложений
  30.     print_sentences(sentences, amount_of_sentences);
  31.     // Создаем массив хранящий слова
  32.     char** words;
  33.     // Создаем переменную хранящую количество слов
  34.     int* amount_of_words = new int;
  35.     // Превращяем предложения в слова
  36.     words = sentences_to_words(sentences, amount_of_sentences, amount_of_words);
  37.     // Выводим несортированный массив слов
  38.     print_words(words, amount_of_words);
  39.     // Сортируем массив слов
  40.     char** sorted_words = sort_words(words, amount_of_words);
  41.     // Выводим сортированный массив слов
  42.     print_words(sorted_words, amount_of_words);
  43.     // Чистим память
  44.     for (int i = 0; i < *amount_of_sentences; i++) {
  45.         delete[] sentences[i];
  46.     }
  47.     delete[] sentences;
  48.     for (int i = 0; i < *amount_of_words; i++) {
  49.         delete[] words[i];
  50.         delete[] sorted_words[i];
  51.     }
  52.     delete[] words;
  53.     delete[] sorted_words;
  54.     delete amount_of_sentences;
  55.     delete amount_of_words;
  56.     system("pause");
  57. }
  58. void text_input(char*** sentences, int* amount_of_sentences) {
  59.     // Спрашиваем у пользователя каким способом он хочет заполнить массив предложений
  60.     int answer = asking();
  61.     if (answer == 1) {
  62.         //Ввод предложений вручную задавая количество предложений и длину каждого предложения
  63.         first(sentences, amount_of_sentences);
  64.     }
  65.     else {
  66.         //Ввод предложений автоматически, введя лишь все предложения разделяя точкой
  67.         second(sentences, amount_of_sentences);
  68.     }
  69. }
  70. int asking() {
  71.     // Задаем вопрос
  72.     printf("Как вы хотите заполнить предложения ?(напишите 1 или 2)\n");
  73.     printf("1) в ручную вводя количество предложений и длину каждого предложения \n");
  74.     printf("2) автоматически, вводя размер текста и разделяя предложения точкой\n");
  75.     // Создаем переменную которая хранит ответ на вопрос
  76.     int answer;
  77.     // Получаем ответ
  78.     scanf_s("%d", &answer);
  79.     while (answer != 1 && answer != 2) {
  80.         // Повторно спрашиваем, если пользователь ввел недопустимый ответ
  81.         printf("ну кто так пишет!");
  82.         printf("Как вы хотите заполнить предложения ?(напишите 1 или 2)\n");
  83.         printf("1) в ручную вводя количество предложений и длину каждого предложения \n");
  84.         printf("2) автоматически, вводя размер текста и разделяя предложения точкой\n");
  85.         scanf_s("%d", &answer);
  86.     }
  87.     return answer;
  88. }
  89. void first(char*** sentences, int* amount_of_sentences) {
  90.     printf("сколько предложений в твоем тексте");
  91.     scanf_s("%d", amount_of_sentences);
  92.     char** array = new char* [*amount_of_sentences];
  93.     *sentences = array;
  94.  
  95.     for (int i = 0; i < *amount_of_sentences; i++) {
  96.         char** sentence = &array[i];
  97.         sentence_input(sentence);
  98.     }
  99. }
  100. void sentence_input(char** array) {
  101.     int number_of_letters;
  102.     printf("сколько символов в твоем предложении?");
  103.     scanf_s("%d", &number_of_letters);
  104.     char* sentence = new char[number_of_letters + 1];
  105.     printf("напишите предложение: \n");
  106.     getchar();
  107.     fgets(sentence, number_of_letters, stdin);
  108.     for (int i = 0; i < number_of_letters; i++)
  109.     {
  110.         if (sentence[i] == '\n')
  111.         {
  112.             sentence[i] = '\0';
  113.             break;
  114.         }
  115.         else if (i == number_of_letters - 1)
  116.         {
  117.             sentence[i + 1] = '\0';
  118.         }
  119.     }
  120.     *array = sentence;
  121. }
  122. void second(char*** sentences, int* amount_of_sentences) {
  123.     printf("напишите размер текста: ");
  124.     int size_of_text;
  125.     scanf_s("%d", &size_of_text);
  126.     char* text = new char[size_of_text + 1];
  127.     printf("введите текст разделяя педложения точкой:\n", size_of_text);
  128.     getchar();
  129.     fgets(text, size_of_text, stdin);
  130.     for (int i = 0; i < size_of_text; i++)
  131.     {
  132.         if (text[i] == '\n')
  133.         {
  134.             text[i] = '\0';
  135.             break;
  136.         }
  137.         else if (i == size_of_text - 1)
  138.         {
  139.             text[i + 1] = '\0';
  140.         }
  141.     }
  142.     text_to_sentences(sentences, text, amount_of_sentences, size_of_text);
  143.     delete[] text;
  144. }
  145. void text_to_sentences(char*** sentences, char* text, int* amount_of_sentences, int size_of_text) {
  146.     int counter_of_sentence = 0;
  147.     int i = 0;
  148.     while (text[i] != '\0') {
  149.         if (text[i] == '.') { counter_of_sentence++; }
  150.         i++;
  151.     }
  152.     *amount_of_sentences = counter_of_sentence;
  153.     char** array = new char* [*amount_of_sentences];
  154.     *sentences = array;
  155.     char* buffer = new char[size_of_text];
  156.     int amount_of_characters_in_sentence = 0;
  157.     int current_sentence = 0;
  158.     for (int i = 0; i < size_of_text; i++) {
  159.         if ((text[i] != '.') && (text[i] != '\0')) {
  160.             buffer[amount_of_characters_in_sentence] = text[i];
  161.             amount_of_characters_in_sentence++;
  162.         }
  163.         else if (text[i] == '.') {
  164.             buffer[amount_of_characters_in_sentence] = '\0';
  165.             array[current_sentence] = insert_sentence_in_array(buffer, amount_of_characters_in_sentence, current_sentence);
  166.             amount_of_characters_in_sentence = 0;
  167.             current_sentence++;
  168.         }
  169.     }
  170.     delete[] buffer;
  171. }
  172. char* insert_sentence_in_array(char* buffer, int amount_of_characters_in_sentence, int current_sentence) {
  173.     char* array = new char[amount_of_characters_in_sentence + 1];
  174.     int j = 0;
  175.     int k = 0;
  176.     while (buffer[j] != '\0') {
  177.         if ((j == 0) && (buffer[j] == ' ')) {
  178.             j++;
  179.         }
  180.         else {
  181.             array[k] = buffer[j];
  182.             k++;
  183.             j++;
  184.         }
  185.     }
  186.     array[k] = '\0';
  187.     return array;
  188. }
  189. void print_sentences(char** sentences, int* amount_of_sentences) {
  190.     printf("список предложлений:\n");
  191.     for (int i = 0; i < *amount_of_sentences; i++) {
  192.         printf("%s\n", sentences[i]);
  193.     }
  194. }
  195. char** sentences_to_words(char** sentences, int* amount_of_sentences, int* amount_of_words) {
  196.     int number_of_words = 0;
  197.     for (int i = 0; i < *amount_of_sentences; i++) {
  198.         int j = 0;
  199.         while (sentences[i][j] != '\0') {
  200.             if ((sentences[i][j] == ' ')) {
  201.                 number_of_words++;
  202.             }
  203.             j++;
  204.         }
  205.         number_of_words++;
  206.     }
  207.     *amount_of_words = number_of_words;
  208.     char** words = new char* [number_of_words];
  209.     const int max_size_of_word = 13;
  210.     for (int i = 0; i < number_of_words; i++) {
  211.         words[i] = new char[max_size_of_word + 1];
  212.     }
  213.     char* buffer = new char[max_size_of_word + 1];
  214.     int k = 0;
  215.     int current_word = 0;
  216.     for (int i = 0; i < *amount_of_sentences; i++) {
  217.         int j = 0;
  218.         while (sentences[i][j] != '\0') {
  219.             if (sentences[i][j] != ' ') {
  220.                 buffer[k] = to_lower_case(sentences[i][j]);
  221.                 j++;
  222.                 k++;
  223.             }
  224.             else {
  225.                 buffer[k] = '\0';
  226.                 for (int l = 0; l < k + 1; l++) {
  227.                     words[current_word][l] = buffer[l];
  228.                 }
  229.                 j++;
  230.                 k = 0;
  231.                 current_word++;
  232.             }
  233.         }
  234.         buffer[k] = '\0';
  235.         for (int l = 0; l < k + 1; l++) {
  236.             words[current_word][l] = buffer[l];
  237.         }
  238.         j++;
  239.         k = 0;
  240.         current_word++;
  241.     }
  242.     return words;
  243. }
  244. char to_lower_case(char letter) {
  245.     const int EN_letters = 26;
  246.     char alphabet[][EN_letters] = { { 'A','B' ,'C' ,'D' ,'E' ,'F' ,'G' ,'H' ,'I' ,'J' ,'K' ,'L' ,'M' ,'N' ,'O' ,'P' ,'Q' ,'R' ,'S' ,'T' ,'U' ,'V' ,'W' ,'X' ,'Y' ,'Z' },
  247.                                     { 'a','b' ,'c' ,'d' ,'e' ,'f' ,'g' ,'h' ,'i' ,'j' ,'k' ,'l' ,'m' ,'n' ,'o' ,'p' ,'q' ,'r' ,'s' ,'t' ,'u' ,'v' ,'w' ,'x' ,'y' ,'z' } };
  248.     for (int i = 0; i < EN_letters; i++) {
  249.         if (letter == alphabet[0][i]) {
  250.             letter = alphabet[1][i];
  251.         }
  252.     }
  253.     return letter;
  254. }
  255. void print_words(char** words, int* amount_of_words)
  256. {
  257.     printf("слова:\n\n");
  258.     for (int i = 0; i < *amount_of_words; i++) {
  259.         printf("%s\n", words[i]);
  260.     }
  261.     printf("\n\n");
  262. }
  263. char** sort_words(char** words, int* amount_of_words) {
  264.     int max_word_len = 0;
  265.     for (int i = 0; i < *amount_of_words; i++) {
  266.         if (max_word_len < (strlen(words[i]) + 1)) {
  267.             max_word_len = (strlen(words[i]) + 1);
  268.         }
  269.     }
  270.     char** sorted_words = new char* [*amount_of_words];
  271.     for (int i = 0; i < *amount_of_words; i++) {
  272.         sorted_words[i] = new char[max_word_len];
  273.         int j = 0;
  274.         while (words[i][j] != '\0') {
  275.             sorted_words[i][j] = words[i][j];
  276.             j++;
  277.         }
  278.         sorted_words[i][j] = words[i][j];
  279.     }
  280.     for (int i = 0; i < *amount_of_words - 1; i++) {
  281.         for (int j = 0; j < *amount_of_words - i - 1; j++) {
  282.             if (need_to_change(sorted_words[j], sorted_words[j + 1])) {
  283.                 int max_len;
  284.                 char* buffer;
  285.                 if (strlen(sorted_words[j]) > strlen(sorted_words[j + 1])) {
  286.                     max_len = strlen(sorted_words[j]) + 1;
  287.                 }
  288.                 else {
  289.                     max_len = strlen(sorted_words[j + 1]) + 1;
  290.                 }
  291.                 buffer = new char[max_len];
  292.                 for (int k = 0; k < max_len; k++) {
  293.                     buffer[k] = sorted_words[j][k];
  294.                     sorted_words[j][k] = sorted_words[j + 1][k];
  295.                     sorted_words[j + 1][k] = buffer[k];
  296.                 }
  297.                 delete[] buffer;
  298.             }
  299.         }
  300.     }
  301.     return sorted_words;
  302. }
  303. bool need_to_change(char* word1, char* word2) {
  304.     int i = 0;
  305.     while ((word1[i] != '\0') && (word2[i] != '\0')) {
  306.         if (word1[i] > word2[i]) {
  307.             return true;
  308.         }
  309.         i++;
  310.     }
  311.     if (strlen(word1) > strlen(word2)) {
  312.         return true;
  313.     }
  314.     else {
  315.         return false;
  316.     }
  317.  
  318. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement