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:
The code includes necessary libraries for input/output (
iostream
), stack (stack
), and strings (string
).The
isBalanced
function takes a stringexpr
as input and returns a boolean value indicating whether the expression is balanced or not.Inside the
isBalanced
function, a stack of characterss
is created using thestack
container from the<stack>
library. This stack will be used to store opening brackets encountered in the expression.The code then iterates over each character
bracket
in the input expression.If
bracket
is an opening bracket ((
,[
, or{
), it is pushed onto the stack using thepush
function.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 returnsfalse
.If the stack is not empty, the code retrieves the top element of the stack using the
top
function and stores it in the variabletop
. The top element represents the most recent opening bracket encountered.The top element is then popped from the stack using the
pop
function.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
.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 returnsfalse
.In the
main
function, the code prompts the user to enter an expression and stores it in the string variableexpr
.The
isBalanced
function is called with the input expressionexpr
, and the return value is checked.Depending on the return value, the code outputs either "The expression is balanced." or "The expression is not balanced."
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.