-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfix_postfix.c
More file actions
89 lines (89 loc) · 1.79 KB
/
infix_postfix.c
File metadata and controls
89 lines (89 loc) · 1.79 KB
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
#include<stdio.h>
#include<conio.h>
#include<string.h>
int top=0,ele;
char stack[30];
void push(int);
char pop();
char infix[30],postfix[30];
int prec(char);
int main()
{
int i=0,j=0,length;
char temp;
printf("Enter infix expresion::\n");
scanf("%s",infix);
length=strlen(infix);
for(i=0;i<length;i++)
{
if(infix[i]!='+' && infix[i]!='-' && infix[i]!='*' && infix[i]!='/' && infix[i]!='^' && infix[i]!=')' && infix[i]!='(')
{
postfix[j++]=infix[i];
}
else
{
if(top==0)
push(infix[i]);
else
{
if(infix[i]!=')' && infix[i]!='(')
{
if(prec(infix[i])<=prec(stack[top-1]))
{
temp=pop();
postfix[j++]=temp;
push(infix[i]);
}
else
push(infix[i]);
}
else
{
if(infix[i]=='(')
{
push(infix[i]);
}
if(infix[i]==')')
{
temp=pop();
while(temp!='(')
{
postfix[j++]=temp;
temp=pop();
}
}
}
}
}
}
while(top!=0)
{
postfix[j++]=pop();
}
printf("postfix is:: ");
printf("%s",postfix);
}
void push(int ele)
{
stack[top]=ele;
top++;
}
char pop()
{
top--;
return(stack[top]);
}
int prec(char symbol)
{
if(symbol=='(')
return(0);
if(symbol==')')
return(0);
if(symbol=='+' || symbol=='-')
return 1;
if(symbol=='*' || symbol=='/')
return 2;
if(symbol=='^')
return 3;
getch();
}