• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

read an expression and solve it...

 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic