-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack_CheckBalance.cpp
91 lines (80 loc) · 1.77 KB
/
Stack_CheckBalance.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
#include <iostream>
using namespace std;
#define size 10
class stackexp {
int top;
char stk[size];
public:
stackexp() {
top = -1;
}
void push(char);
char pop();
int isfull();
int isempty();
};
void stackexp::push(char x) {
top = top + 1;
stk[top] = x;
}
char stackexp::pop() {
char s;
s = stk[top];
top = top - 1;
return s;
}
int stackexp::isfull() {
if (top == size)
return 1;
else
return 0;
}
int stackexp::isempty() {
if (top == -1)
return 1;
else
return 0;
}
int main() {
stackexp s1;
char exp[20], ch;
int i = 0;
cout << "\n==========================Parenthesis Balancing==========================" << endl;
cout << "\nEnter the expression to check whether it is in well form or not: ";
cin >> exp;
if ((exp[0] == ')') || (exp[0] == ']') || (exp[0] == '}')) {
cout << "\nInvalid expression.";
return 0;
} else {
while (exp[i] != '\0') {
ch = exp[i];
switch (ch) {
case '(':
s1.push(ch);
break;
case '[':
s1.push(ch);
break;
case '{':
s1.push(ch);
break;
case ')':
s1.pop();
break;
case ']':
s1.pop();
break;
case '}':
s1.pop();
break;
}
i = i + 1;
}
}
if (s1.isempty()) {
cout << "\nExpression is well parenthesis.";
} else {
cout << "\nInvalid expression or not in well parenthesized.";
}
return 0;
}