This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Stacks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Stacks" Watch "Stacks" New topic
Author

Stacks

Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok here is the only expression that is giving me a problem...

((2^((4+(7*1))/2))/((4^0)+6))

ok here is my Main program, and the two stack classes, Stack and charStack







ok now the expression above, it will be evaluated like so...

((2^((4+(7*1))/2))/((4^0)+6))

so it will start iterating in the for loop,

and first it will push 2 to num stack
then it will push ^ to opstack
then it will push 4 to num stack
then it will push + to opstack
then it will push 7 to num stack
then it will push * to opstack
then it will push 1 to numStack
the then you hit a ')'

so you pop the first two integers from numstack,
and then pop the first operator from opstack,
then perform the indicated operation from left to right...

so it would calculate 7*1, then push 7 onto the stack.
another ')' so it would calculate 4+7, then push 11 onto the Numstack.

** remember that the operators are being popped off as we go**

then it would push '/' onto the Opstack
then it would push 2 onto the NumStack

but we have another ')' so is would calculate 11/2, and push 5 onto the
NumStack.

yet another ')' so it would calculate 2^5 , and push 32 onto the NumStack.

so at this point we have

NumStack Contents: 32
OpStack Contents: nothing

NumStack's top: 0
OpStacks's top: -1

so technically OpStack is empty right?... will this affect the rest of
the evaluation of the expression?

because i can get my application to evaluate

((2^((4+(7*1))/2))) and it returns 32

and when i do ((4^0)+6) it returns 7

but it wont calculate ((2^((4+(7*1))/2))/((4^0)+6))

this is the output:




please help me if you can, this is my last stick..

thank you,
Justin


You down with OOP? Yeah you know me!
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
i need away to deal with exponents of negative and zero..

i tried doing if statements in the switch

like

case '^':
int A = 0;

if(one >0)
A = two;

if(one < 0)
A = 0;

if(one == 0)
A=1;

for(int count=1; count<one; count++)
A *= two;

NumStack.push(A);





break;

but it wont recognize...

(4^0)

but it will recognize

(4^(2-2))

and when i do (6+(4^0))

the result comes out as 1,296??

thats outrageous....

but when i do ((4^0)+6)

the out put is 6...

ima perdy confused... and pretty much lost..


I would appreciate any help...

thx
Justin
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
dude seriously, i really need help on this,

can someone plz help me out here....
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Patience, young grasshopper. Your earlier post is time stamped at 10:30 pm. I am willing to bet that many of our visitors and certainly the owners of this website are in bed. In addition, you have posted a lot of code and additional information, so it takes a little while for us to figure out what is going on.

With that said, let me see what I can do to help you out:
so technically OpStack is empty right?... will this affect the rest of
the evaluation of the expression?

Yes, this is totally correct. In fact, your analysis of what SHOULD happens looks fine to me. So now we need to find out what your program ACTUALLY DOES.
because i can get my application to evaluate

((2^((4+(7*1))/2))) and it returns 32

and when i do ((4^0)+6) it returns 7

but it wont calculate ((2^((4+(7*1))/2))/((4^0)+6))

this is the output:

Okay, this is a good place to start in tracking down the problem. To continue, you need to use some debugging techniques. A visual debugger will help a lot if you know how to use it. However, simply adding System.out.println() calls (abbreviated to SOP hereafter) in your code will help track down what is happening. To start, you should print out simple messages like "in case '+'" so that you can trace the execution of the program. From there, you should print out values of the variables to ensure that they are what you expect.

Debugging is a learned skill. I am not willing to do it for you since I think it is essential that you learn how to do it on your own.

i need away to deal with exponents of negative and zero..

Why are you writing your own code to do exponents? I strongly suggest that you use the Math.pow() method which takes care of all of these details for you. Unfortunately, this method uses doubles rather than ints, so you will need to learn how to perform a type cast to convert the result back to an int. Look in your textbook for information about how to do this.

If you are not allowed to use the Math.pow() method for some reason, I strongly suggest that you create a separate method for this calculation. In fact, your code could be cleaned up a lot by separating things out into short methods that perform a single, well-defined class. It takes some experience and practice to be able to do this well, so I encourage you to try it as much as possible.

I hope these tips help you along the way. If you don't understand any of my suggestions, please post back and ask for clarifications. Also describe what you tried and I will be glad to continue to help you.

Keep Coding!

Layne


Java API Documentation
The Java Tutorial
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
Patience, young grasshopper.
Agree. Some of us do have to do a little work occasionally.

Agree. I have tested your two stack classes, which both work. You do realise you could have uses the same class for int and char; char is just a number? All you have to do is cast it back when it is returned.

Your main method is far far too long. You oughtn't really to have the workings-out in the main method, but move them into another method, call it workOut(). So you can abbreviate your main method toIf you move evreything into a workOut() method, it will still be too long. See if you can't break it into several smaller methods, say readFromScreen() and calculate().


I have already told you there is a much quicker way to get a char[] array from a String.

I have also told you there is a quicker way than a switch to put the values into your stacks. Look at what I told you before. It is much more reliable, avoiding errors, including the error of pushing 9 numerical characters onto the Stack and forgetting 0.

Simplify your stack loading, then make the corrections I have suggested.

Then, follow Layne Lund's suggestions about debugging with messages.
If you can work an IDE (I would recommend Eclipse, which I use most of the time myself, but NetBeans is very good too), find the debugger, and you can follow your app through line by line and inspect the variables at every stage. Otherwise the multiple System.out.println calls will work wonders.

Unless you absolutely have to use powers, leave out the power calculation until you have got the add and subtract working.

I think we have told you enough to keep you quiet for at least 10 minutes.
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok, i appreciate all the replies, but i figured out what was wrong...

i didnt switch the '0':


so it would recognize 4^(4-4)

but not 4^0, now everything works lol, im a goober..

thx again
Justin
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
the reason we can't use math.pow()is because that is only supposed to be

used for numbers raised to a non-integer power.. like 2 ^2.3

and for the switch statement, he said to use that..

...

thx Justin...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
You got it working in the end. That's what matters. Well done.

And you will remember 0 in future.
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
yep, thx for the help again!

Justin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stacks
 
Similar Threads
Problem with return statement....
read an expression and solve it...
stack generic
unable to detect and correct d error..plss help
stack