Advertisement
Alyks

Untitled

Dec 9th, 2019
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. bool isBalanced(string str) {
  8.     stack<char> charStack;
  9.     int i = 0;
  10.     bool continueCycle = true;
  11.     while(i < str.length() && continueCycle) {
  12.         char curr = str[i];
  13.         if(curr == '(')
  14.             charStack.push(curr);
  15.         else if(curr == ')') {
  16.             if(charStack.empty())
  17.                 continueCycle = false;
  18.             else
  19.                 charStack.pop();
  20.         }
  21.         i++;
  22.     }
  23.  
  24.     return continueCycle;
  25. }
  26.  
  27. bool isStringContainsParentheses(string str) {
  28.     return str.find('(') != -1 || str.find(')') != -1;
  29. }
  30.  
  31. string readFile(string filePath) {
  32.     string str;
  33.     ifstream inputFile;
  34.     inputFile.open(filePath);
  35.     if (inputFile && getline(inputFile, str))
  36.         inputFile.close();
  37.     else
  38.         cout << "Произошла ошибка при чтении файла. Убедитесь, что такой файл существует, либо проверьте имя файла."
  39.              << endl;
  40.     return str;
  41. }
  42.  
  43. void saveResult(string resultStr) {
  44.     string decision;
  45.     while (decision != "Y" && decision != "N") {
  46.         cout << "Сохранить результат в файл? (Y - сохранить, N - не сохранять)" << endl;
  47.         cin >> decision;
  48.     }
  49.  
  50.     if (decision == "Y") {
  51.         ofstream outputFile;
  52.         outputFile.open("output.txt");
  53.         outputFile << resultStr;
  54.         outputFile.close();
  55.         cout << "Результат сохранен в файл output.txt" << endl;
  56.     }
  57. }
  58.  
  59. int main() {
  60.     cout << "Данная программа проверяет баланс скобок в строке\n" << endl;
  61.     string inputType;
  62.     while (inputType != "C" && inputType != "F") {
  63.         cout << "Введите C, если хотите ввести строку из консоли, или F, если хотите ввести строку из файла" << endl;
  64.         cin >> inputType;
  65.     }
  66.     string resultStr, str;
  67.     bool result = false;
  68.     if (inputType == "C") {
  69.         cout << "Введите строку, из которой необходимо удалить повторяющиеся символы" << endl;
  70.         cin >> str;
  71.     } else if (inputType == "F") {
  72.         cout << "Введите путь к файлу" << endl;
  73.         string filePath;
  74.         cin >> filePath;
  75.         str = readFile(filePath);
  76.     }
  77.     result = isStringContainsParentheses(str);
  78.     if(result) {
  79.         result = isBalanced(str);
  80.         resultStr = result ? "Баланс скобок соблюдён" : "Баланс скобок не соблюдён";
  81.     }
  82.     else
  83.         resultStr = "В строке отсутсвуют скобки";
  84.     cout << resultStr << endl;
  85.     saveResult(resultStr);
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement