forked from sarthakd999/Hacktoberfest2021-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parenthesis.cpp
102 lines (97 loc) · 1.74 KB
/
Parenthesis.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//Parenthsis Matching
#include <iostream>
#include <stdlib.h>
#include<string.h>
#define MAX 10
using namespace std;
struct stack{
int top;
char exp[MAX];
}sp;
void push(char);
void pop();
int main()
{
sp.top = -1;
cout<<"Input values : ";
cin>>sp.exp;
for(int i=0;i<strlen(sp.exp);i++)
{
if(sp.exp[i]=='(' || sp.exp[i]== '{' || sp.exp[i]=='[')
{
push(sp.exp[i]);
continue;
}
else if(sp.exp[i]==')' || sp.exp[i]=='}' || sp.exp[i]==']')
{
if(sp.exp[i]==')')
{
if(sp.exp[sp.top]=='(')
{
pop();
}
else
{
cout<<"Unbalanced"<<endl;
break;
}
}
if(sp.exp[i]=='}')
{
if(sp.exp[sp.top]=='{')
{
pop();
}
else
{
cout<<"Unbalanced"<<endl;
break;
}
}
if(sp.exp[i]==']')
{
if(sp.exp[sp.top]=='[')
{
pop();
}
else
{
cout<<"Unbalanced"<<endl;
break;
}
}
}
}
if(sp.top == -1)
{
cout<<"Balanced"<<endl;
}
else if(sp.top != -1)
{
cout<<"Unbalanced"<<endl;
}
return 0;
}
void push(char item)
{
if(sp.top == MAX-1)
{
cout<<"Stack Overflow"<<endl;
}
else
{
sp.top = sp.top+1;
sp.exp[sp.top] = item;
}
}
void pop()
{
if(sp.top == -1)
{
cout<<"Stack Underflow"<<endl;
}
else
{
sp.top = sp.top - 1;
}
}