caichaoqing / rpn.js

Reverse Polish Notation tools. Translate infix expression to reverse polish notation (postfix expression) and Calculate reverse polish notation. support + - * / ^ % ! √ ( ). 逆波兰表达式工具,可以把普通表达式转换为逆波兰表达式,还可以计算逆波兰表达式。支持+ - * / ^ % ! √ ( )。

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

逆波兰表达式工具Reverse Polish Notation tool

1. 介绍Intro

逆波兰表达式工具Reverse polish notation tool, 普通表达式 和 逆波兰表达式 互相转换 translate between infix expression and reverse polish notation, 计算 普通表达式 和 逆波兰表达式calculate infix expression and reverse polish notation, 支持 + - * / ^ % ! √ ( )support + - * / ^ % ! √ ( ).

可以在浏览器里使用,也可以当作Node的模块使用the tool is both can work in browser and work as a node module.


2. 用法Usage

2.1 Node

$npm install jsrpn
$node
> let rpn = require('jsrpn');
> console.log(rpn.calculate('1+2+3+4+5+√√81!'));
21

2.2 Browser

<script src='./rpn.js'></script><script>console.log(rpn.calculate('1+2+3+4+5+√√81!')); //21 </script>

3. 接口Interface

3.1 计算算式表达式Calculate infix expression

rpn.calculate(expression)

范例Example
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

3.2 转换算式表达式为逆波兰表达式Translate infix expression to reverse polish notation

rpn.infix2rpn(expression)

范例Example

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 ^ +"

3.3 计算逆波兰表达式Calculate reverse polish notation

rpn.rpnCalculate(expression)

范例Example

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

3.4 转换逆波兰表达式为算式表达式Translate reverse polish notation to infix expression

rpn.rpn2infix(expression)

范例Example

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"

4. 调试及测试Debug and testing

  • mocha
  • 如果有全局安装 'mocha' 工具,用命令直接启动测试If 'mocha' already installed as a global tool, use command to start testing
  • npm install
  • 安装测试工具 mocha 和 测试报告工具 mochawesomeUse this command to install test tool 'mocha' and test report tool 'mochawesome'
  • npm test
  • 用命令会启动测试,完成后测试报告会显示在浏览器里Use this command to start testing, and the test report will open in browser after test finish.

About

Reverse Polish Notation tools. Translate infix expression to reverse polish notation (postfix expression) and Calculate reverse polish notation. support + - * / ^ % ! √ ( ). 逆波兰表达式工具,可以把普通表达式转换为逆波兰表达式,还可以计算逆波兰表达式。支持+ - * / ^ % ! √ ( )。

License:MIT License


Languages

Language:JavaScript 99.4%Language:Shell 0.6%