-
Notifications
You must be signed in to change notification settings - Fork 7
/
infixToPostfix.js
78 lines (76 loc) · 1.88 KB
/
infixToPostfix.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
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
function isBracketed(expr) {
var stack = []
for (var i = 0; i < expr.length; i++) {
if ('(' === expr[i]) {
stack.push('(')
} else if (')' === expr[i]) {
stack.pop()
if (stack.length === 0) {
if (i !== expr.length - 1) {
return false
} else {
return true
}
}
}
}
return false;
}
function isValidExpression(expr) {
if (expr.length < 3) {
return false;
}
for (var i = 0; i < expr.length; i++) {
if ('abcdefghijklmnopqrstuvwxyz*()/+-'.indexOf(expr[i]) === -1) {
return false;
}
}
try {
while ("(" === expr[0] && ")" === expr[expr.length - 1]) {
if (isBracketed(expr)) {
expr = expr.substring(1, expr.length - 1);
} else break;
}
var res = math.parse(expr);
if ((typeof res.implicit === 'undefined') || res.fn.indexOf('unary') !== -1) {
return false;
}
return true;
}
catch (ex) {
return false;
}
}
function infixToPostfix(expression) {
if (!isValidExpression(expression)) {
return null;
}
const prec = { "*": 3, "/": 3, "-": 2, "+": 2, "(": 1 }
op_stack = []
postfixList = []
tokens = expression.split('')
for (const token of tokens) {
if ("abcdefghijklmnopqrstuvwxyz".indexOf(token) !== -1) {
postfixList.push(token)
} else if ("(" === token) {
op_stack.push(token)
} else if (")" === token) {
var top_op_token = op_stack.pop()
while (top_op_token !== '(') {
postfixList.push(top_op_token)
top_op_token = op_stack.pop()
}
} else {
var peek_elem = op_stack.slice(-1)[0];
while (op_stack.length > 0 && (prec[peek_elem] >= prec[token])) {
postfixList.push(op_stack.pop())
peek_elem = op_stack.slice(-1)[0];
}
op_stack.push(token)
}
}
while (op_stack.length > 0) {
postfixList.push(op_stack.pop())
}
return postfixList
}