MaksNew

Untitled

Nov 8th, 2020
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <filesystem>
  4. #include <sstream>
  5. #include <string>
  6. #include <set>
  7. #include <iomanip>
  8. #include <windows.h>
  9.  
  10.  
  11. using namespace std;
  12.  
  13. string getSentenceFromConsole(string& sentence)
  14. {
  15.     cout << "Введите предложение" << endl;
  16.     cin.ignore();
  17.     getline(cin, sentence);
  18.     return sentence;
  19. }
  20.  
  21. string getFilePath()
  22. {
  23.     string path;
  24.     bool isIncorrect;
  25.     do
  26.     {
  27.         cout << "Введите абсолютный путь к файлу: " << endl;
  28.         cin >> path;
  29.         isIncorrect = false;
  30.         if (!std::filesystem::exists(path))
  31.         {
  32.             cout << "Файл не найден. Проверьте введённый путь." << endl;
  33.             isIncorrect = true;
  34.         }
  35.     } while (isIncorrect);
  36.     return path;
  37. }
  38.  
  39. string getSentencefromFile(string& sentence, string path)
  40. {
  41.     path = getFilePath();
  42.     fstream SenFile(path, fstream::in);
  43.     getline(SenFile, sentence);
  44.     cout << endl;
  45.     return sentence;
  46. }
  47.  
  48. int getInputType(int& value)
  49. {
  50.     bool IsNotCorrect;
  51.     string inputLine;
  52.     cout << "Выберите способ ввода предложения: 1 - файл, 2 - консоль. " << endl;
  53.     do
  54.     {
  55.         IsNotCorrect = false;
  56.         try
  57.         {
  58.             cin >> inputLine;
  59.             value = stoi(inputLine);
  60.         }
  61.         catch (...)
  62.         {
  63.             IsNotCorrect = true;
  64.             cout << "Введите одно из предложенных чисел" << endl;
  65.         }
  66.         if ((((value < 1) || (value > 2))) && !IsNotCorrect)
  67.         {
  68.             cout << "Выберете одно из двух значений! 1 для ввода из файла, или 2 для ввода консоли!" << endl;
  69.             IsNotCorrect = true;
  70.         }
  71.     } while (IsNotCorrect);
  72.     return value;
  73. }
  74.  
  75. string getChoiceType(int value, string sentence, string path)
  76. {
  77.     value = getInputType(value);
  78.     if (value == 1)
  79.     {
  80.         sentence = getSentencefromFile(sentence, path);
  81.     }
  82.     if (value == 2)
  83.     {
  84.         sentence = getSentenceFromConsole(sentence);
  85.     }
  86.     return sentence;
  87. }
  88.  
  89. void printSentenceBeforeEditing(string sentence)
  90. {
  91.     cout << "Исходное предложение: " << endl;
  92.     cout << sentence << endl;
  93.     cout << endl;
  94. }
  95.  
  96. string editing(string sentence)
  97. {
  98.     set<char> symbols = { ',', '.', '?', '!', ';', ':' };
  99.     set<char> ::iterator it;
  100.     int pos;
  101.     while (sentence[0] == ' '){
  102.         sentence.erase(0, 1);
  103.     }
  104.     while ((pos = sentence.find("  ")) > 0) {
  105.         sentence.erase(pos, 1);
  106.     }
  107.  
  108.     for (int i = 0; i < sentence.length(); i++) {
  109.         for(auto it : symbols) {
  110.             if ((sentence[i] == it) && (sentence[i - 1] == ' ')) {
  111.                 sentence.erase(i - 1, 1);
  112.             }
  113.         }
  114.     }
  115.     return sentence;
  116. }
  117.  
  118. void printSentenceAfterEditing(string sentence)
  119. {
  120.     cout << "Исправленное предложение:" << endl;
  121.     cout << sentence << endl;
  122. }
  123.  
  124. string Output()
  125. {
  126.     string patho;
  127.     bool isIncorrect;
  128.     isIncorrect = true;
  129.     do
  130.     {
  131.         cout << "Введите директорию, в которую хотите сохранить ответ:" << endl;
  132.         cin >> patho;
  133.         if (filesystem::exists(patho))
  134.         {
  135.             isIncorrect = false;
  136.         }
  137.         else
  138.         {
  139.             cout << "Такой директории не существует. Попробуйте ещё раз." << endl;
  140.         }
  141.  
  142.     } while (isIncorrect);
  143.     return patho;
  144. }
  145.  
  146. void saveResultToFile(string sentence)
  147. {
  148.     string path;
  149.     path = Output();
  150.     ofstream fout;
  151.     fout.open(path + "\output.txt");
  152.     fout << sentence << endl;
  153.     fout.close();
  154.     cout << "Результат сохранён по указанному пути.";
  155.  
  156. }
  157.  
  158.  
  159. int main()
  160. {
  161.     SetConsoleCP(1251);
  162.     SetConsoleOutputCP(1251);
  163.     setlocale(LC_ALL, "ru");
  164.     string sentence;
  165.     string path;
  166.     int value;
  167.     value = 0;
  168.     sentence = getChoiceType(value, sentence, path);
  169.     printSentenceBeforeEditing(sentence);
  170.     sentence = editing(sentence);
  171.     printSentenceAfterEditing(sentence);
  172.     saveResultToFile(sentence);
  173. }
  174.  
Advertisement
Add Comment
Please, Sign In to add comment