forked from Git-Commit-Show/100-Days-of-Code
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
64 lines (59 loc) · 1.13 KB
/
main.cpp
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
using namespace std;
struct Node{
char data;
Node *next;
}*top=NULL;
void push(char x){
Node *t=new Node;
if(t==NULL){
cout<<"Stack is full"<<endl;
return;
}else{
t->data=x;
t->next=top;
top=t;
}
}
char pop(){
char x=-1;
if(top==NULL){
cout<<"Stack is empty"<<endl;
return x;
}else{
Node *p;
p=top;
top=top->next;
x=p->data;
delete p;
}
return x;
}
int isBalance(char *exp){
for(int i=0;exp[i]!='\0';i++){
if(exp[i]=='(' ||exp[i]=='{' ||exp[i]=='['){
push(exp[i]);
}else if(exp[i]==')'||exp[i]=='}' ||exp[i]==']'){
if(top==NULL){
cout<<"No match found"<<endl;
return false;
}else{
if( (int(exp[i])==41&&int(top->data)==40) || (int(exp[i])==93&&int(top->data)==91) || (int(exp[i])==125&&int(top->data)==123) )
pop();
}
}
}
if(top==NULL){
cout<<"Balanced"<<endl;
return 1;
}else{
cout<<"Not Balanced"<<endl;
return 0;
}
}
int main() {
char exp[]="{[a+b]*[c+d]}";
cout<<"The expression is: "<<endl<<exp<<endl<<endl;
cout<<isBalance(exp);
//cout<<int(exp[0]);
}