-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.cpp
More file actions
110 lines (110 loc) · 1.59 KB
/
Copy pathStack.cpp
File metadata and controls
110 lines (110 loc) · 1.59 KB
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
#include <iostream>
using namespace std;
class stack
{
char s[25];
int top;
public:
void push(char val);
char pop();
bool isempty();
bool isfull();
void display();
stack() { top = -1; }
};
bool stack::isempty()
{
return top == -1; // Stack is empty if top == -1
}
void stack::push(char val)
{
if (top < 24) // Check if the stack is full
{
top++;
s[top] = val;
}
else
{
cout << "\nStack is full";
}
}
char stack::pop()
{
char val;
if (!isempty())
{
val = s[top];
top--;
return val;
}
else
{
cout << "\nStack is empty";
return '*'; // Return '*' to indicate an error
}
}
class paranthesis
{
char expn[25];
stack obj;
public:
void read();
void checkexpn();
};
void paranthesis::read()
{
cout << "\nEnter the expression: ";
cin >> expn;
cout << "\nEntered expression is: " << expn;
}
void paranthesis::checkexpn()
{
int i, flag;
char ch;
flag = 0;
for (i = 0; expn[i] != '\0'; i++)
{
if (expn[i] == '{' || expn[i] == '[' || expn[i] == '(')
{
obj.push(expn[i]);
}
if (expn[i] == '}' || expn[i] == ']' || expn[i] == ')')
{
if (!obj.isempty())
{
ch = obj.pop();
if (expn[i] == '}' && ch != '{')
{
flag = 1;
break;
}
if (expn[i] == ']' && ch != '[')
{
flag = 1;
break;
}
if (expn[i] == ')' && ch != '(')
{
flag = 1;
break;
}
}
else
{
flag = 1; // Unmatched closing bracket
break;
}
}
}
if (flag == 0 && obj.isempty())
cout << "\nExpression is well parenthesized";
else
cout << "\nExpression is not well parenthesized";
}
int main()
{
paranthesis obj1;
obj1.read();
obj1.checkexpn();
return 0;
}