Home arrow static arrow Java Programming [Archive] - RPN stack POP
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - RPN stack POP
This topic has 9 replies on 1 page.

Posts:357
Registered: 8/5/04
RPN stack POP  
Aug 7, 2004 8:14 PM



 
I can't get this ******* approach:::

Why does it do
switch(token.charAt(0))

and then
 case '*' case '+' case '-' 


what the **** is it for?

[code]
import java.util.*;
import type.lang.*;

public class RPN {

public RPN() {}

public void process(String s) {
System.out.print("Processing RPN |" + s +"|");
StringTokenizer st = new StringTokenizer(s);
Stack stack = new Stack();
while(st.hasMoreTokens()) {
String token = st.nextToken();
try {
stack.push(new Integer(token));
IO.println("NO. " + token);
IO.println("SEE " +token.charAt(0));
}catch(NumberFormatException e) {
int v1, v2;
switch(token.charAt(0)){ /// HERE what is this for?

case '*': //and this stupid cases?
v2 = ((Integer)stack.pop()).intValue();
v1 = ((Integer)stack.pop()).intValue();
stack.push(new Integer(v1*v2));
break;
case '+':
v2 = ((Integer)stack.pop()).intValue();
v1 = ((Integer)stack.pop()).intValue();
stack.push(new Integer(v1+v2));
break;
case '-':
v2 = ((Integer)stack.pop()).intValue();
v1 = ((Integer)stack.pop()).intValue();
stack.push(new Integer(v1-v2));
break;
default:
System.out.println("Command " + token + " not understood");
}
}
}
System.out.println("Value on stack is " + stack.pop());
}

public static void main(String[] args) {

RPN rpn = new RPN();
rpn.process("2 3 + 4 - 2 *");
rpn.process("123 6 - 5 +");
}
}

 

Posts:12,831
Registered: 2/22/00
Re: RPN stack POP  
Aug 7, 2004 9:34 PM (reply 1 of 9)



 
http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#35518
 

Posts:357
Registered: 8/5/04
Re: RPN stack POP  
Aug 7, 2004 9:37 PM (reply 2 of 9)



 
ahh thanks for that but the problem is

switch(token.charAt(0))


in here the "token" at nos... 2 ,3 , 4 ,5 etc

now what the use of calling token.charAt(0) ?? on a single digit no.?
and '*' and '+' doesn't make any sense
 

Posts:357
Registered: 8/5/04
Re: RPN stack POP  
Aug 7, 2004 9:39 PM (reply 3 of 9)



 
why would any one call token.charAt(0) on string 5 ? or 2? or 3? whats the use of that?
 

Posts:357
Registered: 8/5/04
Re: RPN stack POP  
Aug 7, 2004 9:54 PM (reply 4 of 9)



 
had the tokens been something like lets say

+5 or *3 or -2 then its okay to do

Switch(token.charAt(0))

then it will be either of '*' or '-' or '+' and the corresponding case will be executed but her the tokens are

5 , 4 , 3 , 2 ?? ?
 

Posts:1,135
Registered: 1/16/04
Re: RPN stack POP  
Aug 7, 2004 11:06 PM (reply 5 of 9)



 
why would any one call token.charAt(0) on string 5 ?
or 2? or 3? whats the use of that?

Okay to help you understand, lets break down on of the "RPN" strings from the "main" method...
"123 6 - 5 +"

The tokenizer runs and the first "token" is "123"
So we check the first character of "123" and find it is "1"...
Then we check to see if "1" matches one of our switch cases (" + ", " - " , " * ") which it does not...
Next, we grab the next token which is "6" and we check the first character again which is now "6"
And Then we check to see if "6" matches one of our switch cases (" + ", " - " , " * ") which again does not...

Now remember these numbers are going into ( being "pushed" into ) a Stack, so right now we have two in our Stack
123 and 6...

So let continue...
Now we grab the next token "-" and check the first character which is "-"... and you know what next, right ?
Of course, we check to see if "-" matches one of our switch cases (" + ", " - " , " * ") which this time it does...

Now looking at that switch statement for " - " we see that we grab ( "pop" ) the last two values "pushed" in our stack ( "123" and "6") and subtract them... The result "117" get pushed in to our Stack...

Since we "poped" two numbers ( "123" and "6" ) and they were the only ones, the Stack was emptied, and after doing the subtraction, we pushed the result "117" into the Stack
So our Stack now holds only "117"

So what next, well once again we grab the next token "5" and check the first character "5"...
and once again we check to see if it matches one of our cases (" + ", " - " , " * "), and it doesn't
so "5" gets "pushed" into the stack...

Now our stack has "117" and "5"...

What do you think is going to happen next ???

Right, we get our next token which is "" and check the first character ""
and check to see if " + " matches one of our switch statements (" + ", " - " , " * "), yup... it does...

Looking at the switch statement for "+" we see that we are to "pop" the last two numbers "pushed" into our Stack ( "117" and "5") and add them together... which is "122" and "push" that into our Stack...

So now our Stack holds "122" only... What happens next...

We try to grab the next token, but there are no more, so empty handed we return to our switch statements and obviously we can not match any of these cases " + ", " - " , " * " so we end up defaulting to the "default" case which states "pop" that last values "pushed" into our Stack, in this case "122" and display our result...

Now that is the relative play by play,
But this class design leaves alot to be desired because we are using an thrown exception to bring us to our switch statement, while it works, it not pretty... But that another matter...

That said, I included several System.out.println() statements in your code so that you can see some of the process...

import java.util.*;public class RPN {	   public RPN() {}	   public void process(String s) {	   		  System.out.println("Processing RPN |" + s +"|");	   		  StringTokenizer st = new StringTokenizer(s);	   		  Stack stack = new Stack();	   		  while(st.hasMoreTokens()) {	   		  String token = st.nextToken();	   		  try {	   		  	  stack.push(new Integer(token));	   			  System.out.println("New Number Processed " + token);     			  }	   		catch(NumberFormatException e) {	   		int v1, v2;	   		switch(token.charAt(0)){ /// HERE what is this for?	   		case '*': //and this stupid cases?	   			 v2 = ((Integer)stack.pop()).intValue();	   			 v1 = ((Integer)stack.pop()).intValue();	   			 System.out.println("Multiplying " + v1 + " * " + v2);	   			 System.out.println("Product Processed " + (v1*v2));				 stack.push(new Integer(v1*v2));	   			 break;	   		case '+':	   			 v2 = ((Integer)stack.pop()).intValue();				 v1 = ((Integer)stack.pop()).intValue();				 System.out.println("Adding " + v1 + " + " + v2);				 System.out.println("Sum Processed " + (v1+v2));	   			 stack.push(new Integer(v1+v2));	   			 break;	   		case '-':	   			 v2 = ((Integer)stack.pop()).intValue();	   			 v1 = ((Integer)stack.pop()).intValue();	   			 System.out.println("Subtracting " + v1 + " - " + v2);	   			 System.out.println("Difference Processed " + (v1-v2));	   			 stack.push(new Integer(v1-v2));	   			 break;	   		default:	   		System.out.println("Command " + token + " not understood");}}}	   		System.out.println("Value on stack is " + stack.pop());		}	   public static void main(String[] args) {	   		  RPN rpn = new RPN();	   		  rpn.process("2 3 + 4 - 2 *");	   		  rpn.process("123 6 - 5 +");  		}}


When it hit you, sit back and enjoy that lucid feeling of enlightenment...

Have fun...

- MaxxDmg...

- ' He who never sleeps... '
 

Posts:357
Registered: 8/5/04
Re: RPN stack POP  
Aug 8, 2004 4:09 AM (reply 6 of 9)



 
Thanks!
 

Posts:826
Registered: 7/1/03
Re: RPN stack POP  
Aug 8, 2004 4:28 AM (reply 7 of 9)



 
Great.

Now you can/copy paste the solution, instantly forget about it, and move on to the next question on your assignment paper.

I'd love it if I was wrong about the above estimation of what you are up to, A. But somehow I doubt it.

/k1
 

Posts:1,135
Registered: 1/16/04
Re: RPN stack POP  
Aug 8, 2004 7:11 AM (reply 8 of 9)



 
Great.

Now you can/copy paste the solution, instantly forget
about it, and move on to the next question on your
assignment paper.

I'd love it if I was wrong about the above estimation
of what you are up to, A. But somehow I doubt it.

/k1

I hope he does read it...
You never know, I could be completely wrong...
After all, I am still relatively new to Java...
And I do not have a Professor or TA to ask question or get assistance from...
So there is alway a possibility that I might not have understood the process myself...

Plus, he will still have to proof read it prior to turning it in ( I would hope ), and therefore will have "read" it indirectly, and through that indirection possible learn it by exposure...

- MaxxDmg...

- ' He who never sleeps... '
 

Posts:24,036
Registered: 2/3/03
Re: RPN stack POP  
Aug 8, 2004 1:57 PM (reply 9 of 9)



 
I hope he does read it...

It won't.

You never know, I could be completely wrong...

You are.

After all, I am still relatively new to Java...

So at least there's value in YOU working through the problem.

And I do not have a Professor or TA to ask question or
get assistance from...
So there is alway a possibility that I might not have
understood the process myself...

Plus, he will still have to proof read it prior to
turning it in ( I would hope ), and therefore will
have "read" it indirectly, and through that
indirection possible learn it by exposure...

Not gonna happen.

Spoken from repeated experience,
§
 
This topic has 9 replies on 1 page.