Writing a simple equation parser

前端 未结 10 1177
-上瘾入骨i
-上瘾入骨i 2020-12-01 03:56

What sorts of algorithms would be used to do this (as in, this is a string, and I want to find the answer):

((5 + (3 + (7 * 2))) - (8 * 9)) / 72
10条回答
  •  鱼传尺愫
    2020-12-01 04:31

    Yes the algorithm is Shunting yard algorithm but if you want to implement I suggest you python and it's compiler package

    import compiler
    equation = "((5 + (3 + (7 * 2))) - (8 * 9)) / 72"
    parsed = compiler.parse( equation )
    
    print parsed
    

    You can also evaluate this expression with built-in eval() method

    print eval("5 + (4./3) * 9") // 17
    

提交回复
热议问题