-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.js
30 lines (27 loc) · 832 Bytes
/
calc.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
const math = require("./math");
const postFix = require("./postFix");
const operation = require("./operation");
const expression = require("./expression");
this.PostFixCalc = expr => {
expr = expression(expr);
let postfixExpr = postFix(expr);
let operandStack = [];
let arrToken = postfixExpr.split(" ");
arrToken.forEach(token => {
if (math.IsNumber(token)) {
operandStack.push(parseFloat(token));
} else {
let b = operandStack.pop();
let a = operandStack.pop();
let result = "";
if (math.IsOperator(token)) {
result = operation[token].calc(a, b);
}
operandStack.push(result);
}
});
return operandStack.pop();
};
module.exports = get => {
return this.PostFixCalc(get);
};