Advertisement
Vladislav8653

laba_3_4_c++

Dec 9th, 2022
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5. ifstream fin;
  6. ofstream fout;
  7.  
  8. const int SIZE = 6;
  9. const char brakes [SIZE] = { '[', ']', '{', '}', '(', ')' };
  10. const int SQUARE_BRACKET = 0;
  11. const int BRACE = 2;
  12. const int BRACKET = 4;
  13. const int* counters = new int[6];
  14.  
  15. bool choose() {
  16.     int inputNumber;
  17.     bool isIncorrect;
  18.     const int MIN_NUM = 0;
  19.     const int MAX_NUM = 1;
  20.     do {
  21.         cin >> inputNumber;
  22.         isIncorrect = false;
  23.         if (cin.fail()) {
  24.             isIncorrect = true;
  25.             cout << "Please, enter a number." << endl;
  26.             cin.clear();
  27.             while (cin.get() != '\n');
  28.         }
  29.         if (!isIncorrect && (inputNumber < MIN_NUM || inputNumber > MAX_NUM)) {
  30.             cout << "You are out of input range!" << endl;
  31.             isIncorrect = true;
  32.         }
  33.     } while (isIncorrect);
  34.     if (inputNumber == 1)
  35.         return true;
  36.     else
  37.         return false;
  38. }
  39.  
  40. // консольный ввод и вывод
  41.  
  42. string consoleInputString() {
  43.     string strInput;
  44.     bool isIncorrect;
  45.     do {
  46.         cin >> strInput;
  47.         isIncorrect = false;
  48.         if (cin.fail()) {
  49.             cout << "Please, enter a string." << endl;
  50.             isIncorrect = true;
  51.             cin.clear();
  52.             while (cin.get() != '\n');
  53.         }
  54.     } while (isIncorrect);
  55.     return strInput;
  56. }
  57.  
  58.  
  59. void countBrackets(int j, int i, char* brakes, int* counters, string str) {
  60.     if (str[i] == brakes[j])
  61.         counters[j]++;
  62.     if ((str[i] == brakes[j + 1]))
  63.         counters[j + 1]++;
  64. }
  65.  
  66. static void loop(char* brakes, int* counters, string str) {
  67.     for (int i = 0; i < str.length(); i++) {
  68.         countBrackets(SQUARE_BRACKET, i, brakes, counters, str);
  69.         if (counters[SQUARE_BRACKET + 1] > counters[SQUARE_BRACKET])
  70.             break;
  71.         countBrackets(BRACE, i, brakes, counters, str);
  72.         if (counters[BRACE + 1] > counters[BRACE])
  73.             break;
  74.         countBrackets(BRACKET, i, brakes, counters, str);
  75.         if (counters[BRACKET + 1] > counters[BRACKET])
  76.             break;
  77.     }
  78. }
  79.  
  80. static void check(int* counters) {
  81.     if ((counters[SQUARE_BRACKET] == counters[SQUARE_BRACKET + 1]) && (counters[BRACE] == counters[BRACE + 1]) && (counters[BRACKET] == counters[BRACKET + 1]))
  82.         cout << "Congratulations! Balance is exist!" << endl;
  83.     else
  84.        cout << "There is no balance there." << endl;
  85. }
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94. string inputFilePath() {
  95.     const int EXTENSION_SIZE = 4;
  96.     string path;
  97.     string ext;
  98.     bool isIncorrect;
  99.     do {
  100.         isIncorrect = false;
  101.         cout << "Input path to file: " << endl;
  102.         cin >> path;
  103.         fin.open(path);
  104.         if (path.size() > EXTENSION_SIZE) {
  105.             ext = path.substr(path.size() - EXTENSION_SIZE, EXTENSION_SIZE);
  106.         }
  107.         else {
  108.             cout << "Incorrect file name." << endl;
  109.             isIncorrect = true;
  110.         } if (!isIncorrect && ext != ".txt") {
  111.             cout << "Must have .txt!" << endl;
  112.             isIncorrect = true;
  113.         }
  114.         else if (!isIncorrect && !fin.is_open()) {
  115.             cout << "Wrong way to file." << endl;
  116.             isIncorrect = true;
  117.         }
  118.     } while (isIncorrect);
  119.     fin.close();
  120.     return path;
  121. }
  122.  
  123. string FileInputString(string path) {
  124.     string strInput;
  125.     bool isIncorrect;
  126.     do {
  127.         isIncorrect = false;
  128.         fin.open(path);
  129.         fin >> strInput;
  130.         if (cin.fail()) {
  131.             isIncorrect = true;
  132.             cout << "Check file" << endl;
  133.             path = inputFilePath();
  134.         }
  135.     } while (isIncorrect);
  136.     return strInput;
  137. }
  138.  
  139. void fileOutput(string path, int * counters) {
  140.     bool isIncorrect;
  141.     fout.open(path);
  142.     do {
  143.         isIncorrect = false;
  144.         try {
  145.             if ((counters[SQUARE_BRACKET] == counters[SQUARE_BRACKET + 1]) && (counters[BRACE] == counters[BRACE + 1]) && (counters[BRACKET] == counters[BRACKET + 1]))
  146.                 fout << "Congratulations!Balance is exist!" << endl;
  147.             else
  148.                 fout << "There is no balance there." << endl;
  149.         }
  150.         catch (exception e) {
  151.             isIncorrect = true;
  152.             fout << "Mistake of output in file. Input path." << endl;
  153.             path = inputFilePath();
  154.         }
  155.     } while (isIncorrect);
  156.     cout << "Successful output in file." << endl;
  157.     fout.close();
  158. }
  159.  
  160. int main() {
  161.     cout << "Check if the given text has a balance of opening and closing brackets." << endl;
  162.     cout << "Enter type of input." << endl << "1 is console input, 0 is file input." << endl;
  163.     bool chose = choose();
  164.     string strInput;
  165.     int* counters = new int[6];
  166.     char brakes[SIZE] = { '[', ']', '{', '}', '(', ')' };
  167.     string path;
  168.     if (chose) {
  169.         cout << "Input string: " << endl;;
  170.         strInput = consoleInputString();
  171.     }
  172.     else {
  173.         path = inputFilePath();
  174.         strInput = FileInputString(path);
  175.     }
  176.     loop(brakes, counters, strInput);
  177.  
  178.  
  179.  
  180.     cout << "Enter type of input." << endl << "1 is console output, 0 is file output." << endl;
  181.     chose = choose();
  182.     if (chose) {
  183.         check(counters);
  184.     }
  185.     else {
  186.         path = inputFilePath();
  187.         fileOutput(path, counters);
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement