-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path_3.a_Infix to postfix.c
128 lines (123 loc) · 1.55 KB
/
_3.a_Infix to postfix.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include<stdio.h>
#define MAXSIZE 20
struct lifo
{
char st[MAXSIZE];
int top;
};
typedef struct lifo STACK;
STACK s;
void push(char x)
{
s.top = s.top + 1;
s.st[s.top] = x;
}
char pop()
{
s.top = s.top - 1;
return (s.st[s.top+1]);
}
int isp(char x)
{
if (x == '(')
{
return 0;
}
else if (x == '+' || x == '-')
{
return 3;
}
else if (x == '*' || x == '/')
{
return 5;
}
else if (x == '^')
{
return 7;
}
else
{
return -1;
}
}
int icp(char x)
{
if (x == '+' || x == '-')
{
return 2;
}
else if (x == '*' || x == '/')
{
return 4;
}
else if (x == '^')
{
return 6;
}
else if (x == '(')
{
return 8;
}
else
{
return -1;
}
}
int main()
{
int i;
char exp[20],*e;
s.top = -1;
printf("Enter the infix expression:");
scanf("%s",exp);
e = exp;
push('#');
while(*e != '#')
{
if (*e >= 'a' && *e <= 'z' || *e >= 'A' && *e <= 'Z')
{
printf("%c", *e);
}
else if (*e == '(')
{
push(*e);
}
else if (*e == ')')
{
while (s.st[s.top] != '(')
{
char x = pop();
printf("%c", x);
}
pop();
}
else
{
if(s.top == -1)
{
push(*e);
}
else
{
while(isp(s.st[s.top]) >= icp(*e))
{
char x = pop();
printf("%c", x);
}
push(*e);
}
}
e++;
}
if(s.st[s.top] != '#')
{
char x = pop();
printf("%c",x);
s.top = s.top-1;
}
else
{
pop();
}
return 0;
}