aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
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: 32

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: 3647
    
  17

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

Also, show us what you've tried so far.
Ranjeet Deshmukh
Ranch Hand

Joined: Jan 18, 2013
Posts: 32



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: 8008
    
  22

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Infix to Postfix conversion.