-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrpn.js
170 lines (154 loc) · 5.48 KB
/
rpn.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
(function () {
'use strict';
var rpn = {
_precedence: {'√': 3, '%': 3, '!': 3, '^': 3, '/': 2, '*': 2, '-': 1, '+': 1, '#': 0},
/**
* operations
* @private
*/
_operation: {
'+': (a, b) => (+a) + (+b),
'-': (a, b) => (+a) - (+b),
'*': (a, b) => (+a) * (+b),
'/': (a, b) => (+a) / (+b),
'^': (x, n) => Math.pow(+x, +n),
'!': function (n) {
for (var i = 1, r = 1; i <= +n; i++) {
r = r * i;
}
return (+n < 0) ? NaN : r;
},
'%': n => +n / 100,
'√': n => Math.sqrt(+n)
},
/**
* split expression to array
* @private
* @param exp - infix expression
* @returns {Array|null}
*/
_splitExp: function (exp) {
exp = exp.replace(/[a-zA-Z]/g, '').replace(/([\d%!])\-(\d)/g, '$1 - $2').replace(/([+\-\*\/^])\-(\d)/g, '$1 -$2');
return (/^[+*\/!^%]|\d\(|[\d\)]√|%[\d\(]|![\d\(]|%%|[+\-*\/^]{2,}|[+\-*\/√^]$/.test(exp)) ?
null : exp.match(/(-?(?:\d+\.?\d*|-?\.\d*))|[()+\-*\/√!^%]/gi);
},
/**
* check character, is or not a operator
* @private
* @param char - character
* @returns {boolean}
*/
_isOperator: function (char) {
return /^[√%!^\/\*\-\+#]$/.test(char);
},
/**
* check character, is or not a unary operator
* @private
* @param char - character
* @returns {boolean}
*/
_isUnaryOperator: function (char) {
return /^[√%!]$/.test(char);
},
/**
* check character, is or not a bracket
* @private
* @param char - character
* @returns {boolean}
*/
_isBrackets: function (char) {
return /^[\(\)]$/.test(char);
},
/**
* check string, is or not a number
* @private
* @param str - character
* @returns {boolean}
*/
_isNumber: function (str) {
return /^-?\d+\.\d+$|^-?\d+$/.test(str);
},
/**
* transfer infix expression to reverse polish notation
* @param exp - infix expression
* @returns {string|null}
*/
infix2rpn: function (exp) {
var arrExp = rpn._splitExp(exp),
expStack = [], opStack = [], opItem, stackItem;
if (!arrExp) {
return null;
}
arrExp = arrExp.concat('#');
for (var looper = 0; looper < arrExp.length; looper++) {
opItem = arrExp[looper];
if (rpn._isNumber(opItem)) {
expStack.push(opItem);
} else if (rpn._isOperator(opItem)) {
while (opStack.length) {
stackItem = opStack.pop();
if ((opItem === '√' && stackItem === '√' && rpn._precedence[stackItem] > rpn._precedence[opItem]) ||
((opItem !== '√' || stackItem !== '√') && rpn._precedence[stackItem] >= rpn._precedence[opItem])) {
expStack.push(stackItem);
} else {
opStack.push(stackItem);
break;
}
}
opStack.push(opItem);
} else if (rpn._isBrackets(opItem)) {
if (opItem === '(') {
opStack.push(opItem);
} else {
while (opStack.length) {
stackItem = opStack.pop();
if (stackItem !== '(') {
expStack.push(stackItem);
} else {
break;
}
}
}
}
}
return expStack.length ? expStack.join(' ') : null;
},
/**
* calculate reverse polish notation
* @param exp - reverse polish notation
* @returns {number}
*/
rpnCalculate: function (exp) {
var arrExp = exp.split(' '), calcStack = [], opItem, param1, param2;
for (var looper = 0; looper < arrExp.length; looper++) {
opItem = arrExp[looper];
if (rpn._isNumber(opItem)) {
calcStack.push(opItem);
} else if (rpn._isOperator(opItem)) {
if (rpn._isUnaryOperator(opItem)) {
calcStack.push(rpn._operation[opItem](calcStack.pop()));
} else {
param2 = calcStack.pop();
param1 = calcStack.pop();
calcStack.push(rpn._operation[opItem](param1, param2));
}
}
}
return +calcStack.pop().toFixed(3);
},
/**
* calculate expression
* @param exp - expression string
* @returns {number|null}
*/
calculate: function (exp) {
return rpn.rpnCalculate(rpn.infix2rpn(exp));
}
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = rpn;
}
if (typeof window !== 'undefined') {
window.rpn = rpn;
}
}());