forked from a-r-nida/HactoberFest2020-Beginers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
balancing_equation.c
74 lines (74 loc) · 1.28 KB
/
balancing_equation.c
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
#include<stdio.h>
#define SIZE 50
char stack[SIZE];
int Top = -1;
void push(char item)
{
if( Top >= SIZE-1 )
printf("\nStack overflow.\n");
else
stack[++Top] = item;
}
char pop()
{
char item;
if( Top<0 )
{
printf("\nStack underflow : INVALID EXPRESSION\n");
}
else
{
return(stack[Top--]);
}
}
void checkExp(char exp[])
{
int i=0;
char c = exp[i],item;
while(exp[i]!='\0')
{
if(c=='{'||c=='['||c=='(')
push(c);
else if(c=='}')
{
item = pop();
if( item!='{' )
{
printf("missing pair of %c.\n",item);
printf("Non balanced expression.\n");
return;
}
}
else if(c==']')
{
item = pop();
if( item!='[' ){
printf("missing pair of %c.\n",item);
printf("Non balanced expression.\n");
return;
}
}
else if(c==')')
{
item = pop();
if( item!='(' ){
printf("missing pair of %c.\n",item);
printf("Non balanced expression.\n");
return;
}
}
c=exp[++i];
}
if(Top<0)
printf("Balanced Expression.\n");
else
printf("Non balanced expression:(Since it has odd number of symbols)\n");
}
int main()
{
char exp[SIZE];
printf("Enter expression.\n");
gets(exp);
checkExp(exp);
return 0;
}