-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathInfixtoPostfix.c
105 lines (102 loc) · 2.02 KB
/
InfixtoPostfix.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
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define SIZE 10
struct stack{
char exp[SIZE];
int top;
};
void push(struct stack *,char);
char pop(struct stack *);
void intopost(struct stack *,char infixexp[],char postfixexp[]);
int prcd(char ch);
int main()
{
char infixexp[50],postfixexp[50];
struct stack s;
s.top=-1;
while(1){
printf("Enter the infix expression.\n");
scanf("%s",infixexp);
intopost(&s,infixexp,postfixexp);
printf("POSTFIX EXPRESSION: ");
puts(postfixexp);//printf("postfix form is %s \n",postfixexp);
}return 0;
}
void push(struct stack *s,char ch)
{
s->top++;
s->exp[s->top]=ch;
}
char pop(struct stack *s)
{
char ch;
ch=s->exp[s->top];
s->top--;
return ch;
}
void intopost(struct stack *s,char infixexp[],char postfixexp[])
{
int i=0,j=0;
char ch;
while (infixexp[i]!='\0')// better use for loop
{
if(isalnum(infixexp[i]))
{
postfixexp[j++]=infixexp[i];
}else
{
if(infixexp[i]=='(')
{push(s,infixexp[i]);
}
else if(infixexp[i]==')')
{
while((ch=pop(s))!='(')
{
postfixexp[j++]=ch;
}
}
else if (prcd(infixexp[i])>prcd(s->exp[s->top]))
{
push(s,infixexp[i]);
}else if(prcd(infixexp[i])<=prcd(s->exp[s->top]))
{
while(prcd(infixexp[i])<= prcd(s->exp[s->top]))
{
ch=pop(s);
postfixexp[j++]=ch;
}
push(s,infixexp[i]);
}
/*else//can skip it because we have witten '<=' in upper else if statement.
{
while(prcd(infixexp[i])==prcd(s->exp[s->top]))
{
ch=pop(s);
postfixexp[j++]=ch;
}
push(s,infixexp[i]);
}*/
}
i++; //update i for ist while loop
}
while(s->top!=-1)
{
ch=pop(s);
postfixexp[j++]=ch;
}postfixexp[j]='\0';
}
int prcd(char ch)
{
if (ch=='$'){
return 3;}
else if (ch=='*'||ch=='/')
{
return 2;}
else if (ch=='+'||ch=='-')
{
return 1;}
else{
return 0;
}
}