Parenthesis Checker using stack in c++

#include <iostream>
#include <stack>
#include <string>

bool isBalanced(const std::string& expr) {
    std::stack<char> s;
    for (char bracket : expr) {
        if (bracket == '(' || bracket == '[' || bracket == '{') {
            s.push(bracket);
        } else if (bracket == ')' || bracket == ']' || bracket == '}') {
            if (s.empty()) {
                return false;
            }
            char top = s.top();
            s.pop();
            if ((bracket == ')' && top != '(') ||
                (bracket == ']' && top != '[') ||
                (bracket == '}' && top != '{')) {
                return false;
            }
        }
    }
    return s.empty();
}

int main() {
    std::string expr;
    std::cout << "Enter an expression: ";
    std::cin >> expr;

    if (isBalanced(expr)) {
        std::cout << "The expression is balanced." << std::endl;
    } else {
        std::cout << "The expression is not balanced." << std::endl;
    }

    return 0;
}

Explanation:

The code above is an implementation of a Parenthesis Checker using a stack in C++. Here's a step-by-step explanation of how it works:

  1. The code includes necessary libraries for input/output (iostream), stack (stack), and strings (string).

  2. The isBalanced function takes a string expr as input and returns a boolean value indicating whether the expression is balanced or not.

  3. Inside the isBalanced function, a stack of characters s is created using the stack container from the <stack> library. This stack will be used to store opening brackets encountered in the expression.

  4. The code then iterates over each character bracket in the input expression.

  5. If bracket is an opening bracket ((, [, or {), it is pushed onto the stack using the push function.

  6. If bracket is a closing bracket (), ], or }), the code checks if the stack is empty. If it is, it means there is no corresponding opening bracket for the current closing bracket, and the function returns false.

  7. If the stack is not empty, the code retrieves the top element of the stack using the top function and stores it in the variable top. The top element represents the most recent opening bracket encountered.

  8. The top element is then popped from the stack using the pop function.

  9. The code then checks if the current closing bracket matches the top element. If they do not match, it means the expression is not balanced, and the function returns false.

  10. After iterating over all the characters in the expression, the code checks if the stack is empty. If it is, it means all the opening brackets have been matched with their corresponding closing brackets, and the function returns true. Otherwise, it returns false.

  11. In the main function, the code prompts the user to enter an expression and stores it in the string variable expr.

  12. The isBalanced function is called with the input expression expr, and the return value is checked.

  13. Depending on the return value, the code outputs either "The expression is balanced." or "The expression is not balanced."

  14. Finally, the main function returns 0, indicating successful execution of the program.

This code uses a stack to keep track of the opening brackets encountered. When a closing bracket is encountered, it checks if the corresponding opening bracket is at the top of the stack. If not, the expression is not balanced. If the stack is empty at the end of the expression, it means all the opening brackets have been matched, and the expression is balanced.