-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathass4.cpp
96 lines (84 loc) · 2.41 KB
/
ass4.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
#include <iostream>
using namespace std;
#define SIZE 10
class stackexp {
int top;
char stk[SIZE];
public:
stackexp() : top(-1) {} // Initialize top to -1
void push(char);
char pop();
int isfull();
int isempty();
};
void stackexp::push(char x) {
if (isfull()) {
cout << "Stack overflow!" << endl;
return;
}
stk[++top] = x;
}
char stackexp::pop() {
if (isempty()) {
cout << "Stack underflow!" << endl;
return '\0'; // Return null character for empty stack
}
return stk[top--];
}
int stackexp::isfull() {
return top == SIZE - 1;
}
int stackexp::isempty() {
return top == -1;
}
int main() {
stackexp s1;
char exp[100]; // Increased size to accommodate longer expressions
int i = 0;
cout << "\n\t!! Parenthesis Checker !!" << endl;
cout << "\nEnter the expression to check whether it is well-formed or not: " << endl;
cin >> exp;
if ((exp[0] == ')') || (exp[0] == ']') || (exp[0] == '}')) {
cout << "\nInvalid Expression .......\n";
return 0;
} else {
while (exp[i] != '\0') {
char ch = exp[i];
switch (ch) {
case '(':
case '[':
case '{':
s1.push(ch);
break;
case ')':
if (s1.isempty() || s1.pop() != '(') {
cout << "\nSorry! The Expression is not well-parameterized....\n";
return 0;
}
break;
case ']':
if (s1.isempty() || s1.pop() != '[') {
cout << "\nSorry! The Expression is not well-parameterized....\n";
return 0;
}
break;
case '}':
if (s1.isempty() || s1.pop() != '{') {
cout << "\nSorry! The Expression is not well-parameterized....\n";
return 0;
}
break;
default:
cout << "\nInvalid Character in Expression.\n";
return 0;
}
i++;
}
}
if (s1.isempty()) {
cout << "\nThe expression is well-parameterized.\n";
} else {
cout << "\nSorry! The expression is not well-parameterized.\n";
}
return 0;
}