-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfixToPostfix.cpp
More file actions
73 lines (48 loc) · 893 Bytes
/
InfixToPostfix.cpp
File metadata and controls
73 lines (48 loc) · 893 Bytes
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
#include <stdio.h>
#include <string.h>
int main()
{
char in[1000]="(",post[1000],stack[1000],temp[999],t;
int k=0;
int st=-1,p=-1;
printf("Enter the infix expression : \n");
gets(temp);
strcat(in,temp);
strcat(in,")");
while(in[k]!='\0')
{
t=in[k++];
if(t=='(')
stack[++st]=t;
else if(t=='-' || t=='+')
{
while(stack[st]!='(')
post[++p]=stack[st--];
stack[++st]=t;
}
else if(t=='^')
{
if(stack[st-1]!=t)
stack[++st]=t;
else
post[++p]=t;
}
else if(t=='/' || t=='*' || t=='%')
{
while(stack[st]!='(' && stack[st]!='+' && stack[st]!='-')
post[++p]=stack[st--];
stack[++st]=t;
}
else if(t==')')
{
while(stack[st]!='(')
post[++p]=stack[st--];
st--;
}
else
post[++p]=t;
}
post[++p]='\0';
printf("The corresponding postfix expression is : \n");
puts(post);
}