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

后端 未结 6 699
眼角桃花
眼角桃花 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条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-09 16:48

    my suggestion is this. don't handle the '-' as an arithmetic operator. treat it as a 'sign' operator. or treat it as if it's a part of the whole operand (i.e. its sign). what i mean is that everytime you encounter '-', you just have to multiply the operand after it by -1, then proceed to read the next token. :) i hope that helps. just a simple thought...

提交回复
热议问题