一个算数表达式(以下简称为表达式)由运算数、运算符、左括号和右括号组成,定义一个枚举类型TokenType表示为:
typedef enum {
BEGIN,
NUMBER,
OPERATOR,
LEFT_BRAC,
RIGHT_BRAC
} TokenType;
BEGIN用来表示表达式的开始,稍后会再提及到它。
对表达式进行求值需要借助数据结构栈,C++的标准模板库中包含stack类型,只需要包含头文件stack并引用命名空间std就可以使用了。整个求值过程总共需要2个栈,分别用来存储运算数和运算符;基本求值过程是这样的:比较当前运算符和运算符栈中栈顶运算符的优先级,若栈顶运算符优先级高于当前运算符,则从运算数栈中弹出两个运算数,使用运算符栈栈顶运算符进行计算后再压入运算数栈,并将当前运算符压入运算符栈,否则只将当前运算符压入运算符栈;最后反复上述运算压栈过程直至运算符栈为空,运算数栈的栈顶元素即为运算结果。Calculator类声明如下:
class Calculator
{
public:
double calculate(string expression) throw(string); private:
stack<double> _stkNumbers;
stack<char> _stkOperators; static int priority(char op);
static double calculate(double d1, char op, double d2) throw(string); void calculateStack() throw(string);
void dealWithNumber(char *&pToken) throw(string);
void dealWithOperator(char *&pToken) throw(string);
void dealWithLeftBrac(char *&pToken) throw(string);
void dealWithRightBrac(char *&pToken) throw(string);
};
使用代码描述的运算压栈过程如下:
void Calculator::calculateStack() throw (string) {
double d2 = _stkNumbers.top();
_stkNumbers.pop();
double d1 = _stkNumbers.top();
_stkNumbers.pop();
char op = _stkOperators.top();
_stkOperators.pop();
_stkNumbers.push(calculate(d1, op, d2));
}
静态成员函数calculate()用于进行简单的四则运算,同时也处理了除数为0的情况:
double Calculator::calculate(double d1, char op, double d2) throw (string) {
assert(op == '+' || op == '-' || op == '*' || op == '/'); cout << d1 << op << d2 << endl; if (op == '+') {
return d1 + d2;
} else if (op == '-') {
return d1 - d2;
} else if (op == '*') {
return d1 * d2;
} else {
if (!d2) {
throw string("divided by 0");
}
return d1 / d2;
}
}
此外,根据数学规则,有:
- 表达式只能以左括号或运算数开始;
- 运算数后只能是右括号或运算符;
- 运算符或左括号后只能是左括号或运算数;
- 右括号后只能是另一个右括号或运算符。
使用代码描述这些数学规则和最后清空运算符栈的过程如下:
double Calculator::calculate(string expression) throw (string) {
while (!_stkNumbers.empty()) {
_stkNumbers.pop();
}
while (!_stkOperators.empty()) {
_stkOperators.pop();
}
TokenType lastToken = BEGIN; char * pToken = &expression[];
while (*pToken) {
switch (lastToken) {
case BEGIN:
if (*pToken == '(') {
// an expression begin with a left bracket
dealWithLeftBrac(pToken);
lastToken = LEFT_BRAC;
} else {
// or a number
dealWithNumber(pToken);
lastToken = NUMBER;
}
break;
case NUMBER:
// after a number
if (*pToken == ')') {
// it may be a right bracket
dealWithRightBrac(pToken);
lastToken = RIGHT_BRAC;
} else {
// it may be an operator
dealWithOperator(pToken);
lastToken = OPERATOR;
}
break;
case OPERATOR:
case LEFT_BRAC:
// after an operator or a left bracket
if (*pToken == '(') {
// it may be a left bracket
dealWithLeftBrac(pToken);
lastToken = LEFT_BRAC;
} else {
// it may be a number
dealWithNumber(pToken);
lastToken = NUMBER;
}
break;
case RIGHT_BRAC:
// after a right bracket
if (*pToken == ')') {
// it may be another right bracket
dealWithRightBrac(pToken);
lastToken = RIGHT_BRAC;
} else {
// it may be an operator
dealWithOperator(pToken);
lastToken = OPERATOR;
}
break;
}
} while (!_stkOperators.empty()) {
if (_stkOperators.top() == '(') {
throw string("bad token '('");
}
calculateStack();
} assert(!_stkNumbers.empty());
return _stkNumbers.top();
}
lastToken用来指示上一个token的类型,它应该被初始化为BEGIN;在开始求值之前清空运算符栈和运算数栈,可以防止出错,是很有必要的。