-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpostfix0001
155 lines (117 loc) · 2.7 KB
/
postfix0001
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<script>
// JavaScript program to convert
// infix to prefix.
// Function to check if
// given character is
// an operator or not.
function isOperator(c)
{
return (!(c >= 'a' && c <= 'z') &&
!(c >= '0' && c <= '9') &&
!(c >= 'A' && c <= 'Z'));
}
// Function to find priority
// of given operator.
function getPriority(C)
{
if (C == '-' || C == '+')
return 1;
else if (C == '*' || C == '/')
return 2;
else if (C == '^')
return 3;
return 0;
}
// Function that converts infix
// expression to prefix expression.
function infixToPrefix(infix)
{
// stack for operators.
let operators = [];
// stack for operands.
let operands = [];
for (let i = 0; i < infix.length; i++)
{
// If current character is an
// opening bracket, then
// push into the operators stack.
if (infix[i] == '(')
{
operators.push(infix[i]);
}
// If current character is a
// closing bracket, then pop from
// both stacks and push result
// in operands stack until
// matching opening bracket is
// not found.
else if (infix[i] == ')')
{
while (operators.length!=0 &&
operators[operators.length-1] != '(')
{
// operand 1
let op1 = operands.pop();
// operand 2
let op2 = operands.pop();
// operator
let op = operators.pop();
// Add operands and operator
// in form operator +
// operand1 + operand2.
let tmp = op + op2 + op1;
operands.push(tmp);
}
// Pop opening bracket
// from stack.
operators.pop();
}
// If current character is an
// operand then push it into
// operands stack.
else if (!isOperator(infix[i]))
{
operands.push(infix[i] + "");
}
// If current character is an
// operator, then push it into
// operators stack after popping
// high priority operators from
// operators stack and pushing
// result in operands stack.
else
{
while (operators.length &&
getPriority(infix[i]) <=
getPriority(operators[operators.length-1]))
{
let op1 = operands.pop();
let op2 = operands.pop();
let op = operators.pop();
let tmp = op + op2 + op1;
operands.push(tmp);
}
operators.push(infix[i]);
}
}
// Pop operators from operators
// stack until it is empty and
// operation in add result of
// each pop operands stack.
while (operators.length!=0)
{
let op1 = operands.pop();
let op2 = operands.pop();
let op = operators.pop();
let tmp = op + op2 + op1;
operands.push(tmp);
}
// Final prefix expression is
// present in operands stack.
return operands[operands.length-1];
}
// Driver code
let s = "(A-B/C)*(A/K-L)";
document.write( infixToPrefix(s));
// This code is contributed by avanitrachhadiya2155
</script>