• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Infix to PostFix

 
Ranch Hand
Posts: 119
Mac Eclipse IDE VI Editor
  • Mark post as helpful
  • send pies
  • 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: 11472
247
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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
  • 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: 68084
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a “beginning” topic: moving discussion.
 
Stephan van Hulst
Saloon Keeper
Posts: 11472
247
  • Mark post as helpful
  • send pies
  • 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.
 
Ruth Stout was famous for gardening naked. Just like this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!