代码从中缀转换为前缀时遇到问题

发布于 2024-12-13 01:32:14 字数 2426 浏览 1 评论 0原文

我发现从中缀到前缀的转换并且正在尝试遵循它,但遇到了麻烦。我拥有其中的大部分,但在比较运算符层次结构时遇到困难。示例将 \ 与 * 进行比较。 * 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文