-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfix2postfix.c
80 lines (70 loc) · 1.26 KB
/
infix2postfix.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
#include<stdio.h>
#include<ctype.h>
#define SIZE 50
void push(char elem);
char pop();
int priority(char elem);
char s[SIZE];
int top =-1;
int main()
{
char infix[50],postfix[50],ch,elem;
int i=0,k=0;
printf("\n enter your infix equation \n");
scanf("%s",infix);
push('#');
while((ch=infix[i++])!='\0')
{
if(ch=='(')
{
push(ch);
}
else if(isalnum(ch))
{
postfix[k++]=ch;
}
else if(ch==')')
{
while(s[top]!='(')
{
postfix[k++]=pop();
}
elem=pop();
}
else
{
while(priority(s[top])>=priority(ch))
postfix[k++]=pop();
push(ch);
}
}
while(s[top]!='#')
postfix[k++]=pop();
postfix[k]='\0';
printf("\n given postfix string is %s",postfix);
}
void push(char elem)
{
top=top+1;
s[top]=elem;
}
char pop()
{
return(s[top--]);
}
int priority(char elem )
{
switch (elem)
{
case '#':
return 0;
case '(':
return 1;
case '+':
case '-':
return 2;
case '*':
case '/':
return 3;
}
}