NIKOLAY_TETUS

Lab3_ex1

Jun 21st, 2022
836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <algorithm>
  5. #include <iterator>
  6.  
  7. using namespace std;
  8.  
  9. void fixStr(string &str)
  10. {
  11.     //Убираем лишние пробелы
  12.     string::iterator new_end = unique(str.begin(), str.end(),
  13.             [](char leftChar, char rightChar)
  14.              {
  15.                   return (leftChar == rightChar) && (leftChar == ' ');
  16.              });
  17.     str.erase(new_end, str.end());
  18.  
  19.     //Исправляем буквы после точек
  20.     for (size_t i = 0; i < str.size() - 1; i++)
  21.         if (str[i] == '.' && isalpha(str[i+1]))
  22.         {
  23.             str.insert(i+1, " ");
  24.             i++;
  25.         }
  26.  
  27.     //Убираем пробелы в дефисе
  28.     for (size_t i = 0; i < str.size() - 1; i++)
  29.     {
  30.         if (str[i] == ' ' && str[i+1] == '-')
  31.             str.erase(i, 1);
  32.  
  33.         if (str[i] == '-' && str[i+1] == ' ')
  34.             str.erase(i+1, 1);
  35.     }
  36.  
  37.     //Ставим пробелы в тире
  38.     for (size_t i = 0; i < str.size() - 2; i++)
  39.     {
  40.         if (isalpha(str[i]) && str[i+1] == '-' && str[i+2] == '-')
  41.         {
  42.             str.insert(i+1, " ");
  43.             i++;
  44.         }
  45.  
  46.         if (str[i] == '-' && str[i+1] == '-' && isalpha(str[i+2]))
  47.         {
  48.             str.insert(i+2, " ");
  49.             i++;
  50.         }
  51.     }
  52. }
  53.  
  54. int main(int argc, char* argv[])
  55. {
  56.     //Получаем имя файла
  57.     string fileName;
  58.     if (argc == 1)
  59.     {
  60.         cout << "Введите имя файла> ";
  61.         getline(cin, fileName);
  62.     }
  63.     else if (argc == 2)
  64.         fileName = argv[1];
  65.  
  66.     //Открываем файл
  67.     fstream newfile;
  68.     newfile.open(fileName, ios::in);
  69.  
  70.     if (!newfile.is_open())
  71.     {
  72.         cout << "Ошибка при открытии файла" << endl;
  73.         return 0;
  74.     }
  75.  
  76.     //Получаем текст из файла
  77.     string buf;
  78.     vector<string> fileContent;
  79.     while(getline(newfile, buf))
  80.         fileContent.push_back(buf);
  81.     newfile.close();
  82.  
  83.     //Исправляем и выводим на экран
  84.     for (size_t i = 0; i < fileContent.size(); i++)
  85.         fixStr(fileContent[i]);
  86.  
  87.     for (size_t i = 0; i < fileContent.size(); i++)
  88.         cout << fileContent[i] << endl;
  89.  
  90.     //Открываем тот же файл и кидаем туды исправленный текст
  91.     newfile.open(fileName, ios::out | ios::trunc);
  92.  
  93.     if (!newfile.is_open())
  94.     {
  95.         cout << "Ошибка при открытии файла для записи" << endl;
  96.         return 0;
  97.     }
  98.  
  99.     for (size_t i = 0; i < fileContent.size(); i++)
  100.         newfile << fileContent[i] << endl;
  101.  
  102.     //Profit
  103.     cout << "Файл успешно изменён" << endl;
  104.  
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment