forked from harikrishnan669/DS_Lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
infix_to_postfix.c
78 lines (78 loc) · 1.23 KB
/
infix_to_postfix.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
75
76
77
78
#include<stdio.h>
char stack[30];
int top=-1;
int priority(char);
void push(char x)
{
top=top+1;
stack[top]=x;
}
char pop()
{
char ch;
ch=stack[top];
top=top-1;
return ch;
}
int main()
{
char exp[30],x;
int i=1;
int z;
printf("Enter the expression\n");
scanf("%s",exp);
x=exp[0];
while(x!='\0')
{
if(x>='a' && x<='b')
{
printf("%c",x);
}
else if(x=='(')
{
push(x);
}
else if(x==')')
{
z=pop();
while(z!='(')
{
printf("%c",z);
z=pop();
}
}
else
{
while(priority(stack[top])>=priority(x))
{
printf("%c",pop());
}
push(x);
}
x=exp[i++];
}
while(top!=-1)
{
printf("%c",pop());
}
return 0;
}
int priority(char ch)
{
if(ch=='(')
{
return 0;
}
if(ch=='+'||ch=='-')
{
return 1;
}
if(ch=='*'||ch=='/')
{
return 2;
}
if(ch=='^')
{
return 3;
}
}