Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <filesystem>
- #include <sstream>
- #include <string>
- #include <set>
- #include <iomanip>
- #include <windows.h>
- using namespace std;
- string getSentenceFromConsole(string& sentence)
- {
- cout << "Введите предложение" << endl;
- cin.ignore();
- getline(cin, sentence);
- return sentence;
- }
- string getFilePath()
- {
- string path;
- bool isIncorrect;
- do
- {
- cout << "Введите абсолютный путь к файлу: " << endl;
- cin >> path;
- isIncorrect = false;
- if (!std::filesystem::exists(path))
- {
- cout << "Файл не найден. Проверьте введённый путь." << endl;
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- string getSentencefromFile(string& sentence, string path)
- {
- path = getFilePath();
- fstream SenFile(path, fstream::in);
- getline(SenFile, sentence);
- cout << endl;
- return sentence;
- }
- int getInputType(int& value)
- {
- bool IsNotCorrect;
- string inputLine;
- cout << "Выберите способ ввода предложения: 1 - файл, 2 - консоль. " << endl;
- do
- {
- IsNotCorrect = false;
- try
- {
- cin >> inputLine;
- value = stoi(inputLine);
- }
- catch (...)
- {
- IsNotCorrect = true;
- cout << "Введите одно из предложенных чисел" << endl;
- }
- if ((((value < 1) || (value > 2))) && !IsNotCorrect)
- {
- cout << "Выберете одно из двух значений! 1 для ввода из файла, или 2 для ввода консоли!" << endl;
- IsNotCorrect = true;
- }
- } while (IsNotCorrect);
- return value;
- }
- string getChoiceType(int value, string sentence, string path)
- {
- value = getInputType(value);
- if (value == 1)
- {
- sentence = getSentencefromFile(sentence, path);
- }
- if (value == 2)
- {
- sentence = getSentenceFromConsole(sentence);
- }
- return sentence;
- }
- void printSentenceBeforeEditing(string sentence)
- {
- cout << "Исходное предложение: " << endl;
- cout << sentence << endl;
- cout << endl;
- }
- string editing(string sentence)
- {
- set<char> symbols = { ',', '.', '?', '!', ';', ':' };
- set<char> ::iterator it;
- int pos;
- while (sentence[0] == ' '){
- sentence.erase(0, 1);
- }
- while ((pos = sentence.find(" ")) > 0) {
- sentence.erase(pos, 1);
- }
- for (int i = 0; i < sentence.length(); i++) {
- for(auto it : symbols) {
- if ((sentence[i] == it) && (sentence[i - 1] == ' ')) {
- sentence.erase(i - 1, 1);
- }
- }
- }
- return sentence;
- }
- void printSentenceAfterEditing(string sentence)
- {
- cout << "Исправленное предложение:" << endl;
- cout << sentence << endl;
- }
- string Output()
- {
- string patho;
- bool isIncorrect;
- isIncorrect = true;
- do
- {
- cout << "Введите директорию, в которую хотите сохранить ответ:" << endl;
- cin >> patho;
- if (filesystem::exists(patho))
- {
- isIncorrect = false;
- }
- else
- {
- cout << "Такой директории не существует. Попробуйте ещё раз." << endl;
- }
- } while (isIncorrect);
- return patho;
- }
- void saveResultToFile(string sentence)
- {
- string path;
- path = Output();
- ofstream fout;
- fout.open(path + "\output.txt");
- fout << sentence << endl;
- fout.close();
- cout << "Результат сохранён по указанному пути.";
- }
- int main()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- setlocale(LC_ALL, "ru");
- string sentence;
- string path;
- int value;
- value = 0;
- sentence = getChoiceType(value, sentence, path);
- printSentenceBeforeEditing(sentence);
- sentence = editing(sentence);
- printSentenceAfterEditing(sentence);
- saveResultToFile(sentence);
- }
Advertisement
Add Comment
Please, Sign In to add comment