MaksNew

Untitled

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