Advertisement
believe_me

Untitled

Nov 24th, 2021
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.55 KB | None | 0 0
  1. #include <fstream>
  2. #include <string>
  3. #include <iostream>
  4. #include <set>
  5.  
  6. using namespace std;
  7.  
  8. bool checkPermissionForWriting(const string& PathToFile) {
  9.     bool Flag;
  10.     ofstream file_out(PathToFile);
  11.     if (file_out.is_open()) {
  12.         file_out.close();
  13.         Flag = true;
  14.     }
  15.     else {
  16.         std::cout << "File isn't available for writing.\n";
  17.         Flag = false;
  18.     }
  19.     return Flag;
  20. }
  21.  
  22. int inputNumber(const int MIN_NUMBER, const int MAX_NUMBER) {
  23.     bool isIncorrect;
  24.     int number;
  25.     string input = "";
  26.     do {
  27.         getline(cin, input);
  28.         isIncorrect = false;
  29.         try {
  30.             number = stoi(input);
  31.         }
  32.         catch (invalid_argument ex) {
  33.             cout << "You need to write a number.\n";
  34.             isIncorrect = true;
  35.         }
  36.         catch (out_of_range ex) {
  37.             cout << "Number mustn't be less than " << MIN_NUMBER << " and more than"
  38.                 << MAX_NUMBER << "\n";
  39.             isIncorrect = true;
  40.         }
  41.         if (!isIncorrect && (number < MIN_NUMBER || number > MAX_NUMBER)) {
  42.             cout << "Number mustn't be less than " << MIN_NUMBER << " and more than "
  43.                 << MAX_NUMBER << "\n";
  44.             isIncorrect = true;
  45.         }
  46.     } while (isIncorrect);
  47.     return number;
  48. }
  49.  
  50. int chooseWayOfInputOrOuput() {
  51.     const int CONSOLE_INPUT = 1;
  52.     const int FILE_INPUT = 2;
  53.     int UserWay;
  54.     do {
  55.         UserWay = inputNumber(1, 2);
  56.     } while (UserWay != CONSOLE_INPUT && UserWay != FILE_INPUT);
  57.     return UserWay;
  58. }
  59.  
  60. const string receiveTextFromFile(const string Path)
  61. {
  62.     string Text;
  63.     ifstream InputFile(Path);
  64.     getline(InputFile, Text);
  65.     InputFile.close();
  66.     return Text;
  67. }
  68.  
  69. string receiveTextFromConsole() {
  70.     cout << "Input string:\n";
  71.     string Text;
  72.     getline(cin, Text);
  73.     return Text;
  74. }
  75.  
  76. void printResultInConsole(const set<char>& ResultSet) {
  77.     cout << "Result set of chars:\n";
  78.     for (set<char> ::iterator it = ResultSet.begin(); it != ResultSet.end(); it++)
  79.         cout << *it << " ";
  80.     cout << "\n";
  81. }
  82.  
  83. bool checkPermissionForReading(const string& PathToFile) {
  84.     bool Flag;
  85.     ifstream file_out(PathToFile);
  86.     if (file_out.is_open()) {
  87.         file_out.close();
  88.         Flag = true;
  89.     }
  90.     else {
  91.         std::cout << "File isn't available for reading.\n";
  92.         Flag = false;
  93.     }
  94.     return Flag;
  95. }
  96.  
  97. bool checkExtension(const string& PathToFile) {
  98.     const string extension = "txt";
  99.     bool Flag;
  100.     if (PathToFile.substr(PathToFile.length() - 3) == extension) {
  101.         Flag = true;
  102.     }
  103.     else {
  104.         cout << "Wrong extension.\n";
  105.         Flag = false;
  106.     }
  107.     return Flag;
  108. }
  109.  
  110. const string inputPathToFileForReading() {
  111.     string PathToFile;
  112.     do {
  113.         cout << "Input path to file for reading: \n";
  114.         getline(cin, PathToFile);
  115.     } while (!checkExtension(PathToFile) || !checkPermissionForReading(PathToFile));
  116.     return PathToFile;
  117. }
  118.  
  119. bool checkReceivedText(string Text, const set<char>& SetOfRequiredChars) {
  120.     bool IsStringCorrect = false;
  121.     int EndNumber = Text.length();
  122.     for (int i = 0; (i < EndNumber && !IsStringCorrect); i++)
  123.         if (SetOfRequiredChars.count(Text[i]) == 1)
  124.             IsStringCorrect = true;
  125.     return IsStringCorrect;
  126. }
  127.  
  128. string receiveText(const set<char> &SetOfRequiredChars) {
  129.     string Path;
  130.     string Text;
  131.     int UserWay;
  132.     bool IsIncorrect;
  133.     IsIncorrect = false;
  134.     do {
  135.         cout << "Choose way of input: \nType '1' if you want to receive string from console.\nType '2' if you want to receieve string from file.\n";
  136.         UserWay = chooseWayOfInputOrOuput();
  137.         switch (UserWay) {
  138.         case 1:
  139.         {
  140.             Text = receiveTextFromConsole();
  141.             break;
  142.         }
  143.         case 2:
  144.         {
  145.             do {
  146.                 Path = inputPathToFileForReading();
  147.                 IsIncorrect = checkPermissionForReading(Path);
  148.             } while (!IsIncorrect);
  149.             Text = receiveTextFromFile(Path);
  150.             break;
  151.         }
  152.         }
  153.         IsIncorrect = checkReceivedText(Text, SetOfRequiredChars);
  154.         if (!IsIncorrect)
  155.             cout << "There are not any required chars in text.\n";
  156.     } while (!IsIncorrect);
  157.     return Text;
  158. }
  159.  
  160. set<char> receiveResultSet(const string& Text, const set<char>& SetOfRequiredChars) {
  161.     set<char> ResultSet;
  162.     int EndNumber = Text.length();
  163.     for (int i = 0; i < EndNumber; i++)
  164.         if (SetOfRequiredChars.count(Text[i]) == 1)
  165.             ResultSet.insert(Text[i]);
  166.     return ResultSet;
  167. }
  168.  
  169. void printResultInFile(const string &PathToFile, set<char> ResultSet)
  170. {
  171.     ofstream fout(PathToFile, ios::trunc);
  172.     fout << "Result set of chars:\n";
  173.     for (set<char> ::iterator it = ResultSet.begin(); it != ResultSet.end(); it++)
  174.         fout << *it << " ";
  175.     fout.close();
  176. }
  177.  
  178. const string inputPathToFileForWriting() {
  179.     string PathToFile;
  180.     do {
  181.         cout << "Input path to file for writing:  \n";
  182.         getline(cin, PathToFile);
  183.     } while (!checkExtension(PathToFile) || !checkPermissionForWriting(PathToFile));
  184.     return PathToFile;
  185. }
  186.  
  187. void printResult(const set<char> ResultSet) {
  188.     cout << "Choose way of output: \nType '1' if you want to print result in console.\nType '2' if you want to print result in file.\n";
  189.     string PathToFile;
  190.     int UserWay;
  191.     UserWay = chooseWayOfInputOrOuput();
  192.     bool IsIncorrect;
  193.     IsIncorrect = false;
  194.     switch (UserWay) {
  195.     case 1:
  196.     {
  197.         printResultInConsole(ResultSet);
  198.         break;
  199.     }
  200.     case 2:
  201.     {
  202.         do {
  203.             PathToFile = inputPathToFileForWriting();
  204.             IsIncorrect = checkPermissionForWriting(PathToFile);
  205.         } while (!IsIncorrect);
  206.         printResultInFile(PathToFile, ResultSet);
  207.         break;
  208.     }
  209.     }
  210. }
  211.  
  212. int main() {
  213.     const set<char> SetOfRequiredChars{ '+' ,'-' ,'/', '*', '{' ,'}' ,'(' ,')', '[', ']', '}', '0', '2', '4', '6', '8'};
  214.     cout << "Program receives and prints even number, brackets and characters of arithmetic operations from the string you inserted\n";
  215.     string Text;
  216.     Text = receiveText(SetOfRequiredChars);
  217.     set<char> ResultSet = receiveResultSet(Text, SetOfRequiredChars);
  218.     printResult(ResultSet);
  219.     cout << "Program is completed.";
  220.     return 0;
  221. }
  222.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement