Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cstdlib>
- #include <string.h>
- #include<locale.h>
- void text_input(char*** sentences, int* amount_of_sentences);
- int asking();
- void first(char*** sentences, int* amount_of_sentences);
- void second(char*** sentences, int* amount_of_sentences);
- void text_to_sentences(char*** sentences, char* text, int* amount_of_sentences, int size_of_text);
- char* insert_sentence_in_array(char* buffer, int amount_of_characters_in_sentence, int current_sentence);
- char** sentences_to_words(char** sentences, int* amount_of_sentences, int* amount_of_words);
- void print_sentences(char** sentences, int* amount_of_sentences);
- void sentence_input(char** sentence);
- void print_words(char** words, int* amount_of_words);
- char** sort_words(char** words, int* amount_of_words);
- bool need_to_change(char* word1, char* word2);
- char to_lower_case(char letter);
- int main() {
- setlocale(LC_ALL, "Russian");
- // Создаем массив предложений
- char** sentences = nullptr;
- char*** sent_ptr = &sentences;
- // Создаем переменную хранящую количество предложений
- int* amount_of_sentences = new int;
- // Ввод предложений
- text_input(sent_ptr, amount_of_sentences);
- // Вывод предложений
- print_sentences(sentences, amount_of_sentences);
- // Создаем массив хранящий слова
- char** words;
- // Создаем переменную хранящую количество слов
- int* amount_of_words = new int;
- // Превращяем предложения в слова
- words = sentences_to_words(sentences, amount_of_sentences, amount_of_words);
- // Выводим несортированный массив слов
- print_words(words, amount_of_words);
- // Сортируем массив слов
- char** sorted_words = sort_words(words, amount_of_words);
- // Выводим сортированный массив слов
- print_words(sorted_words, amount_of_words);
- // Чистим память
- for (int i = 0; i < *amount_of_sentences; i++) {
- delete[] sentences[i];
- }
- delete[] sentences;
- for (int i = 0; i < *amount_of_words; i++) {
- delete[] words[i];
- delete[] sorted_words[i];
- }
- delete[] words;
- delete[] sorted_words;
- delete amount_of_sentences;
- delete amount_of_words;
- system("pause");
- }
- void text_input(char*** sentences, int* amount_of_sentences) {
- // Спрашиваем у пользователя каким способом он хочет заполнить массив предложений
- int answer = asking();
- if (answer == 1) {
- //Ввод предложений вручную задавая количество предложений и длину каждого предложения
- first(sentences, amount_of_sentences);
- }
- else {
- //Ввод предложений автоматически, введя лишь все предложения разделяя точкой
- second(sentences, amount_of_sentences);
- }
- }
- int asking() {
- // Задаем вопрос
- printf("Как вы хотите заполнить предложения ?(напишите 1 или 2)\n");
- printf("1) в ручную вводя количество предложений и длину каждого предложения \n");
- printf("2) автоматически, вводя размер текста и разделяя предложения точкой\n");
- // Создаем переменную которая хранит ответ на вопрос
- int answer;
- // Получаем ответ
- scanf_s("%d", &answer);
- while (answer != 1 && answer != 2) {
- // Повторно спрашиваем, если пользователь ввел недопустимый ответ
- printf("ну кто так пишет!");
- printf("Как вы хотите заполнить предложения ?(напишите 1 или 2)\n");
- printf("1) в ручную вводя количество предложений и длину каждого предложения \n");
- printf("2) автоматически, вводя размер текста и разделяя предложения точкой\n");
- scanf_s("%d", &answer);
- }
- return answer;
- }
- void first(char*** sentences, int* amount_of_sentences) {
- printf("сколько предложений в твоем тексте");
- scanf_s("%d", amount_of_sentences);
- char** array = new char* [*amount_of_sentences];
- *sentences = array;
- for (int i = 0; i < *amount_of_sentences; i++) {
- char** sentence = &array[i];
- sentence_input(sentence);
- }
- }
- void sentence_input(char** array) {
- int number_of_letters;
- printf("сколько символов в твоем предложении?");
- scanf_s("%d", &number_of_letters);
- char* sentence = new char[number_of_letters + 1];
- printf("напишите предложение: \n");
- getchar();
- fgets(sentence, number_of_letters, stdin);
- for (int i = 0; i < number_of_letters; i++)
- {
- if (sentence[i] == '\n')
- {
- sentence[i] = '\0';
- break;
- }
- else if (i == number_of_letters - 1)
- {
- sentence[i + 1] = '\0';
- }
- }
- *array = sentence;
- }
- void second(char*** sentences, int* amount_of_sentences) {
- printf("напишите размер текста: ");
- int size_of_text;
- scanf_s("%d", &size_of_text);
- char* text = new char[size_of_text + 1];
- printf("введите текст разделяя педложения точкой:\n", size_of_text);
- getchar();
- fgets(text, size_of_text, stdin);
- for (int i = 0; i < size_of_text; i++)
- {
- if (text[i] == '\n')
- {
- text[i] = '\0';
- break;
- }
- else if (i == size_of_text - 1)
- {
- text[i + 1] = '\0';
- }
- }
- text_to_sentences(sentences, text, amount_of_sentences, size_of_text);
- delete[] text;
- }
- void text_to_sentences(char*** sentences, char* text, int* amount_of_sentences, int size_of_text) {
- int counter_of_sentence = 0;
- int i = 0;
- while (text[i] != '\0') {
- if (text[i] == '.') { counter_of_sentence++; }
- i++;
- }
- *amount_of_sentences = counter_of_sentence;
- char** array = new char* [*amount_of_sentences];
- *sentences = array;
- char* buffer = new char[size_of_text];
- int amount_of_characters_in_sentence = 0;
- int current_sentence = 0;
- for (int i = 0; i < size_of_text; i++) {
- if ((text[i] != '.') && (text[i] != '\0')) {
- buffer[amount_of_characters_in_sentence] = text[i];
- amount_of_characters_in_sentence++;
- }
- else if (text[i] == '.') {
- buffer[amount_of_characters_in_sentence] = '\0';
- array[current_sentence] = insert_sentence_in_array(buffer, amount_of_characters_in_sentence, current_sentence);
- amount_of_characters_in_sentence = 0;
- current_sentence++;
- }
- }
- delete[] buffer;
- }
- char* insert_sentence_in_array(char* buffer, int amount_of_characters_in_sentence, int current_sentence) {
- char* array = new char[amount_of_characters_in_sentence + 1];
- int j = 0;
- int k = 0;
- while (buffer[j] != '\0') {
- if ((j == 0) && (buffer[j] == ' ')) {
- j++;
- }
- else {
- array[k] = buffer[j];
- k++;
- j++;
- }
- }
- array[k] = '\0';
- return array;
- }
- void print_sentences(char** sentences, int* amount_of_sentences) {
- printf("список предложлений:\n");
- for (int i = 0; i < *amount_of_sentences; i++) {
- printf("%s\n", sentences[i]);
- }
- }
- char** sentences_to_words(char** sentences, int* amount_of_sentences, int* amount_of_words) {
- int number_of_words = 0;
- for (int i = 0; i < *amount_of_sentences; i++) {
- int j = 0;
- while (sentences[i][j] != '\0') {
- if ((sentences[i][j] == ' ')) {
- number_of_words++;
- }
- j++;
- }
- number_of_words++;
- }
- *amount_of_words = number_of_words;
- char** words = new char* [number_of_words];
- const int max_size_of_word = 13;
- for (int i = 0; i < number_of_words; i++) {
- words[i] = new char[max_size_of_word + 1];
- }
- char* buffer = new char[max_size_of_word + 1];
- int k = 0;
- int current_word = 0;
- for (int i = 0; i < *amount_of_sentences; i++) {
- int j = 0;
- while (sentences[i][j] != '\0') {
- if (sentences[i][j] != ' ') {
- buffer[k] = to_lower_case(sentences[i][j]);
- j++;
- k++;
- }
- else {
- buffer[k] = '\0';
- for (int l = 0; l < k + 1; l++) {
- words[current_word][l] = buffer[l];
- }
- j++;
- k = 0;
- current_word++;
- }
- }
- buffer[k] = '\0';
- for (int l = 0; l < k + 1; l++) {
- words[current_word][l] = buffer[l];
- }
- j++;
- k = 0;
- current_word++;
- }
- return words;
- }
- char to_lower_case(char letter) {
- const int EN_letters = 26;
- 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' },
- { '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' } };
- for (int i = 0; i < EN_letters; i++) {
- if (letter == alphabet[0][i]) {
- letter = alphabet[1][i];
- }
- }
- return letter;
- }
- void print_words(char** words, int* amount_of_words)
- {
- printf("слова:\n\n");
- for (int i = 0; i < *amount_of_words; i++) {
- printf("%s\n", words[i]);
- }
- printf("\n\n");
- }
- char** sort_words(char** words, int* amount_of_words) {
- int max_word_len = 0;
- for (int i = 0; i < *amount_of_words; i++) {
- if (max_word_len < (strlen(words[i]) + 1)) {
- max_word_len = (strlen(words[i]) + 1);
- }
- }
- char** sorted_words = new char* [*amount_of_words];
- for (int i = 0; i < *amount_of_words; i++) {
- sorted_words[i] = new char[max_word_len];
- int j = 0;
- while (words[i][j] != '\0') {
- sorted_words[i][j] = words[i][j];
- j++;
- }
- sorted_words[i][j] = words[i][j];
- }
- for (int i = 0; i < *amount_of_words - 1; i++) {
- for (int j = 0; j < *amount_of_words - i - 1; j++) {
- if (need_to_change(sorted_words[j], sorted_words[j + 1])) {
- int max_len;
- char* buffer;
- if (strlen(sorted_words[j]) > strlen(sorted_words[j + 1])) {
- max_len = strlen(sorted_words[j]) + 1;
- }
- else {
- max_len = strlen(sorted_words[j + 1]) + 1;
- }
- buffer = new char[max_len];
- for (int k = 0; k < max_len; k++) {
- buffer[k] = sorted_words[j][k];
- sorted_words[j][k] = sorted_words[j + 1][k];
- sorted_words[j + 1][k] = buffer[k];
- }
- delete[] buffer;
- }
- }
- }
- return sorted_words;
- }
- bool need_to_change(char* word1, char* word2) {
- int i = 0;
- while ((word1[i] != '\0') && (word2[i] != '\0')) {
- if (word1[i] > word2[i]) {
- return true;
- }
- i++;
- }
- if (strlen(word1) > strlen(word2)) {
- return true;
- }
- else {
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement