-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_brack.c
More file actions
97 lines (97 loc) · 1.91 KB
/
Copy pathstack_brack.c
File metadata and controls
97 lines (97 loc) · 1.91 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
#include <stdio.h>
#include <stdlib.h>
#define size 100
typedef struct
{
char a[size];
int top;
} stack_type;
int initial(stack_type *v)
{
v->top = -1;
}
int overflow(stack_type *v)
{
if (v->top == size - 1)
return 0;
else
return 1;
}
int underflow(stack_type *v)
{
if (v->top == -1)
return 0;
else
return 1;
}
void push(stack_type *v, char ch)
{
v->top++;
v->a[v->top] = ch;
}
void pop(stack_type *v)
{
int temp = v->a[v->top];
v->top--;
}
int main()
{
stack_type s;
initial(&s);
int u;
char ch[100];
printf("Enter the arithmetic expression\n");
scanf("\n%s", &ch);
int i;
for (i = 0; ch[i] != '\0'; i++)
{
if (ch[i] == '(')
push(&s, ch[i]);
else if (ch[i] == ')')
{
u = underflow(&s);
if (u == 0)
{
printf("The expression is not balanced");
exit(0);
}
pop(&s);
}
}
for (i = 0; ch[i] != '\0'; i++)
{
if (ch[i] == '[')
push(&s, ch[i]);
else if (ch[i] == ']')
{
u = underflow(&s);
if (u == 0)
{
printf("The expression is not balanced");
exit(0);
}
pop(&s);
}
}
for (i = 0; ch[i] != '\0'; i++)
{
if (ch[i] == '{')
push(&s, ch[i]);
else if (ch[i] == '}')
{
u = underflow(&s);
if (u == 0)
{
printf("The expression is not balanced");
exit(0);
}
pop(&s);
}
}
u = underflow(&s);
if (u == 0)
printf("The expression is balanced");
else
printf("The expression is not balanced");
return 0;
}