-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunary.js
43 lines (40 loc) · 1.26 KB
/
unary.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
const math = require("./math");
const arrCom = require("./arrCom");
module.exports = expr => {
let saveOp = "";
let num = false;
let newToken = [];
expr = arrCom.ClaerExpr(expr);
expr = arrCom.NumberMerge(expr);
expr.forEach((x, i) => {
if (i == 0 && math.IsOperator(x)) {
saveOp = x;
num = true;
} else if (!math.IsOperator(x) && !math.IsNumber(x)) {
saveOp = x;
num = true;
newToken.push(x);
} else if (num && math.IsNumber(x)) {
saveOp = math.IsOperator(saveOp) ? saveOp : "";
if (saveOp == "+") {
newToken.push(saveOp);
newToken.push(x);
} else {
newToken.push(saveOp + x);
}
num = false;
} else if (!math.IsOperator(saveOp) && !math.IsNumber(saveOp)) {
if (!math.IsOperator(expr[i + 1]) && !math.IsNumber(expr[i + 1])) {
newToken.push(x);
}
} else if (math.IsOperator(saveOp) && math.IsOperator(x)) {
newToken.push(saveOp);
num = true;
} else {
newToken.push(x);
}
saveOp = x;
});
newToken = arrCom.IsDubling(newToken);
return newToken;
};