-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9]fds_program_for_stack_parenthesized.cpp
101 lines (94 loc) · 1.74 KB
/
9]fds_program_for_stack_parenthesized.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
#include <iostream>
using namespace std;
#define size 10
class Stack
{
int top;
char stack[size];
public:
Stack()
{
top = -1;
}
void push(char);
char pop();
int isfull();
int isempty();
};
void Stack::push(char x)
{
top = top + 1;
stack[top] = x;
}
char Stack::pop()
{
char s;
s = stack[top];
top = top - 1;
return s;
}
int Stack::isfull()
{
if (top == size)
return 1;
else
return 0;
}
int Stack::isempty()
{
if (top == -1)
return 1;
else
return 0;
}
int main()
{
Stack s1;
char exp[20], ch;
int i = 0;
cout << "\nEnter the expression to check whether it is in well form or not : ";
cin >> exp;
if ((exp[0] == ')') || (exp[0] == ']') || (exp[0] == '}'))
{
cout << "\n Invalid 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 parenthesis...\n";
}
else
{
cout << "\nInvalid Expression, not well parenthesized....\n";
}
return 0;
}