MaksNew

Untitled

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