# read an expression and solve it...

Justin Fox
Ranch Hand
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....

justin

Justin Fox
Ranch Hand
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
Posts: 50654
83
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
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()
{
}

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
Posts: 50654
83
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
Posts: 50654
83