bool Calculator::Compute(void)
{ // 后缀表达式求值
int i, l, type;
double x, y, r;
element EL;
Stack<double> ST; //Stack to operands
Queue<element> postfix(PO); // 将PO复制到postfix
ST.Clear();
l = postfix.GetLength();
for (i = 0; i < l; i++) { //逆波兰表达式求值
EL = postfix.Delete();
type = EL.type;
if (type < 20) { // 操作数直接压栈
if (type == 0) // 压入数字
ST.Push(EL.value);
else // 压入变量
ST.Push(Vars[type-1]);
continue;
}
if (type > 40) { // 单目操作符
x = ST.Pop();
r = Calculate(EL, x);
ST.Push(r);
continue;
}
if (type > 20 && type < 40) { // 双目操作符
y = ST.Pop(); x = ST.Pop();
r = Calculate(EL, x, y);
ST.Push(r);
continue;
}
}