Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hello,
I am writing a calculator in C# and am having problems setting the operator precedence.
For example if the user enters 8*8-9 the program gives an answer of -8 instead of 55.
My program uses two stacks to store the numbers and operators and then evaluate (trying to use the shunting yard idea).
Does anyone know how to set the operator precedence or search through a stack and pick the operator with the highest precedence?
I can send the code I have if it is needed,
Regards,
Andrew
Posted
Updated 24-Feb-21 7:30am

This is an interesting problem. I don't understand the full details but I might have a bash at implementing this myself. Anyway this is what I have garnered, referencing this heavily:

http://en.wikipedia.org/wiki/Shunting_yard_algorithm[^]

"My program uses two stacks to store the numbers and operators and then evaluate (trying to use the shunting yard idea)."

If I understand correctly, this is wrong (apologies if I misunderstand). The purpose of the shunting yard is to re-write a formula so that it appear in reverse Polish notation where
3 2 +
adds 3 to 2 (in general Operand2 Operand1 Operator where the operands themselves can be evaluated bits of the function). Your formula 8*8-9 --> 9 - 8 8 * I think. Reverse Polish notation is very confusing, I have to use Polish, then reverse it if you get my meaning.

The shunting algorithm has one stack and one queue. The operator stack is really "temporary", when the algorithm is finished it should be empty. Everything should end up in the output queue rather than just the values. If you follow the algorithm correctly you get a RPN formula in the output queue without any extra work. You shouldn't go through the output queue and operator stack at the end and work through the precedences. Even if you got this working (and I don't think it is possible as you described it) it would be slower than the proper algorithm.

The algorithm is detailed in the wikipedia page, but simplistically:

  1. The Next term, if it is a number push it onto the output queue.
  2. If the next term (an operator) has a higher precedence than the one on the operator stack() or the op stack is empty, push it to the stack. Otherwise pop the operator stack into the output queue and push the term into the operator stack
  3. Repeat until there are no more terms: pop each item in the operator stack into the output queue

Obviously this doesn't deal with brackets (unlike the wiki page).


[edit: I have started to implement this alogrithm, I thought the ouptut was a stack, it isn't, it is a queue. I have corrected this.]
 
Share this answer
 
v2
That is odd, it ought to perform the * before the - as a matter of course, and that is the order in which they are written as well.

Have you tried using parentheses?

Here[^] may help.
 
Share this answer
 
Comments
Keith Barrow 12-Oct-10 14:00pm    
Dave, I think you have misunderstood the question, I definately did at first. The OP isn't calculating 8*8-9 in code, he is trying to take a text formula and parse it so he can evaluate the operator precedences, then calculate the result. The idead being that the user can enter any string calculation and the alogrithm will blatt out the correct answer. See my reply below, it is an interesting problem to solve.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900