Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <locale>
- #include <fstream>
- #include <vector>
- #include <locale>
- #include <exception>
- #include <ios>
- /*
- Написать программу, которая для непрерывного текста расставляет переносы слов для
- заданного ограничения по длине строки. Текст вводится на русском.
- */
- class FileOpenErr : public std::exception {
- /*
- Класс-исключение для выбрасывание в случае ошибки открытия файла
- */
- private:
- std::string m_message;
- public:
- explicit FileOpenErr(std::string message) : m_message(std::move(message)) {
- }
- const char *what() const noexcept override { return m_message.c_str(); }
- };
- std::vector<std::string> &hyphenate(const std::string filename, int limit) {
- /*
- Функция hyphenate() форматирует текс из файла по указанному ограничению по длине строки
- На вход принимает имя файла и ограничение про длине
- Возвращает отфармитораванный текст в формате шаблона вектора
- */
- std::ifstream fin;
- std::ofstream fout;
- fin.open(filename, std::ios_base::in);
- fout.open("formatted_text.txt", std::ios_base::out | std::ios_base::trunc);
- std::vector<std::string> formatted_text;
- std::string buff;
- char *c_style_buff;
- if (fin.is_open() and fout.is_open()) {
- while (!fin.eof()) {
- c_style_buff = new char[limit];
- // кладем текст в buff
- fin.getline(c_style_buff, limit);
- buff = c_style_buff;
- fout << buff << std::endl;
- formatted_text.push_back(buff + '\n');
- // освобождаем buff
- delete[] c_style_buff;
- buff.clear();
- }
- } else {
- fin.close();
- fout.close();
- throw FileOpenErr("Не удалось открыть файл!");
- }
- fin.close();
- fout.close();
- return formatted_text;
- }
- int main_menu() {
- std::cout << "Введите название файла с техтом для форматирования:" << std::endl;
- std::string filename;
- std::cin >> filename;
- std::cout << "Введите ограничение по количеству символов для переноса строки:" << std::endl;
- int length_limit;
- std::cin >> length_limit;
- std::cout << "Ваш отфармотированный текст будет храниться в файле с именем \"formatted_text.txt\""
- << std::endl << "Внимание, при следующем запуске программы файл будет перезаписан!" << std::endl;
- try {
- std::vector<std::string> formatted_text = hyphenate(filename, length_limit);
- std::cout << "Отформатированный текст: " << std::endl << "------------------------------------------"
- << std::endl;
- for (const std::string& i : formatted_text) {
- std::cout << i;
- }
- std::cout << std::endl << "------------------------------------------" << std::endl;
- system("pause");
- } catch (FileOpenErr &error) {
- std::cerr << error.what() << std::endl;
- system("pause");
- return 1;
- } catch (std::exception &error) {
- std::cerr << "Неизвестная ошибка!" << std::endl;
- system("pause");
- return 1;
- }
- return 0;
- }
- int main() {
- setlocale(LC_ALL, "ru");
- main_menu();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement