【发布时间】:2019-08-02 09:38:34
【问题描述】:
我目前正在尝试测试是否有太多操作数,但无法确定后缀表达式何时有太多操作数的条件。
有人可以给我一些关于测试什么的指示吗?
到目前为止,这是我的功能:
void evaluatePostFix(string str){
Stack stack;
// Strip whitespaces
str.erase(str.find(' '), 1);
if (str.length() == 1 || str.length() == 0){
string singleOperand;
singleOperand.push_back(str[0]);
stack.push(createExpression("", singleOperand, ""));
}
int count = 0;
for (const char & c : str){
count++;
if (isOperand(c)){
string singleOperand;
singleOperand.push_back(c);
stack.push(singleOperand);
} else {
if (stack.isEmpty()){
cout << "To many operators" << endl;
return;
}
string operand1 = stack.top();
stack.pop();
if (stack.isEmpty()){
cout << "To many operators" << endl;
return;
}
string operand2 = stack.top();
stack.pop();
string operator1, expression;
operator1.push_back(c);
expression = createExpression(operand1, operand2, operator1);
stack.push(expression);
}
}
stack.print();
}
【问题讨论】:
标签: c++ algorithm stack postfix-notation infix-notation