/**
* 功能:依据指定的操作数、运算符,进行运算。
*/
private void calc() throws Exception {
double b = this.numStack.pop();
double a = this.numStack.pop();
char op = this.chStack.pop();
double result = 0;
switch (op) {
case '+': result = a + b; break;
case '-': result = a - b; break;
case '*': result = a * b; break;
case '/': {
if (b == 0) {
throw new ArithmeticException("除数不能为0!");
}
result = a / b;
break;
}
case '%': {
if (b == 0) {
throw new ArithmeticException("除数不能为0!");
}
result = a % b;
break;
}
}
// 将运算的结果压栈。
this.numStack.push(result);
}
public void print(){
System.out.println("数字栈元素:"+this.numStack);
System.out.println("符号栈元素:"+this.chStack);
}
}[/code]范例3:Test类。[code=java]package org.cxy.expression;