逆波兰表达式工具,普通表达式 和 逆波兰表达式 互相转换 ,计算 普通表达式 和 逆波兰表达式,支持 + - * / ^ % ! √ ( )。
可以在浏览器里使用,也可以当作Node的模块使用。
$npm install jsrpn
$node
> let rpn = require('jsrpn');
> console.log(rpn.calculate('1+2+3+4+5+√√81!'));
21
<script src='./rpn.js'></script>
<script>console.log(rpn.calculate('1+2+3+4+5+√√81!')); //21 </script>
rpn.calculate(expression)
rpn.calculate('1+2+3');
6
rpn.calculate('8!');
40320
rpn.calculate('√81');
9
rpn.calculate('√√81!');
6
rpn.calculate('1+2+3+4+5+√√81!');
21
rpn.calculate('1+2*3+4/5');
7.8
rpn.calculate('1+2^3');
9
rpn.calculate('1%+2^3');
8.01
rpn.calculate('15%+2^3');
8.15
rpn.infix2rpn(expression)
rpn.infix2rpn('1+2+3');
"1 2 + 3 +"
rpn.infix2rpn('8!');
"8 !"
rpn.infix2rpn('√81');
"81 √"
rpn.infix2rpn('√√81!');
"81 √ √ !"
rpn.infix2rpn('1+2+3+4+5+√√81!');
"1 2 + 3 + 4 + 5 + 81 √ √ ! +"
rpn.infix2rpn('1+2*3+4/5');
"1 2 3 * + 4 5 / +"
rpn.infix2rpn('1+2^3');
"1 2 3 ^ +"
rpn.infix2rpn('1%+2^3');
"1 % 2 3 ^ +"
rpn.infix2rpn('15%+2^3');
"15 % 2 3 ^ +"
rpn.rpnCalculate(expression)
rpn.rpnCalculate('1 2 + 3 +');
6
rpn.rpnCalculate('8 !');
40320
rpn.rpnCalculate('81 √');
9
rpn.rpnCalculate('81 √ √ !');
6
rpn.rpnCalculate('1 2 + 3 + 4 + 5 + 81 √ √ ! +');
21
rpn.rpnCalculate('1 2 3 * + 4 5 / +');
7.8
rpn.rpnCalculate('1 2 3 ^ +');
9
rpn.rpnCalculate('1 % 2 3 ^ +');
8.01
rpn.rpnCalculate('15 % 2 3 ^ +');
8.15
rpn.rpn2infix(expression)
rpn.rpn2infix('1 2 + 3 +');
"1 + 2 + 3"
rpn.rpn2infix('8 !');
"8!"
rpn.rpn2infix('81 √');
"√81"
rpn.rpn2infix('81 √ √ !');
"√√81!"
rpn.rpn2infix('1 2 + 3 + 4 + 5 + 81 √ √ ! +');
"1 + 2 + 3 + 4 + 5 + √√81!"
rpn.rpn2infix('1 2 3 * + 4 5 / +');
"1 + 2 * 3 + 4 / 5"
rpn.rpn2infix('1 2 3 ^ +');
"1 + 2 ^ 3"
rpn.rpn2infix('1 % 2 3 ^ +');
"1% + 2 ^ 3"
rpn.rpn2infix('15 % 2 3 ^ +');
"15% + 2 ^ 3"
mocha
- 如果有全局安装 'mocha' 工具,用命令直接启动测试
npm install
- 安装测试工具 mocha 和 测试报告工具 mochawesome
npm test
- 用命令会启动测试,完成后测试报告会显示在浏览器里