表达式求值(栈方法/C++语言描述)(三)

代码清单

 // calculator.h
#ifndef CALCULATOR_H
#define CALCULATOR_H #include <stack>
#include <string>
#include <iostream>
#include <cassert> using namespace std; typedef enum {
BEGIN,
NUMBER,
OPERATOR,
LEFT_BRAC,
RIGHT_BRAC
} TokenType; 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);
}; #endif // CALCULATOR_H // calculator.cpp
#include "calculator.h" int Calculator::priority(char op) {
assert(op == '+' || op == '-' || op == '*' || op == '/' || op == '('); if (op == '+' || op == '-') {
return ;
} else if (op == '*' || op == '/') {
return ;
} else {
return ;
}
} 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;
}
} 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));
} 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();
} void Calculator::dealWithNumber(char *&pToken) throw (string) {
if (!isdigit(*pToken) && *pToken != '-') {
throw string("bad token '") + *pToken + "'";
}
_stkNumbers.push(strtod(pToken, &pToken));
} void Calculator::dealWithOperator(char *&pToken) throw (string) {
if (*pToken != '+' && *pToken != '-' && *pToken != '*' && *pToken != '/') {
throw string("bad token '") + *pToken + "'";
}
if (!_stkOperators.empty()
&& priority(_stkOperators.top()) >= priority(*pToken)) {
calculateStack();
}
_stkOperators.push(*pToken);
pToken++;
} void Calculator::dealWithLeftBrac(char *&pToken) throw (string) {
if (*pToken != '(') {
throw string("bad token '") + *pToken + "'";
}
_stkOperators.push(*pToken);
pToken++;
} void Calculator::dealWithRightBrac(char *&pToken) throw (string) {
if (*pToken != ')') {
throw string("bad token '") + *pToken + "'";
}
while (!_stkOperators.empty() && _stkOperators.top() != '(') {
calculateStack();
if (_stkOperators.empty()) {
throw string("bad token ')'");
}
}
_stkOperators.pop();
pToken++;
} // main.cpp
#include "calculator.h" int main(int argc, char *argv[])
{
Calculator calculator; if (argc > ) {
if (argc == ) {
cout << calculator.calculate(string(argv[])) << endl;
} else {
cout << "too many arguments" << endl;
return -;
}
} else {
while () {
string expression;
cout << ">" << flush;
cin >> expression;
if (expression == "quit;") {
cout << "Bye." << endl;
return ;
}
try {
cout << calculator.calculate(expression) << endl;
} catch (string ex) {
cout << ex << endl;
}
}
}
}
上一篇:HDU 1241 Oil Deposits --- 入门DFS


下一篇:Android加载网络图片报android.os.NetworkOnMainThreadException异常