MaksNew

Untitled

Nov 7th, 2020
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <filesystem>
  4. #include <sstream>
  5. #include <set>
  6.  
  7. using namespace std;
  8.  
  9. string getFilePath()
  10. {
  11.     string path;
  12.     bool isIncorrect;
  13.     do
  14.     {
  15.         cout << "Введите абсолютный путь к файлу: " << endl;
  16.         cin >> path;
  17.         isIncorrect = false;
  18.         if (!std::filesystem::exists(path))
  19.         {
  20.             cout << "Файл не найден. Проверьте введённый путь." << endl;
  21.             isIncorrect = true;
  22.         }
  23.     } while (isIncorrect);
  24.     return path;
  25. }
  26.  
  27.  
  28. string fileToSentence(string& sentence, string path)
  29. {
  30.     fstream SenFile(path, fstream::in);
  31.     getline(SenFile, sentence);
  32.     cout << endl;
  33.     return sentence;
  34. }
  35.  
  36. void printSentenceBO(string sentence)
  37. {
  38.     cout << "Исходное предложение: " << endl;
  39.     cout << sentence << endl;
  40.     cout << endl;
  41. }
  42.  
  43. string getCondition(string sentence)
  44. {
  45.     set<char> symbols = { ',', '.', '?', '!', ';', ':' };
  46.     set<char> ::iterator it;
  47.     int pos;
  48.     while (sentence[0] == ' '){
  49.         sentence.erase(0, 1);
  50.     }
  51.     while ((pos = sentence.find("  ")) > 0) {
  52.         sentence.erase(pos, 1);
  53.     }
  54.     while (sentence.back() == ' ') {
  55.         sentence.erase(sentence.back(), 1);
  56.     }
  57.     for (int i = 0; i < sentence.length(); i++) {
  58.         for(auto it : symbols) {
  59.             if ((sentence[i] == it) && (sentence[i - 1] == ' ')) {
  60.                 sentence.erase(i - 1, 1);
  61.             }
  62.         }
  63.     }
  64.     return sentence;
  65. }
  66.  
  67. void printSentencePO(string sentence)
  68. {
  69.     cout << "Исправленное предложение:" << endl;
  70.     cout << sentence << endl;
  71. }
  72.  
  73. string getOutput()
  74. {
  75.     string patho;
  76.     bool isIncorrect;
  77.     isIncorrect = true;
  78.     do
  79.     {
  80.         cout << "Введите директорию, в которую хотите сохранить ответ:" << endl;
  81.         cin >> patho;
  82.         if (filesystem::exists(patho))
  83.         {
  84.             isIncorrect = false;
  85.         }
  86.         else
  87.         {
  88.             cout << "Такой директории не существует. Попробуйте ещё раз." << endl;
  89.         }
  90.  
  91.     } while (isIncorrect);
  92.     return patho;
  93. }
  94.  
  95. void getConditionToFile(string sentence)
  96. {
  97.     string path;
  98.     path = getOutput();
  99.     ofstream fout;
  100.     fout.open(path + "\output.txt");
  101.     fout << sentence << endl;
  102.     fout.close();
  103.     cout << "Результат сохранён по указанному пути.";
  104.  
  105. }
  106.  
  107.  
  108. int main()
  109. {
  110.     setlocale(LC_ALL, "ru");
  111.     string sentence;
  112.     string path;
  113.     path = getFilePath();
  114.     sentence = fileToSentence(sentence, path);
  115.     printSentenceBO(sentence);
  116.     sentence = getCondition(sentence);
  117.     printSentencePO(sentence);
  118.     getConditionToFile(sentence);
  119. }
  120.  
Advertisement
Add Comment
Please, Sign In to add comment