-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfixToPostfix.c
64 lines (59 loc) · 1.31 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
#include<stdio.h>
#include<string.h>
char infix[20],postfix[20],s[20];
int priority(char);
void infix_to_postfix(char[]);
void push(char);
char pop();
int top=-1;
void main(){
int i,n;
printf("Enter the expression: ");
gets(infix);
infix_to_postfix(infix);
}
void infix_to_postfix(char infix[]){
int i;
int j=0;
for(i=0;infix[i]!='\0';i++){
if(infix[i]>='a' && infix[i]<='z')
postfix[j++]=infix[i];
else if(infix[i]>='A' && infix[i]<='Z')
postfix[j++]=infix[i];
else if(infix[i]=='(')
push(infix[i]);
else if(infix[i]==')'){
while(s[top]!='(')
postfix[j++]=pop();
pop();
}
else{
while(top!=-1 && priority(s[top])>=priority(infix[i]))
postfix[j++]=pop();
push(infix[i]);
}
}
while(top>=0)
postfix[j++]=pop();
postfix[j]='\0';
printf("%s", postfix);
}
void push(char item){
top++;
s[top]=item;
}
char pop(){
char temp=s[top];
top--;
return temp;
}
int priority(char c){
if(c=='^')
return 3;
else if(c=='*'||c=='/')
return 2;
else if(c=='+'||c=='-')
return 1;
else
return 0;
}