wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes read an expression and solve it... 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 "read an expression and solve it..." Watch "read an expression and solve it..." New topic
Author

read an expression and solve it...

Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok my program is supposed to read an expression, like so...

(64%(9/(6-3)))

but im having some problems...



here is my stack class...



and i have one for a char stack too.... same .Size() method...

but say i input (9/(56-3))

the number stack would hold 9,56,3

the Operator stack would hold /,-

but when it hits that first ')'

it pops, the first two numbers, and the first operator and does
the indicated operation, which is.

56-3 = 53, then pushes 53 onto the number stack... so now the stack hold

numberstack = 9,53

Operator stack = /

but my output for some reason, is

numstack = 9,1

and operator is empty...


im lost now, i tried to fix it, but yeah....

please help..

justin


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

Joined: Jan 24, 2006
Posts: 802
lol nvm, the prof gave us that, and didn't put a space between the 5 and 6...

so its supposed to be (9/(5 6-3)).

it's supposed to test to see if our program catches the missing operator...

i have my if statement, but for somreason my .Size() method doesn't work,

i return top, which at the end of the evaluation is 1 in the num stack,

so it should print "Missing operator!"

but it doesn't..

im not sure but i think im supposed to use this

like


see that should work, but for some odd reason it doesn't..

help,
Justin
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
A bit difficult to see exactly what is going on because you don't provide the charStack class (which by the way ought to be CharStack).
Were you really told to use an array and make a Stack of restricted size?

Your getSize() method is returning the top item because that is what you have told it to do.

CR
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
i know, and i have

if (NumStack.Size() > 1)
System.out.println("Missing Operator!");

and when i do...

(9/(5 6 - 3))

at the end of the evaluation of the expression

numstack contains... 9 and 1
and the OpStack contains nothing.

so it would be like this
____ ____
| 9 || 1 | <---- the contents of NumStack
0 1 <---- the Index of the NumStack

so if i said..

public int Size()
{
return top+1;
}

so when it comes to the end of the program and tests the stacks...

it should catch that NumStack.size() is infact greater than 1, and print

out missing operator, but doesn't...

thanks,
Justin
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
Well, it is still not a getSize() method if it prints anything out. I still can't see how your app is supposed to work. Where is the CharStack class?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
I have had a look at your stacks.
You can get a String to an array of chars much more easily than you have done. Look through the String class API.
Your switch method to fill the stacks is unnecessary. All you have to do is use the methods in the Character class to find out whether your char represents a digit, and use that to decide which Stack to put it on. You can probably also find out whether a particular char is whitespace and "lose" it.

But I think your stack will only work for single digits. If you put 9 56 3 in, you won't get 3 56 9 back. You will get 3 6 5 9. So you put three numbers in and get four back. It will make all your arithmetic wrong, and will also result in your still having items in your stack when you have finished.

Think what you can do with your char[] array if you find two consecutive digits.

You are going to have to take a different approach. Get your number and operator stacks, and test them. Give them push peek and pop methods, and getCount isFull and isEmpty methods. Give each of them a toString() method which prints out their details, and print method, which can be very simple:Give each of them main methods, and push and peek and pop numbers/operators. Go through them until you are happy they are working. Then get back to the class you are working on.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: read an expression and solve it...
 
Similar Threads
Problems searching through a list
Problem with return statement....
stack generic
stack
Stacks