Advertisement
Guest User

Untitled

a guest
May 21st, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <locale>
  4. #include <fstream>
  5. #include <vector>
  6. #include <locale>
  7. #include <exception>
  8. #include <ios>
  9.  
  10.  
  11. /*
  12.  Написать программу, которая для непрерывного текста расставляет переносы слов для
  13. заданного ограничения по длине строки. Текст вводится на русском.
  14.  */
  15.  
  16.  
  17. class FileOpenErr : public std::exception {
  18. /*
  19.  Класс-исключение для выбрасывание в случае ошибки открытия файла
  20.  */
  21. private:
  22.     std::string m_message;
  23. public:
  24.     explicit FileOpenErr(std::string message) : m_message(std::move(message)) {
  25.     }
  26.  
  27.     const char *what() const noexcept override { return m_message.c_str(); }
  28. };
  29.  
  30.  
  31. std::vector<std::string> &hyphenate(const std::string filename, int limit) {
  32.     /*
  33.      Функция hyphenate() форматирует текс из файла по указанному ограничению по длине строки
  34.      На вход принимает имя файла и ограничение про длине
  35.      Возвращает отфармитораванный текст в формате шаблона вектора
  36.      */
  37.     std::ifstream fin;
  38.     std::ofstream fout;
  39.     fin.open(filename, std::ios_base::in);
  40.     fout.open("formatted_text.txt", std::ios_base::out | std::ios_base::trunc);
  41.     std::vector<std::string> formatted_text;
  42.     std::string buff;
  43.     char *c_style_buff;
  44.     if (fin.is_open() and fout.is_open()) {
  45.         while (!fin.eof()) {
  46.             c_style_buff = new char[limit];
  47.             //  кладем текст в buff
  48.             fin.getline(c_style_buff, limit);
  49.             buff = c_style_buff;
  50.             fout << buff << std::endl;
  51.             formatted_text.push_back(buff + '\n');
  52.             // освобождаем buff
  53.             delete[] c_style_buff;
  54.             buff.clear();
  55.         }
  56.     } else {
  57.         fin.close();
  58.         fout.close();
  59.         throw FileOpenErr("Не удалось открыть файл!");
  60.     }
  61.     fin.close();
  62.     fout.close();
  63.     return formatted_text;
  64. }
  65.  
  66. int main_menu() {
  67.     std::cout << "Введите название файла с техтом для форматирования:" << std::endl;
  68.     std::string filename;
  69.     std::cin >> filename;
  70.     std::cout << "Введите ограничение по количеству символов для переноса строки:" << std::endl;
  71.     int length_limit;
  72.     std::cin >> length_limit;
  73.     std::cout << "Ваш отфармотированный текст будет храниться в файле с именем \"formatted_text.txt\""
  74.               << std::endl << "Внимание, при следующем запуске программы файл будет перезаписан!" << std::endl;
  75.     try {
  76.         std::vector<std::string> formatted_text = hyphenate(filename, length_limit);
  77.         std::cout << "Отформатированный текст: " << std::endl << "------------------------------------------"
  78.                   << std::endl;
  79.         for (const std::string& i : formatted_text) {
  80.             std::cout << i;
  81.         }
  82.         std::cout << std::endl << "------------------------------------------" << std::endl;
  83.         system("pause");
  84.     } catch (FileOpenErr &error) {
  85.         std::cerr << error.what() << std::endl;
  86.         system("pause");
  87.         return 1;
  88.     } catch (std::exception &error) {
  89.         std::cerr << "Неизвестная ошибка!" << std::endl;
  90.         system("pause");
  91.         return 1;
  92.     }
  93.     return 0;
  94. }
  95.  
  96. int main() {
  97.     setlocale(LC_ALL, "ru");
  98.     main_menu();
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement