Advertisement
Spocoman

4. Matching Brackets

Jan 10th, 2024
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stack>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.     string expression;
  9.     getline(cin, expression);
  10.  
  11.     stack<int> openBracketIndexes;
  12.    
  13.     for (size_t i = 0; i < expression.length(); i++) {
  14.         if (expression[i] == '(') {
  15.             openBracketIndexes.push(i);
  16.         }
  17.         else  if (expression[i] == ')') {
  18.             cout << expression.substr(openBracketIndexes.top(), i - openBracketIndexes.top() + 1) << endl;
  19.             openBracketIndexes.pop();
  20.         }
  21.     }
  22.  
  23.     return 0;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement