Does this test case break the shunting yard algorithm? "1*2-3/4+5*6-7*8+9/10"

397 Views Asked by At

I have tried numerous times to solve the following using the shunting yard algorithm: 1*2-3/4+5*6-7*8+9/10

Infix Notation:

"1*2-3/4+5*6-7*8+9/10"

Postfix Notation:

[1,2,*,3,4,/,5,6,*,7,8,*,9,10,/,+,-,+,-]

The correct answer is -24.

Every time I solve the postfix notation version of this I get 28.

I have yet to find a working RPN calculator online ... So I have turned to stack overflow.

1

There are 1 best solutions below

0
user16552100 On

I am working on my own Shunting Yard algorithm, and so this might not be 100% correct, but I what Im getting is [ 1, 2, *, 3, 4, /, -, 5, 6, *, +, 7, 8, *, -, 9, 10, /, +] The Wikipedia for Shunting Yard Algorithm gives the pseudo code for this parsing, and I dont think I could explain it better. As some alredy said, you might want to look into how precedence and associativity affect parsing.