Methods Summary |
---|
void | clearStack()
s.removeAllElements();
|
protected void | doCalc()
int iType;
double tmp;
while ((iType = tf.nextToken()) != StreamTokenizer.TT_EOF) {
switch(iType) {
case StreamTokenizer.TT_NUMBER: // Found a number, push value to stack
push(tf.nval);
break;
case StreamTokenizer.TT_WORD:
// Found a variable, save its name. Not used here.
variable = tf.sval;
break;
case '+":
// + operator is commutative.
push(pop() + pop());
break;
case '-":
// - operator: order matters.
tmp = pop();
push(pop() - tmp);
break;
case '*":
// Multiply is commutative
push(pop() * pop());
break;
case '/":
// Handle division carefully: order matters!
tmp = pop();
push(pop() / tmp);
break;
case '=":
out.println(peek());
break;
default:
out.println("What's this? iType = " + iType);
}
}
|
public static void | main(java.lang.String[] av)
/* Driver - main program */
if (av.length == 0)
new SimpleCalcStreamTok(
new InputStreamReader(System.in)).doCalc();
else
for (int i=0; i<av.length; i++)
new SimpleCalcStreamTok(av[i]).doCalc();
|
double | peek()
return ((Double)s.peek()).doubleValue();
|
double | pop()
return ((Double)s.pop()).doubleValue();
|
void | push(double val)
s.push(new Double(val));
|
public void | setOutput(java.io.PrintWriter out)Change the output destination.
this.out = out;
|