aspose file tools*
The moose likes Beginning Java and the fly likes parsing multiple digits and converting from infix to postfix Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "parsing multiple digits and converting from infix to postfix" Watch "parsing multiple digits and converting from infix to postfix" New topic
Author

parsing multiple digits and converting from infix to postfix

kyro gue
Greenhorn

Joined: Jan 05, 2010
Posts: 12
hi, currently i am doing a program on a calculator parser, i have successfully done the easy parts with + and - and is able to convert it from infix to postfix , however when converting from multiple digits there would be a problem, and the resulting postfix would be wrong as it reads 10 as 1 and 0 and not as 10


with this constructor for the above calcParser calc = new calcParser("1+3+4");
the output would be
13+4+
8.0

however with ("10+3+4") output would be
103+4+
7.0
which is wrong.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

I'd preserve spaces as well, or add them where needed. And why are you always popping in between? If I try to convert "+ - 10 2 3 + 4 5" (with brackets "+ (- 10 2 3) (+ 4 5)") it gives me "+ 10 2 3 - 4 5 +" where I expected the first + at the end. I think you should only pop an operator if you encounter an operator and the previous was a digit. Never mind. That's prefix, not infix.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

Here's your code slightly modified. The only differences are the added spaces and the combination of the equal if and else blocks.
10-2-3+4+5 now leads to 10 2 - 3 - 4 + 5 +, 1+3+4 to 1 3 + 4 + and 10+3+4 to 10 3 + 4 +.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: parsing multiple digits and converting from infix to postfix