NickAndNick

Баланс скобок

Mar 19th, 2020
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stack>
  4. using namespace std;
  5. bool brackets(const string& line) {
  6.     stack<char> box;
  7.     const string left{ "([{" };
  8.     const string rigth{ ")]}" };
  9.     bool balance = true;
  10.     for (auto letter : line) {
  11.         if (rigth.find(letter) != string::npos) {
  12.             if (box.empty() || box.top() != letter) {
  13.                 balance = false;
  14.                 break;
  15.             } else box.pop();
  16.         }
  17.         auto pos = left.find(letter);
  18.         if (pos != string::npos) box.push(rigth.at(pos));
  19.     }
  20.     return balance;
  21. }
  22. bool brackets(const char* line) {
  23.     return brackets(string(line));
  24. }
  25. int main() {
  26.     cout << ">>> ";
  27.     string line;
  28.     getline(cin, line);
  29.     cout << (brackets(line) ? "YES" : "NO") << '\n';
  30.     system("pause");
  31. }
Add Comment
Please, Sign In to add comment