jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Infix to Postfix conversion. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Infix to Postfix conversion." Watch "Infix to Postfix conversion." New topic
Author

Infix to Postfix conversion.

Ranjeet Deshmukh
Ranch Hand

Joined: Jan 18, 2013
Posts: 35

Hi All,

I have string "(1+2*3/4+7)". Now I want to postfix it and store the result on the stack for further calculation.


~R@njeet.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3994
    
  18

What stack? What result? Please be a bit more clear.

Also, show us what you've tried so far.


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Ranjeet Deshmukh
Ranch Hand

Joined: Jan 18, 2013
Posts: 35



And the output is :- [(, 1, +, 2, *, 3, /, 4, +, 7, )]

Now I want to calculate the answer. For this I think doing postfix is more suitable. If anyone have better idea then please mention it.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8661
    
  23

Ranjeet Deshmukh wrote:And the output is :- [(, 1, +, 2, *, 3, /, 4, +, 7, )
Now I want to calculate the answer. For this I think doing postfix is more suitable.

Well postfix (or RPN) is definitely the easiest to deal with; but all you've done so far is convert String.toCharArray() to a Stack, which isn't likely to help you much.

Also: What about values that have more than one digit?

My advice: Split your String up into proper operators; and then have a look at the Shunting-yard algorithm.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: Infix to Postfix conversion.
 
It's not a secret anymore!