-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathD26.cpp
executable file
·182 lines (137 loc) · 3.33 KB
/
D26.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* DSL – EXPERIMENT 9 – D26 */
#include <iostream>
using namespace std;
#define MAX 25
// Class Stack
class Stack {
private:
char *data; // Dynamic allocation or else 'char data[MAX]'
int top;
public:
Stack() { // Constructor initialization
data = new char[MAX]; // Allocate 'data' with 'new char[MAX]'
top = -1;
}
bool isEmpty(); // Prototypes
bool isFull();
bool push(char x);
char pop();
char peek();
void display();
};
// Class Parenthesis
class Parenthesis {
char expn[MAX];
Stack stack;
public:
void read();
void checkexpn();
};
// Stack class function defintions
bool Stack::isEmpty() {
return (top == -1);
}
bool Stack::isFull() {
return (top == (MAX - 1));
}
bool Stack::push(char x) {
if (isFull()) {
cout << "Stack Overflow\n";
return false;
}
else {
data[++top] = x;
cout << x << " : Pushed into stack\n";
return true;
}
}
char Stack::pop() {
char x;
if (isEmpty()) {
cout << "Stack Underflow\n";
return 0;
}
else{
x = data[top--];
cout << x << " : Popped from stack\n";
return x;
}
}
char Stack::peek() {
if (isEmpty()) {
cout << "Stack is Empty\n";
return 0;
}
else
return data[top];
}
void Stack::display() {
cout << "\nCurrent stack is: ";
for (int i = 0; i <= top; i++) {
cout << data[i] << " --> ";
}
cout << "NULL";
}
// Parenthesis class function defintions
void Parenthesis::read() {
cout << "\nEnter the expression: "; cin >> expn;
cout << "\n";
}
void Parenthesis::checkexpn() {
int i,flag = 0;
char ch;
for (i = 0; expn[i] != '\0'; i++) {
if (expn[i] == '{' || expn[i] == '[' || expn[i] == '(')
stack.push(expn[i]);
if(expn[i] == '}' || expn[i] == ']' || expn[i] == ')') {
if (!stack.isEmpty()) {
ch = stack.pop();
if (expn[i] == '}' && ch != '{') {
flag = 1;
break;
}
if (expn[i] == ']' && ch != '[') {
flag = 1;
break;
}
if (expn[i] == ')' && ch != '(') {
flag = 1;
break;
}
}
}
}
stack.display();
if (flag == 0 && stack.isEmpty())
cout<<"\n\nExpression is in well Paranthesis";
else
cout<<"\n\nExpression is not in well Paranthesis";
}
int main() {
Parenthesis expr;
expr.read();
expr.checkexpn();
return 0;
}
/*
--------------------- OUTPUT -----------------------
Enter the expression: [(a+b)/{c*(d+e)}]
[ : Pushed into stack
( : Pushed into stack
( : Popped from stack
{ : Pushed into stack
( : Pushed into stack
( : Popped from stack
{ : Popped from stack
[ : Popped from stack
Current stack is: NULL
Expression is in well Paranthesis
Enter the expression: [(a+b)/{c*(d+e
[ : Pushed into stack
( : Pushed into stack
( : Popped from stack
{ : Pushed into stack
( : Pushed into stack
Current stack is: [ --> { --> ( --> NULL
Expression is not in well Paranthesis
*/