我找到了conversion from infix to prefix,我正试图遵循它,但遇到了麻烦。我有大部分内容,但是在比较运营商层次结构方面遇到了麻烦。示例比较\到*。 *获得更高的等级。这是代码。我放 ??在我遇到麻烦的部分。我使用的语言是c ++。
string s;
stack<char>operatorStack;
stack<char>operandStack;
char rightOperand;
char leftOperandd;
char operand;
char opera
s = "1 + 2 * ( 1 - 2 - 3 - 4 ) ";
for (int i=0; i<s.length(); i++) {
if (s[i]!=' ') {
int arr= s[i];
cout << arr<<" ";
if (arr>=48) {
operandStack.push(s[i]);
}else {
if (s[i]=='(' || operatorStack.empty()||??(OperatorHierarchy(token) > OperatorHierarchy(OperatorStack.Top()) )??
operandStack.push(s[i]);
else if(s[i]==')'){
while (operandStack.top()!='(') {
opera=operatorStack.top();
operatorStack.pop();
rightOperand=operandStack.top();
operandStack.pop();
leftOperandd=operandStack.top();
operandStack.pop();
operand=opera+rightOperand+leftOperandd;
operandStack.push(operand);
}
operatorStack.pop();
}else if (?? operator hierarchy of token is less than or equal to hierarchy of top of the operator stack ??) {
while (!operatorStack.empty() && ??OperatorHierarchy(token) lessThen Or Equal to OperatorHierarchy(OperatorStack.Top()) ) ?? ) {
opera=operatorStack.top();
operatorStack.pop();
rightOperand=operandStack.top();
operandStack.pop();
leftOperandd=operandStack.top();
operandStack.pop();
operand=opera+rightOperand+leftOperandd;
operandStack.push(operand);
}
operatorStack.push(s[i]);
}
}
}
}
while (!operatorStack.empty()) {
opera=operatorStack.top();
operatorStack.pop();
rightOperand=operandStack.top();
operandStack.pop();
leftOperandd=operandStack.top();
operandStack.pop();
operand=opera+rightOperand+leftOperandd;
operandStack.push(operand);
}
cout << operatorStack.top();
operatorStack.pop();
return 0;
}