• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Infix to PostFix

 
Ranch Hand
Posts: 119
Mac Eclipse IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A String variable references a mathematical equation. Within a method called convertToPostFix(String s), I am analyzing each character in the equation as follows:

Within this method, I loop through the entire string and push digits to the stack until an operator is found, if no operator is currently in the stack, push(operator). If a right parenth is found, pop all the elements in the stack to an output string. Alternatively, if an operator is found to exist in the stack already, I check the stack precedence with the following method:


I'm really not sure if I'm doing this correctly, or if I am way off. My output is incorrect and looks something like this : (1053
Is this the correct use of the stack?

 
Saloon Keeper
Posts: 15510
363
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's really hard to follow the logic in your code. The algorithm could probably be simpler. Also, your switches don't make much sense. One always returns true, and the other always returns false. Not much point to a switch statement then...

Why are you pushing digits on the stack? You should tokenize numbers, and add them to the output queue directly. The stack is for operators only. Take a look at http://en.wikipedia.org/wiki/Shunting-yard_algorithm
 
Derek Smiths
Ranch Hand
Posts: 119
Mac Eclipse IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Stephan, that source makes it much, much clearer the purpose of the stack for this operation. Great resource. Clearly I was way off, on how to use this. I'll update this code shortly.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not a “beginning” topic: moving discussion.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a small piece of code that will make reading your input *much* easier. It breaks up your input string in tokens that are either operators, parentheses, or other values.
operatorTokens is a collection of Strings representing your operators.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic