我稍微修改了这个算法来处理大括号和方括号
queue<string> postEvaluation(vector<string> expression){
stack<string> operators;
queue<string> output;
for(int i = 0; i < expression.size(); i++){
if(isdigit(expression[i]))
output.push(expression[i]);
if(expression[i] == "");
if (IsOperator(expression[i])) {
/* Next, move all operators off the stack until this operator has the
* highest precedence of them all.
*/
while (!operators.empty() && IsOperator(operators.top()) &&
Precedence(operators.top()) >= Precedence(expression[i])) {
output.push(operators.top()); operators.pop();
}
/* Add the operator to the operator stack. */
operators.push(expression[i]);
}
/* Case 2: Found an open parenthesis. */
else if (expression[i] == "(" || expression[i] == "[" || expression[i] == "{" ) {
operators.push(expression[i]);
}
/* Case 3: Found a close parenthesis. */
else if (expression[i] == ")" || expression[i] == "]" || expression[i] == "}") {
/* Keep moving tokens over from the operator stack to the result until
* we find a matching open parenthesis. If we run out of tokens, then
* there must be a syntax error.
*/
while (!operators.empty() && operators.top() != "(" && !operators.empty() && operators.top() != "[" && !operators.empty() && operators.top() != "{") {
output.push(operators.top()); operators.pop();
}
/* Remove the parenthesis we matched. */
operators.pop();
}
}
/* Keep shifting all the operators back off the operator stack onto the end
* of the input. If we find an open parenthesis, then the input is
* malformed.
*/
while (!operators.empty()) {
output.push(operators.top()); operators.pop();
}
return output;
}
出于某种原因,当我输入以下内容时: 10 - [ ( 3 - 2 ) * 10 ] / 5
输出为: 10 3 2 - 10 * - 5 /
当输出应该是:10 3 2 - 10 * 5 / -
关于如何正确修改它以处理大括号{}和括号[]的任何想法?