Code Monkey home page Code Monkey logo

huihuipan / rpn.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tairraos/rpn.js

0.0 0.0 0.0 29 KB

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

License: MIT License

Shell 0.64% JavaScript 99.36%

rpn.js's Introduction

逆波兰表达式工具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.

rpn.js's People

Contributors

tairraos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.