Advertisement
believe_me

Untitled

Nov 23rd, 2021
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.23 KB | None | 0 0
  1. #include <fstream>
  2. #include <string>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. bool checkPermissionForWriting(const string& PathToFile) {
  8.     bool Flag;
  9.     ofstream file_out(PathToFile);
  10.     if (file_out.is_open()) {
  11.         file_out.close();
  12.         Flag = true;
  13.     }
  14.     else {
  15.         std::cout << "Файл закрыт для записи!\n";
  16.         Flag = false;
  17.     }
  18.     return Flag;
  19. }
  20.  
  21. int findPlusOrMinus(const string Text) {
  22.     const char PLUS = '+';
  23.     const char MINUS = '-';
  24.     int Index = -1;
  25.     bool IsCorrect;
  26.     IsCorrect = true;
  27.     int Length = Text.length();
  28.     for (int i = 0; (i < Length && IsCorrect); i++) {
  29.         if (Text[i] == PLUS || Text[i] == MINUS) {
  30.             Index = i;
  31.             IsCorrect = false;
  32.         }
  33.     }
  34.     return Index;
  35. }
  36.  
  37. int inputNumber(const int MIN_NUMBER, const int MAX_NUMBER) {
  38.     bool isIncorrect;
  39.     int number;
  40.     string input = "";
  41.     do {
  42.         getline(cin, input);
  43.         isIncorrect = false;
  44.         try {
  45.             number = stoi(input);
  46.         }
  47.         catch (invalid_argument ex) {
  48.             cout << "Нужно ввести целое число.\n";
  49.             isIncorrect = true;
  50.         }
  51.         catch (out_of_range ex) {
  52.             cout << "Нужно ввести число, которое не меньше " << MIN_NUMBER << " и не больше "
  53.                 << MAX_NUMBER << "\n";
  54.             isIncorrect = true;
  55.         }
  56.         if (!isIncorrect && (number < MIN_NUMBER || number > MAX_NUMBER)) {
  57.             cout << "Нужно ввести число, которое не меньше " << MIN_NUMBER << " и не больше "
  58.                 << MAX_NUMBER << "\n";
  59.             isIncorrect = true;
  60.         }
  61.     } while (isIncorrect);
  62.     return number;
  63. }
  64.  
  65. int chooseWayOfInputOrOuput() {
  66.     const int CONSOLE_INPUT = 1;
  67.     const int FILE_INPUT = 2;
  68.     int UserWay;
  69.     do {
  70.         UserWay = inputNumber(1, 2);
  71.     } while (UserWay != CONSOLE_INPUT && UserWay != FILE_INPUT);
  72.     return UserWay;
  73. }
  74.  
  75. const string receiveTextFromFile(const string Path)
  76. {
  77.     string Text;
  78.     ifstream InputFile(Path);
  79.     getline(InputFile, Text);
  80.     InputFile.close();
  81.     return Text;
  82. }
  83.  
  84. string receiveTextFromConsole() {
  85.     cout << "Введите строку:\n";
  86.     string Text;
  87.     getline(cin, Text);
  88.     return Text;
  89. }
  90.  
  91. void printResultInConsole(const string ResultNumber){
  92.     cout << "Полученное число: " << ResultNumber;
  93. }
  94.  
  95. bool checkPermissionForReading(const string& PathToFile) {
  96.     bool Flag;
  97.     ifstream file_out(PathToFile);
  98.     if (file_out.is_open()) {
  99.         file_out.close();
  100.         Flag = true;
  101.     }
  102.     else {
  103.         std::cout << "Файл закрыт для чтения!\n";
  104.         Flag = false;
  105.     }
  106.     return Flag;
  107. }
  108.  
  109. bool checkExtension(const string& PathToFile) {
  110.     const string extension = "txt";
  111.     bool Flag;
  112.     if (PathToFile.substr(PathToFile.length() - 3) == extension) {
  113.         Flag = true;
  114.     }
  115.     else {
  116.         cout << "Неверное расширение!\n";
  117.         Flag = false;
  118.     }
  119.     return Flag;
  120. }
  121.  
  122. const string inputPathToFileForReading() {
  123.     string PathToFile;
  124.     do {
  125.         cout << "Введите путь к файлу для чтения: \n";
  126.         getline(cin, PathToFile);
  127.     } while (!checkExtension(PathToFile) || !checkPermissionForReading(PathToFile));
  128.     return PathToFile;
  129. }
  130.  
  131. bool checkReceivedText(string Text) {
  132.     bool IsCorrect = true;
  133.     int Length = Text.length();
  134.     int Index = findPlusOrMinus(Text);
  135.     bool IsStringCorrect;
  136.     if (Index == -1)
  137.         IsStringCorrect = false;
  138.     else {
  139.         IsStringCorrect = true;
  140.         for (Index; (Index < Length && IsCorrect); Index++) {
  141.             if (Text[Index] > 47 && Text[Index] < 58) {
  142.                 IsCorrect = false;
  143.                 IsStringCorrect = true;
  144.             }
  145.             else
  146.                 IsStringCorrect = false;
  147.         }
  148.     }
  149.     return IsStringCorrect;
  150. }
  151.  
  152. string receiveText()
  153. {
  154.     string Path;
  155.     string Text;
  156.     cout << "Выберите способ ввода: \nНажмите '1', если хотите ввести строку из консоли.\nНажмите '2', если хотите считать строку из файла.\n";
  157.     int UserWay;
  158.     UserWay = chooseWayOfInputOrOuput();
  159.     bool IsIncorrect;
  160.     IsIncorrect = false;
  161.     do {
  162.         switch (UserWay)
  163.         {
  164.         case 1:
  165.         {
  166.             Text = receiveTextFromConsole();
  167.             break;
  168.         }
  169.         case 2:
  170.         {
  171.             do {
  172.                 Path = inputPathToFileForReading();
  173.                 IsIncorrect = checkPermissionForReading(Path);
  174.             } while (!IsIncorrect);
  175.             Text = receiveTextFromFile(Path);
  176.             break;
  177.         }
  178.         }
  179.         IsIncorrect = checkReceivedText(Text);
  180.         if (!IsIncorrect)
  181.             cout << "В строке нет знака или следующей за ним цифры.\n";
  182.     } while (!IsIncorrect);
  183.     return Text;
  184. }
  185.  
  186. string receiveNumber(const string Text) {
  187.     string ResultNumber;
  188.     int Length;
  189.     Length = Text.length();
  190.     int Index;
  191.     Index = findPlusOrMinus(Text);
  192.     ResultNumber = Text[Index];
  193.     for (Index++; Index < Length; Index++) {
  194.         try {
  195.             stoi(Text.substr(Index, 1));
  196.             ResultNumber += Text.substr(Index, 1);
  197.         }
  198.         catch (invalid_argument) {}
  199.     }
  200.     return ResultNumber;
  201. }
  202.  
  203. void printResultInFile(string Path, string ResultNumber)
  204. {
  205.     ofstream fout(Path, ios::trunc);
  206.     fout << "Полученное число:" << ResultNumber;
  207.     fout.close();
  208. }
  209.  
  210. const string inputPathToFileForWriting() {
  211.     string PathToFile;
  212.     do {
  213.         cout << "Введите путь к файлу для записи: \n";
  214.         getline(cin, PathToFile);
  215.     } while (!checkExtension(PathToFile) || !checkPermissionForWriting(PathToFile));
  216.     return PathToFile;
  217. }
  218.  
  219. void printResult(string ResultNumber){
  220.     cout << "Выберите способ вывода: \nНажмите '1', если хотите вывести число в консоль.\nНажмите '2', если хотите записать строку в файл.\n";
  221.     string PathToFile;
  222.     int UserWay;
  223.     UserWay = chooseWayOfInputOrOuput();
  224.     bool IsIncorrect;
  225.     IsIncorrect = false;
  226.     switch (UserWay)
  227.     {
  228.     case 1:
  229.     {
  230.         printResultInConsole(ResultNumber);
  231.         break;
  232.     }
  233.     case 2:
  234.     {
  235.         do {
  236.             PathToFile = inputPathToFileForWriting();
  237.             IsIncorrect = checkPermissionForWriting(PathToFile);
  238.         } while (!IsIncorrect);
  239.         printResultInFile(PathToFile, ResultNumber);
  240.         break;
  241.     }
  242.     }
  243. }
  244.  
  245. int main(){
  246.     setlocale(LC_ALL, "Russian");
  247.     cout << "Программа выделяет из строки подстроку, которая соответствует целому числу, начинающемуся с ''+'' или ''-''.'\n";
  248.     string Text, ResultNumber;
  249.     Text = receiveText();
  250.     ResultNumber = receiveNumber(Text);
  251.     printResult(ResultNumber);
  252.     cout << "\nПрограмма завершена";
  253.     return 0;
  254. }
  255.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement