-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathin2post.c
106 lines (93 loc) · 1.59 KB
/
in2post.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <stdio.h>
#include <ctype.h>
#define SIZE 500
char stack[SIZE];
int top=-1;
int isEmpty() {
if(top==-1) return 1;
else return 0;
}
int isFull() {
if(top==SIZE-1) return 1;
else return 0;
}
char onTop() {
return stack[top];
}
void push(char element) {
if(isFull())
printf("Stack is full\n");
else {
top++;
stack[top]=element;
}
}
void pop() {
if(isEmpty()==1)
printf("Stack is empty\n");
else
top--;
}
int in_pri(char opr)
{ switch(opr)
{
case '(': return 4;
case '*':
case '/':
case '%': return 3;
case '+':
case '-': return 2;
}
}
int stack_pri(char opr)
{ switch(opr)
{
case '(': return 1;
case '*':
case '/':
case '%': return 3;
case '+':
case '-': return 2;
}
}
main()
{
char infix[200],postfix[200],token;
int i,j=0;
printf("Enter infix expr :\n");
scanf("%s",infix);
for(i=0;infix[i]!='\0';i++)
{ token=infix[i];
if(isdigit(token)) // if operand
postfix[j++]=token;
else // if operator
{
if(isEmpty()==1)
push(token);
else if(token==')')
{
while( onTop()!='(')
{
postfix[j++]=onTop();
pop();
}
pop(); // remove the recent '('
}
else if(in_pri(token)>stack_pri(onTop()) )
push(token);
else
{
postfix[j++]=onTop();
pop();
push(token);
}
}
}//for ends here
while(!isEmpty())
{
postfix[j++]=onTop();
pop();
}
postfix[j++]='\0';
printf("Postfix:%s\n",postfix);
}