Methods Summary |
---|
void | clearStack()
s.removeAllElements();
|
protected void | doCalc()
int iType;
double tmp;
while (scan.hasNext()) {
if (scan.hasNextDouble()) {
push(scan.nextDouble());
} else {
String token = scan.next().toString();
if (token.equals("+")) {
// Found + operator, perform it immediately.
push(pop() + pop());
} else if (token.equals("-")) {
// Found - operator, perform it (order matters).
tmp = pop();
push(pop() - tmp);
} else if (token.equals("*")) {
// Multiply is commutative
push(pop() * pop());
} else if (token.equals("/")) {
// Handle division carefully: order matters!
tmp = pop();
push(pop() / tmp);
} else if (token.equals("=")) {
out.println(peek());
} else {
out.println("What's this? " + token);
}
}
}
|
public static void | main(java.lang.String[] av)
/* Driver - main program */
if (av.length == 0)
new SimpleCalcScanner(
new InputStreamReader(System.in)).doCalc();
else
for (int i=0; i<av.length; i++)
new SimpleCalcScanner(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 | setWriter(java.io.PrintWriter pw)Change the output to go to a new PrintWriter
out = pw;
|