-
Notifications
You must be signed in to change notification settings - Fork 0
/
Practical_26(C++).cpp
79 lines (72 loc) · 1.7 KB
/
Practical_26(C++).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
/*
In any language program mostly syntax error occurs due to unbalancing delimiter such as
(),{},[]. Write C++ program using stack to check whether given expression is well parenthesized or not.
*/
#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\t!! Parenthesis Checker..!!!!" << endl;
cout << "\nEnter the expression to check whether it is well-formed or not: ";
cin >> exp;
if ((exp[0] == ')' || exp[0] == ']' || exp[0] == '}')) {
cout << "\nInvalid Expression.....\n";
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 parenthesized...\n";
} else {
cout << "\nSorry!!! Invalid Expression or not well parenthesized....\n";
}
return 0;
}