-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfix to postfix.cpp
116 lines (96 loc) · 1.43 KB
/
infix to postfix.cpp
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
107
108
109
110
111
112
113
114
115
116
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
char stk[15],inp[20],out[20],elem;
int top=-1,count=0;
void push(char c)
{
top++;
stk[top]=c;
}
void pop()
{
out[count++]=stk[top];
top--;
}
int prior(char c)
{
switch(c)
{
case '/':
return 2;break;
case '*':
return 2;break;
case '+':
return 1;break;
case '-':
return 1;break;
case '^':
return 3;break;
default:
printf("\n please give a operator in the equation.");
}
}
int main()
{
int i,j;
printf("\n Enter the input Expression with delimitter #:");
scanf("%s",&inp);
printf("\n %s",inp);
for(i=0;i<=strlen(inp);i++)
{
elem=inp[i];
if(isalnum(elem))
{
out[count++]=elem;
}
else if(elem=='#')
{
while(top!=-1)
pop();
}
else
{
if(elem=='(')
{
push(elem);
elem=inp[++i];
while(elem!=')')
{
if(isalnum(elem))
out[count++]=elem;
else
push(elem);
elem=inp[++i];
}
i--;
}
else if(elem==')')
{
while(stk[top]!='(')
{
pop();
}
top-=1;
}
else
{
if(top==-1)
push(elem);
else
{
if(prior(stk[top])>=prior(elem))
{
pop();
push(elem);
}
else
push(elem);
}
}
}
}
out[count]='\0';
printf("\n %s",out);
}