-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.js
86 lines (80 loc) · 2.91 KB
/
calculator.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
79
80
81
82
83
84
85
86
var CALCULATOR = new Object;
var OP = new Object;
OP["_"] = { prec: 6, ass: "L", op: function(a, b){ if(!a)return b; if(!b)return a; return Math.min(a, b); } };
OP["·"] = { prec: 6, ass: "L", op: function(a, b){ if(!a)return b; if(!b)return a; return Math.max(a, b); } };
OP["l"] = { prec: 5, ass: "L", op: function(a, b){ if(!a)a = 1; if(!b)b = 1; return Math.log(a)/Math.log(b); } };
OP["^"] = { prec: 4, ass: "R", op: function(a, b){ if(!a)a = 1; if(!b)b = 1; return Math.pow(a, b); } };
OP["*"] = { prec: 3, ass: "L", op: function(a, b){ if(!a)a = 1; if(!b)b = 1; return a*b; } };
OP["/"] = { prec: 3, ass: "L", op: function(a, b){ if(!a)a = 1; if(!b)b = 1; return a/b; } };
OP["+"] = { prec: 2, ass: "L", op: function(a, b){ if(!a)a = 0; if(!b)b = 0; return a+b; } };
OP["-"] = { prec: 2, ass: "L", op: function(a, b){ if(!a)a = 0; if(!b)b = 0; return a-b; } };
OP["("] = { prec: 1, ass: "L" };
OP[")"] = { prec: 1, ass: "L" };
OP["×"] = OP["*"];
OP["÷"] = OP["/"];
OP["−"] = OP["-"];
function compare_symbols(s_new, s_old){
return OP[s_new].prec < OP[s_old].prec || (OP[s_new].prec == OP[s_old].prec && OP[s_old].ass == "L");
}
function translate(arr){
var OUTPUT = new Array;
var OP_STK = new Array;
for(var x of arr){
if(x == "("){
OP_STK.push("(");
} else if(x == ")"){
while(OP_STK.length > 0 && OP_STK[OP_STK.length-1] != "("){
var k = OP_STK.pop();
OUTPUT.push(k);
}
if(OP_STK.length == 0) throw new Error("¡Paréntesis desequilibrados!");
else OP_STK.pop();
} else if(OP[x] != undefined) {
if(OP_STK.length > 0){
while(OP_STK.length > 0 && compare_symbols(x, OP_STK[OP_STK.length-1])){
var k = OP_STK.pop();
OUTPUT.push(k);
if(OP_STK.length > 0){
}
}
}
OP_STK.push(x);
} else {
OUTPUT.push(x);
}
}
while(OP_STK.length > 0) OUTPUT.push(OP_STK.pop());
return OUTPUT;
}
function eval_stack(stk){
var stack = new Array;
for(var i of stk){
if(OP[i] != undefined){
var b = stack.pop();
var a = stack.pop();
stack.push(""+OP[i].op(parseFloat(a), parseFloat(b)));
} else stack.push(i);
}
return stack.pop();
}
CALCULATOR.stack_computation = function(arr){
return eval_stack(translate(arr));
}
CALCULATOR.arrayfy = function(str){
var ret = new Array;
var temp = "";
for(var i of str){
if(OP[i] != undefined){
if(temp == "e") temp = "2.71828182845904";
if(temp == "pi" || temp == "π") temp = "3.14159265358979323";
if(temp.length != 0) ret.push(temp);
ret.push(i);
temp = "";
} else if(i != " ")temp += i;
}
if(temp == "e") temp = "2.71828182845904";
if(temp == "pi" || temp == "π") temp = "3.14159265358979323";
if(temp.length != 0) ret.push(temp);
return ret;
}
module.exports = CALCULATOR;