Here is java program to evaluate post fix expression using stack. Import java.io.*; class stack { int size; int item[]; int top; public stack() { size=100; item= new.
At this point, when I evaluate 12+, it gives me 99. Run For Cover Midi Files. I know that's partly due to the decimal value of the char, but I'm really stuck on how to do this. It's not partly, it's entirely due to it. Chars are not digits, and Java will never treat them as such. Probably the simplest way to do what you want is to make your stack an int stack and store the numeric value of the character. The question is: how do you do that?
You already know the decimal value, so how do you think you might convert 48 to 0, 49 to 1. Have a thunk about it. Hm, no thoughts at all?
You could make all your if.else's a switch statement. The code inside each if is identical except for the result so, instead of the above, why not create a result() method; maybe something like: private static int result(char operator, int i1, int i2) {. I leave you to work out the details.
Have you thought about how to handle negative numbers? Is that '-1 + 2' or '1 + -2', and how do you work it out? Just a few from the top of my head. Those 48 and 57 magic numbers are error-prone. There is a good reason for saying not to use numbers (except ±1 and 0). Your popAsDigit() method is much better. If I remember correctly, that is an exercise from the, which specifically uses one-digit numbers.
Pushing the int 16 back onto the stack will allow you to pop 16 later, still as an int. As for built-in classes, the best stack is, believe it or not. You can probably push and pop those values as ints. You would have to check the switch and find whether the chars +-*/ are being automatically cast to ints. Or more simply, if you use a stack of, and it works, then the cast is working all right. Wow, thanks for all the replies - I'm almost not sure where to start First thing, yes, I agree a switch statement would be much more useful here - I'll work on implementing that sometime when I get a chance. Just a question: Does the assignment includes writing a stack or is it ok to use the Java Stack implementation ()??
He wasn't clear with that, so I just wrote my own. I probably should have used the java implementation, but for this assignment I think I'll levae how I've done it so far. Those 48 and 57 magic numbers are error-prone.
This is where I'm uncomfortable. I really didn't want to write the method this way, but at the time I had no idea how to handle it. I was trying to figure out if it was possible to do the evaluation with a 'char' stack, but I didn't see that working out since as far as I'm aware a char can only handle one character at a time - i.e. What happens when I do 5+5 and I get 10? Can that be pushed as a single char?
But that depends what you are 'pop'ping the 16 as. If you pop it as an int, it will be recognised as an operand. • 7: stack now contains 7 • 9: Stack now 7 9 • +: Pop the two values, stack now empty. Add them and push the result. Stack = 16 • 5: Stack now 16 5 • - Pop the 5 and 16, subtract the two and push the result, so the stack is now 11. Note the way I had three pop operations in the code I posted earlier will handle that arithmetic incorrectly.
So, as Winston Gutkowski hints, you would have to change and receive the operator, then pop two values, not three, from the stack. As for 48 and 57, Winston Gutkowski has shown how you can subtract '0' from a char. Yes, you can do arithmetic with chars. Try this System.out.print((char)('5' - '0' + '5' - '0'));Unfortunately you won’t see much, since the char 10 (0x0a) is the line-feed character.
So you will get a new line and nothing else. It may be easier to work with a stack of.
Note that can take the int 123 and convert it to the Integer representing 123, which makes life much easier when using Collections. Of course, if you wrote your own stack, that is so much better. Campbell Ritchie wrote:Only noticed you are new today. Welcome Thanks! Definitely like the community here as opposed to other places I've been. Am I even on the right track to go through the output character by character?
For (int i = 0; i. Josh Theisen wrote:Am I even on the right track to go through the output character by character?Sounds fine to me; except I think I would call it 'input'. Forgive me, I'm definitely new to this kind of stuff.No sweat. We all started in the same place. From what I understand I should take the number (if I get one, as opposed to getting an operator), which is a char, convert it to an int and then push it into the stack?Yup. I think the whole thing between chars and ints and how to convert them is my biggest problem.But you've got it now, yes?
All Campbell was saying is that it's better to use ch - '0' than ch - 48 for your conversion and ch >= '0' && ch. Install Checkpoint Gaia Virtualbox For Mac. Winston Gutkowski wrote:But you've got it now, yes? All Campbell was saying is that it's better to use ch - '0' than ch - 48 for your conversion and ch >= '0' && ch = '0' && ch.