How can I modify my Shunting-Yard Algorithm so it accepts unary operators?

后端 未结 6 741
眼角桃花
眼角桃花 2020-12-09 16:25

I\'ve been working on implementing the Shunting-Yard Algorithm in JavaScript for class.

Here is my work so far:

var userInput = prompt(\"Enter in a m         


        
6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-09 16:33

    When I needed to support this, I did this in an intermediate stage. I started by generating a list of all expression lexemes, then used helper functions to extract operators and operands and the "get operand" function simply consumed two lexemes whenever it saw a unary operator.

    It really helps if you use another character to signify "unary minus", though.

提交回复
热议问题