Math Expression Evaluation

前端 未结 6 1178
感情败类
感情败类 2020-11-30 09:22

What is the best way to implement a python program that will take a string and will output its result according to operator precedence (for example: \"4+3*5\" will output 19

6条回答
  •  日久生厌
    2020-11-30 09:59

    I'm not terribly familiar with Python and any extremely Pythonic methods, but you could look at the Interpreter pattern, which is defined in the Gang of Four book. It's designed for processing a "language", and mathematical expressions do follow a particular language with rules. In fact, the example on Wikipedia is actually a Java implementation of a RPN calculator.

提交回复
热议问题