EVALUATION OF POSTFIX NOTATION
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);
}
}
Ok so this is a class called STACKOBJ that is a stack of OBJECTS...
THIS IS THE CODE...
Code :
[CENTER]public static int evaluatepostfix(String S){
StackObj temp = new StackObj(S.length());
for(int i = 0; i < S.length(); i++){
if(((Integer.parseInt(S.charAt(i) + "")) >= 1) && ((Integer.parseInt(S.charAt(i) + "")) <= 9)){
temp.push(new Integer (Integer.parseInt((S.charAt(i)+""))));
break;
}else{
int op1 = ((Integer)temp.pop()).intValue();
int op2 = ((Integer)temp.pop()).intValue();
switch (S.charAt(i)){
case '+': temp.push(new Integer ((op2) + (op1))); break;
case '-': temp.push(new Integer ((op2) - (op1))); break;
case '*': temp.push(new Integer ((op2) * (op1))); break;
case '/': temp.push(new Integer ((op2) / (op1))); break;
}
}
}
return ((Integer)temp.pop()).intValue();
}[/CENTER]
Apparently if I try to run System.out.print(evaluatepostfix("123*+9-")); the output is 1 :S
HELP???
Re: EVALUATION OF POSTFIX NOTATION
Code java:
if(((Integer.parseInt(S.charAt(i) + "")) >= 1) && ((Integer.parseInt(S.charAt(i) + "")) <= 9)){
temp.push(new Integer (Integer.parseInt((S.charAt(i)+""))));
break; // ?????
Think very carefully what that statement does. When you fix that you will have plenty of other problems to fix.
Re: EVALUATION OF POSTFIX NOTATION
all this means is that if the character is between 1 and 9 push it into the stack...
Re: EVALUATION OF POSTFIX NOTATION
No. I'm talking about the break statement. What does that do?
Re: EVALUATION OF POSTFIX NOTATION
I guess this is what happens when you spend hours watching the big bang theory -.- :D
THANKS ALOT MAN :D
I missed out that one :D
Re: EVALUATION OF POSTFIX NOTATION
Your next assignment should you choose to accept is to write a game for Rock, Paper, Scissors, Lizard, Spock.
Re: EVALUATION OF POSTFIX NOTATION
I shall respectfully reject the offer because right now am working on databases (stacks, queues, linked lists, trees)...
and regarding games, next semester I shall be taking GUI and most probably I will be doing Entanglement *its a game*...
Thanks again for the offer and if you have a source for questions that could make me better... a website or a book... that would be great...
If you are wondering, I'm a computer Programmer "YET TO BE :p" but am just having a burnout here...