代码从中缀转换为前缀时遇到问题
我发现从中缀到前缀的转换并且正在尝试遵循它,但遇到了麻烦。我拥有其中的大部分,但在比较运算符层次结构时遇到困难。示例将 \ 与 * 进行比较。 * get具有更高的层次结构。这是代码。我放??围绕我遇到问题的部分。我使用的语言是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;
}
I have found conversion from infix to prefix and am trying to follow it but am having trouble. I have most of it, but am having trouble comparing operator hierarchy. Example compare \ to *. * get has higher hierarchy. Here is the code. I put ?? around the part I am having trouble with. The language I am using is 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论