1
votes

I'm struggling getting the pseudo code for this.

Scan string left to right for each char If operand add it to string Else if operator add to stack ....

i'm struggling on how to handle ( )s

4

4 Answers

0
votes

( goes on to the stack, then when you get to ) you pop from the stack until you find a (.

Wikipedia has a more detailed description of the algorithm, supporting functions as well as operators.

0
votes
  1. Scan input string from left to right character by character.
  2. If the character is an operand, put it into output stack.
  3. If the character is an operator and operator's stack is empty, push operator into operators' stack.
  4. If the operator's stack is not empty, there may be following possibilities.
  5. If the precedence of scanned operator is greater than the top most operator of operator's stack, push this operator into operand's stack.
  6. If the precedence of scanned operator is less than or equal to the top most operator of operator's stack, pop the operators from operand's stack until we find a low precedence operator than the scanned character. Never pop out ( '(' ) or ( ')' ) whatever may be the precedence level of scanned character.
  7. If the character is opening round bracket ( '(' ), push it into operator's stack.
  8. If the character is closing round bracket ( ')' ), pop out operators from operator's stack until we find an opening bracket ('(' ).
  9. Now pop out all the remaining operators from the operator's stack and push into output stack.
-2
votes

I am a bit rusty at this, but when you encounter a '(' , you push it onto the stack because it has the highest precedence. I cant remember what to do when you encounter ')', but i think it goes on the stack as well because its the highest precedence.