Java Expression Parser & Calculator Shunting Yard Algorithm

前端 未结 4 1197
一个人的身影
一个人的身影 2021-01-23 19:29

So the task is to create our own parser for a expression calculator. For Example:

Input: 3+2*1-6/3 Output: 3

Input: 3++2 Output: Invalid Expression

Input

4条回答
  •  北海茫月
    2021-01-23 20:01

    Rather than re-invent the wheel you could use a parser generator such as JavaCC or antlr, which is specifically designed for this kind of task. This is a nice example of a simple expression parser and evaluator in a couple of dozen lines of JavaCC.

提交回复
热议问题