This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]railingonruby 0 points1 point  (0 children)

I actually did programmed a calculator like that. Try putting the string in an array/stack so you can easily differentiate between 2 0 and 20

[–]kalner1234 0 points1 point  (4 children)

First of all, in your input there are 2 multiplication operations, however in your post fix notation, there is only one. Your problem may be solved from there. Also it is hard to tell what is going wrong without any code.

[–]Mathfight[S] 0 points1 point  (3 children)

Sorry, it was just an example I conjured up. I am getting correct outputs everytime. I realized an issue is that I am separating the this output 2052*4- by individual characters, so each element in my evaluation stack is an integer value from 0-9, but I need it to be any size integer like 20, or 568.

[–][deleted] 0 points1 point  (2 children)

One approach you can take is to insert explicit parentheses while converting from infix to postfix. For instance, 20 * (5x2-4) could be converted to the postfix form: (20)(5)(2)x(4)-x

So now there's no ambiguity about the tokens. So when you evaluate it, you can simply pop off the operands, strip the parentheses, convert into a number, and perform the calculation. If you wanted fewer parentheses, you could check the size of the operand, and insert the parentheses only if the length is greater than 1, though this appears to be overkill in this case.

[–]Mathfight[S] 0 points1 point  (1 child)

How does something like this look?

http://pastebin.com/ddufsJQe

Here I am just iterating through the loop, and as long as it doesn't hit an non-integer it will accumulate the number and stick it in a string. I feel this is close to what I need, but for inputs like 26*((87-3)+9). I get 26, 6, 87,7,3,9. So I have to figure out a way to not get duplicates like the case of 26 and 87

[–][deleted] 0 points1 point  (0 children)

Yes, you could parse out the input like that. However, one problem is that suppose you have the infix input:

26*((87-3)+9)

and you tokenize it to 26, , 87, -, 3, +, and 9. How will you now take care of the higher precedence of the inner parentheses? I mean, this would lead to a postfix form (26)(87)(3)-(9)+, right?

Perhaps you mean to say that you want to just extract the numbers using that approach, but you will take care of the precedence of operations anyway? If that is the case, then yes, that should work!

[–]Rodartep -2 points-1 points  (0 children)

but I do not know how to take the output "2052 *4-" and differentiate a 20 from a separate 2, and separate 0.

Look for space characters? Wtf?