• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

parsing multiple digits and converting from infix to postfix

 
kyro gue
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 +.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic