MaksNew

Untitled

Nov 10th, 2020
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.30 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.     int pos;
  99.     int i;
  100.     i = 1;
  101.     while (i <= sentence.length())
  102.     {
  103.         if (!('0' <= sentence[i] <= '9' || 'A' <= sentence[i] <='Z' || 'a' <= sentence[i] <= 'z' || sentence[i] == ' '))
  104.         {
  105.             sentence.erase(i-1, 1);
  106.             i--;
  107.         }
  108.         i++;
  109.     }
  110.     return sentence;
  111. }
  112.  
  113. void printSentenceAfterEditing(string sentence)
  114. {
  115.     if (sentence.length() < 1)
  116.     {
  117.         sentence = "Ни один символ не прошёл проверку! От вашей строки ничего не осталось!";
  118.     }
  119.     else
  120.     {
  121.         cout << "Исправленное предложение:" << endl;
  122.     }
  123.     cout << sentence << endl;
  124. }
  125.  
  126. string Output()
  127. {
  128.     string patho;
  129.     bool isIncorrect;
  130.     isIncorrect = true;
  131.     do
  132.     {
  133.         cout << "Введите директорию, в которую хотите сохранить ответ:" << endl;
  134.         cin >> patho;
  135.         if (filesystem::exists(patho))
  136.         {
  137.             isIncorrect = false;
  138.         }
  139.         else
  140.         {
  141.             cout << "Такой директории не существует. Попробуйте ещё раз." << endl;
  142.         }
  143.  
  144.     } while (isIncorrect);
  145.     return patho;
  146. }
  147.  
  148. void saveResultToFile(string sentence)
  149. {
  150.     string path;
  151.     path = Output();
  152.     ofstream fout;
  153.     fout.open(path + "\output.txt");
  154.     fout << sentence << endl;
  155.     fout.close();
  156.     cout << "Результат сохранён по указанному пути.";
  157.  
  158. }
  159.  
  160.  
  161. int main()
  162. {
  163.     SetConsoleCP(1251);
  164.     SetConsoleOutputCP(1251);
  165.     setlocale(LC_ALL, "ru");
  166.     string sentence;
  167.     string path;
  168.     int value;
  169.     value = 0;
  170.     sentence = getChoiceType(value, sentence, path);
  171.     printSentenceBeforeEditing(sentence);
  172.     sentence = editing(sentence);
  173.     printSentenceAfterEditing(sentence);
  174.     saveResultToFile(sentence);
  175. }
  176.  
Advertisement
Add Comment
Please, Sign In to add comment