Перенос

 avatar
unknown
c_cpp
2 years ago
3.2 kB
4
Indexable
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
#include <algorithm>

using namespace std;

#include <iostream>
#include <string>
#include <vector>
#include <cctype>
#include <algorithm>

using namespace std;

bool isVowel(char c) {
    return (c == 'а' || c == 'е' || c == 'є' || c == 'и' || c == 'і' || c == 'ї' || c == 'о' || c == 'у' || c == 'ю' || c == 'я');
}

bool isConsonant(char c) {
    return !isVowel(c) && isalpha(c);
}

bool isTwoVowels(string s, int pos) {
    if (pos < 1 || pos >= s.length() - 1) {
        return false;
    }
    return isVowel(s[pos - 1]) && isVowel(s[pos]) && !isVowel(s[pos + 1]) && !isConsonant(s[pos + 1]);
}

bool isTwoConsonants(string s, int pos) {
    if (pos < 1 || pos >= s.length() - 1) {
        return false;
    }
    return isConsonant(s[pos - 1]) && isConsonant(s[pos]) && !isConsonant(s[pos + 1]);
}

int main() {
    string text;
    getline(cin, text);
    const int max_line_length = 80;

    vector<string> words;
    string current_word;
    for (int i = 0; i < text.length(); i++) {
        if (isspace(text[i])) {
            if (!current_word.empty()) {
                words.push_back(current_word);
                current_word.clear();
            }
        } else {
            current_word += text[i];
        }
    }
    if (!current_word.empty()) {
        words.push_back(current_word);
    }

    vector<string> lines;
    string current_line = words[0];
    for (int i = 1; i < words.size(); i++) {
        string word = words[i];
        if (current_line.length() + 1 + word.length() <= max_line_length) {
            current_line += " " + word;
        } else {
            // Начинаем новую строку, если текущее слово не помещается в строку
            if (word.length() <= max_line_length) {
                lines.push_back(current_line);
                current_line = word;
            } else {
                // Разбиваем слово на несколько частей и добавляем каждую на новую строку
                int pos = 0;
                while (pos < word.length()) {
                    string part = word.substr(pos, max_line_length - 1);
                    lines.push_back(current_line + "-" + part);
                    current_line = "";
                    pos += max_line_length - 1;
                }
            }
        }
    }
    // Добавляем последнюю строку
    lines.push_back(current_line);

    for (int i = 0; i < lines.size(); i++) {
        string line = lines[i];
        int pos = line.length() - 1;
        while (pos > 0 && pos < line.length() - 1) {
            if (isTwoVowels(line, pos)) {
                if (isConsonant(line[pos - 1])) {
                    line.insert(pos, "-\n");
                    break;
                }
            } else if (isTwoConsonants(line, pos)) {
                if (isVowel(line[pos + 1])) {
                    line.insert(pos, "-\n");
                    break;
                }
            } else {
                pos--;
            }
        }
        cout << line << endl;
    }

    return 0;
}
Editor is loading...