POSTFIX NOTATION USING STACKS... [EXAM IN 4 HRS HELP!!!]
when I call this method I get the answer in binary :S
HELP PLEASE MY EXAM IS WITHIN 4 HOURS....
this is supposed to use stacks in order to solve postfix notation...
u take a string push the values till u get an operator then pop 2 values perform the operation that u have then put the result back into stack...
String x = "23+"; // brings back 101 which is 5 in binary :S
Code :
public static int evaluate(String S){
StackObj stack = new StackObj(S.length());
int result = 0;
for(int i = 0; i < S.length(); i++){
switch (S.charAt(i)){
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':stack.push(new Integer (S.charAt(i))); break;
case '+':result = ((Integer)stack.pop()).intValue() + ((Integer)stack.pop()).intValue();
stack.push(new Integer (result)); break;
case '-':result = ((Integer)stack.pop()).intValue() - ((Integer)stack.pop()).intValue();
stack.push(new Integer (result)); break;
case '/':result = ((Integer)stack.pop()).intValue() / ((Integer)stack.pop()).intValue();
stack.push(new Integer (result)); break;
case '*':result = ((Integer)stack.pop()).intValue() * ((Integer)stack.pop()).intValue();
stack.push(new Integer (result)); break;
}
}
return ((Integer)stack.pop()).intValue();
}
Re: POSTFIX NOTATION USING STACKS... [EXAM IN 4 HRS HELP!!!]
If you want help, you'll have to provide an SSCCE that demonstrates the problem- for example, we have no idea what StackObj is doing.
You'll also want to avoid mentioning your deadlines like that. It makes you seem impatient, which will make people not want to help you. There are hundreds of posts here, each with an urgent user waiting on a reply, and your time is not more important than theirs.
Re: POSTFIX NOTATION USING STACKS... [EXAM IN 4 HRS HELP!!!]
You are absolutely right...
F*** it with my midterms...
now stackobj is a class that i made which works exactly like a stack but for objects...
THANK YOU...
Re: POSTFIX NOTATION USING STACKS... [EXAM IN 4 HRS HELP!!!]
Quote:
Originally Posted by
Medo Almasry
You are absolutely right...
F*** it with my midterms...
now stackobj is a class that i made which works exactly like a stack but for objects...
THANK YOU...
I'd be willing to run your code, but I can't do it without an SSCCE. It's up to you, but it's my bedtime soon.
Re: POSTFIX NOTATION USING STACKS... [EXAM IN 4 HRS HELP!!!]
Thnaks alot dude...
It doesn't matter no more...
but Thanks alot :D
have a good night though ;)
Re: POSTFIX NOTATION USING STACKS... [EXAM IN 4 HRS HELP!!!]
Why doesn't it matter? It doesn't look like a complicated problem to fix, if you just throw together the SSCCE.
Re: POSTFIX NOTATION USING STACKS... [EXAM IN 4 HRS HELP!!!]
here this is stackobj
Code Java:
public class StackObj {
private Object[] theStack;
private int maxSize;
private int top;
public StackObj(int s)
{
maxSize = s;
theStack = new Object[maxSize];
top = -1;
}
public void push(Object elem)
{
top++;
theStack[top] = elem;
}
public Object pop()
{
Object result = theStack[top];
top--;
return result;
}
public Object top()
{
return theStack[top];
}
public boolean isFull()
{
return (top == (maxSize-1) );
}
public boolean isEmpty()
{
return (top == -1);
}
public int size()
{
return (top+1);
}
}
Re: POSTFIX NOTATION USING STACKS... [EXAM IN 4 HRS HELP!!!]
You're storing the individual digits as chars. Chars are basically a subset of int, and their values map to ints that have little to do with the character they represent. So when you call:
new Integer (S.charAt(i))
What you're actually getting is the int that represents the char, not the actual char. It can be a bit confusing, so I recommend you write a little test program that prints out char values and the int values underlying them.
But to fix your problem, you want to convert the char to a String first.
Recommended reading: Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)