Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <fstream>
- #include <algorithm>
- #include <iterator>
- using namespace std;
- void fixStr(string &str)
- {
- //Убираем лишние пробелы
- string::iterator new_end = unique(str.begin(), str.end(),
- [](char leftChar, char rightChar)
- {
- return (leftChar == rightChar) && (leftChar == ' ');
- });
- str.erase(new_end, str.end());
- //Исправляем буквы после точек
- for (size_t i = 0; i < str.size() - 1; i++)
- if (str[i] == '.' && isalpha(str[i+1]))
- {
- str.insert(i+1, " ");
- i++;
- }
- //Убираем пробелы в дефисе
- for (size_t i = 0; i < str.size() - 1; i++)
- {
- if (str[i] == ' ' && str[i+1] == '-')
- str.erase(i, 1);
- if (str[i] == '-' && str[i+1] == ' ')
- str.erase(i+1, 1);
- }
- //Ставим пробелы в тире
- for (size_t i = 0; i < str.size() - 2; i++)
- {
- if (isalpha(str[i]) && str[i+1] == '-' && str[i+2] == '-')
- {
- str.insert(i+1, " ");
- i++;
- }
- if (str[i] == '-' && str[i+1] == '-' && isalpha(str[i+2]))
- {
- str.insert(i+2, " ");
- i++;
- }
- }
- }
- int main(int argc, char* argv[])
- {
- //Получаем имя файла
- string fileName;
- if (argc == 1)
- {
- cout << "Введите имя файла> ";
- getline(cin, fileName);
- }
- else if (argc == 2)
- fileName = argv[1];
- //Открываем файл
- fstream newfile;
- newfile.open(fileName, ios::in);
- if (!newfile.is_open())
- {
- cout << "Ошибка при открытии файла" << endl;
- return 0;
- }
- //Получаем текст из файла
- string buf;
- vector<string> fileContent;
- while(getline(newfile, buf))
- fileContent.push_back(buf);
- newfile.close();
- //Исправляем и выводим на экран
- for (size_t i = 0; i < fileContent.size(); i++)
- fixStr(fileContent[i]);
- for (size_t i = 0; i < fileContent.size(); i++)
- cout << fileContent[i] << endl;
- //Открываем тот же файл и кидаем туды исправленный текст
- newfile.open(fileName, ios::out | ios::trunc);
- if (!newfile.is_open())
- {
- cout << "Ошибка при открытии файла для записи" << endl;
- return 0;
- }
- for (size_t i = 0; i < fileContent.size(); i++)
- newfile << fileContent[i] << endl;
- //Profit
- cout << "Файл успешно изменён" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment