-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrackets.cpp
More file actions
33 lines (33 loc) · 925 Bytes
/
brackets.cpp
File metadata and controls
33 lines (33 loc) · 925 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream.h>
#include<stack.h>
using namespace std;
bool checkParentheses(string expr){
stack<char> brackets;
for(int i =0; i < expr.length();i++){
if(brackets.empty()){
brackets.push(expr[i]);
}
else if(brackets.top() == '{' && expr[i] == '}'
|| brackets.top() == '(' && expr[i] == ')'
|| brackets.top() == '[' && expr[i] == ']'){
brackets.pop();
}else{
brackets.push(expr[i]);
}
}
if(brackets.empty()){
return true;
}
return false;
}
int main(){
string expression;
cout<<"Enter your desired expression"
cin>>expression;
if(checkParentheses(expression)){
cout<<"Balanced";
}else{
cout<<"Not balanced!";
}
return 0;
}