Git Product home page Git Product logo

rpn's Introduction

RPN (Reverse Polish Notation)

用逆波兰表达式计算四则混合运算

括号前如果有减法操作,那么,在括号(以及括号后面的乘除)结束后,就必须先计算这个减法。

网上的那些算法,都没有这个判断。

网上找到的一些算法,并不能很好地处理一些复杂的公式,比如“A-B*(A+B)*D-C”。

通过算法改良,当前可以处理的复杂度如下:

//以下样例已通过测试 //字符串 逆波兰表达式 计算结果

"10-2-3*4+4", //10 2 - 3 4 * - 4 + //0

"10-2-3*(4+5)-4-3", //10 2 - 3 4 5 + * - 4 - 3 - //-26

"1+5-6-(7+(1+2))*2+5", //1 5 + 6 - 7 1 2 + + 2 * - 5 - //-15

"1+2+3+4+5+6", //1 2 + 3 + 4 + 5 + 6 + //21

"1+2+(3+4)+5+6", //1 2 + 3 4 + + + 5 + 6 + //21

"1+(2+3)+(4+5)+6", //1 2 3 + + + 4 5 + + + 6 + //21

"1+23+45+6", //1 2 3 * 4 5 * 6 + + + //33

"1+2+(3*4)+5+6", //1 2 + 3 4 * + + 5 + 6 + //26

"1+23/45+6", //1 2 3 * 4 / 5 * 6 + + //14.5

"1+2+(3*4)/5+6", //1 2 + 3 4 * 5 / + 6 + //11.4

"1-2+(3*4)/5-6", //1 2 - 3 4 * 5 / + 6 - //-4.6

"1-2-(3*4)/5-6", //1 2 - 3 4 * 5 / - 6 - //-9.4

"1-(3*4)/5-6", //1 3 4 * 5 / - 6 - //-7.4

"1-444-2", //1 4 4 * 4 * - 2 - //-65

"1-(2-3*(4-5)-3)3-4-44*4", //1 2 3 4 5 - * - 3 - 3 * - 4 - 4 4 * 4 * - //-73

"1-(2-(4-5))34-4", //1 2 4 5 - - 3 * 4 * - 4 - //-39

"1-(2-(3*4)/2-3-4+5)/2-1+3-4", //1 2 3 4 * 2 / - 3 - 4 - 5 + 2 / - 1 - 3 + 4 - //2

"1-(2-(3*4)/5-3-4+5)/7-1+3-4", //1 2 3 4 * 5 / - 3 - 4 - 5 + 7 / - 1 - 3 + 4 - //-0.65714285714286

"1-(2-(3*4)/5-(3-4)*3+5)/7-1+3-4", //1 2 3 4 * 5 / - 3 4 - 3 * - 5 + 7 / - 1 - 3 + 4 - //-2.0857142857143

"123+543/3-3423-43-21-(15+2843-2*(12+32)/2)*2-51+60", //123 543 3 / 34 23 * - 43 - 21 - 15 28 43 * 2 12 32 + * 2 / - + 2 * - 51 - 60 + + //-2883

"123+543/3/2-34233-43-21-(15+2843-24*(12+32)/2)25-51+60", //123 543 3 / 2 / 34 23 * 3 * - 43 - 21 - 15 28 43 * 2 4 * 12 32 + * 2 / - + 2 * 5 * - 51 - 60 + + //-12617.5

"123+543/3/2-34233-43/21-(15+843-24*(12+32)/2)25-51*2-6", //123 543 3 / 2 / 34 23 * 3 * - 43 21 / - 15 8 43 * 2 4 * 12 32 + * 2 / - + 2 * 5 * - 51 2 * - 6 - + //-4072.5476190476

"1+4/2-34-5-6-(7+83-2*(1+2)/3)*2-5+6", //1 4 2 / 3 4 * - 5 - 6 - 7 8 3 * 2 1 2 + * 3 / - + 2 * - 5 - 6 + + //-77

rpn's People

Contributors

luciferstarss avatar

Watchers

 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.