-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostFix.js
47 lines (41 loc) · 1.08 KB
/
postFix.js
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
const math = require("./math");
const unary = require("./unary");
require("./arrCom");
module.exports = expr => {
let tokens = unary(expr);
let S = [],
O = [],
tok;
while ((tok = tokens.shift())) {
if (math.IsNumber(tok)) {
O.push(tok);
} else if (math.IsOperator(tok)) {
if (S.length === 0) {
S.push(tok);
} else {
while (math.GetPriority(S.peek()) >= math.GetPriority(tok)) {
O.push(S.pop());
if (S.length === 0) {
break;
}
}
S.push(tok);
}
} else if (tok === "(" || tok === ")") {
if (tok === "(") {
S.push("(");
} else {
while (S.peek() != "(") {
O.push(S.pop());
}
S.pop();
}
}
}
if (S.length !== 0) {
while (S.length !== 0) {
O.push(S.pop());
}
}
return O.join(" ");
};