Math Expression Evaluation

前端 未结 6 1179
感情败类
感情败类 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条回答
  •  旧时难觅i
    2020-11-30 09:47

    If you're "academically interested", you want to learn about how to write a parser with operator precedence.

    Simple Top-Down Parsing in Python is a nice article that builds an example parser to do exactly what you want to do: Evaluate mathematical expressions.

    I can highly recommend having a go at writing your own first parser -- it's one of those "ah, that's how that works" moments!

提交回复
热议问题