将中缀表示法表达式转换为后缀表示法

发布于 2024-10-08 21:08:53 字数 2212 浏览 4 评论 0原文

我正在为我的数据结构课程做作业,其中我必须将中缀表达式转换为后缀表达式。我几乎完成了它,但当我尝试输入类似 a+b+c 的内容时,我不断收到错误

它可以很好地处理 a+b 和 a+b*c 。

我真的不知道出了什么问题。如果有人能给我指明方向或看到我的代码的问题,我将非常感激。

#include <iostream>
#include <stack>

using namespace std; 

//checks priority of operators.
int priority(char e){
    int pri = 0; 

    if(e == '*' || e == '/' || e == '%'){
        pri = 2; 
    }else{
        if(e == '+' || e == '-'){
            pri = 1; 
        }
    }
    return pri; 
}

void main(){
    cout << "This program will convert an infix expression to a postfix expression." << endl; 
    cout << "Please enter your expression without any spaces." << endl; 

    stack<char> charStack; 

    char input[100]; 
    char output[100];
    char n1; 

    char *o; 
    o = &output[0]; 

    cin >> input; 

    int n = 0; 
    while(input[n] != 0){

        if(isdigit(input[n])  || isalpha(input[n])){
            *o = input[n]; 
            n++; 
            o++; 
        }

        if(input[n] == '('){
            charStack.push(input[n]); 
            n++;
        }

        if(input[n] == ')'){
            n1 = charStack.top(); 
            charStack.pop(); 
            while(n1 != '('){
                *o = n1; 
                o++; 
                n1 = charStack.top(); 
                charStack.pop(); 
            }
            n++; 
        }

        if(input[n] == '+' || input[n] == '-' || input[n] == '*' || input[n] == '/' || input[n] == '%'){
            if(charStack.empty() == true){
                charStack.push(input[n]);
            }else{
                n1 = charStack.top(); 
                charStack.pop(); 
                while(priority(n1) >= priority(input[n])){
                    *o = n1; 
                    o++;
                    n1 = charStack.top(); 
                    charStack.pop(); 
                }
                charStack.push(n1); 
                charStack.push(input[n]); 
            }
            n++; 
        }
    }
    while(!charStack.empty()){
        *o = charStack.top(); 
        o++; 
        charStack.pop(); 
    }
    *o = '\0'; 

    cout << output << endl; 

}

I'm doing an assignment for my Data Structures course where I have to convert an infix expression to a postfix expression. I'm almost done with it but I keep getting an error when I try entering something like a+b+c

It can handle a+b and a+b*c just fine.

I'm really not sure what's wrong with it. If someone could point me in a direction or see the problem with my code, I would really appreciate it.

#include <iostream>
#include <stack>

using namespace std; 

//checks priority of operators.
int priority(char e){
    int pri = 0; 

    if(e == '*' || e == '/' || e == '%'){
        pri = 2; 
    }else{
        if(e == '+' || e == '-'){
            pri = 1; 
        }
    }
    return pri; 
}

void main(){
    cout << "This program will convert an infix expression to a postfix expression." << endl; 
    cout << "Please enter your expression without any spaces." << endl; 

    stack<char> charStack; 

    char input[100]; 
    char output[100];
    char n1; 

    char *o; 
    o = &output[0]; 

    cin >> input; 

    int n = 0; 
    while(input[n] != 0){

        if(isdigit(input[n])  || isalpha(input[n])){
            *o = input[n]; 
            n++; 
            o++; 
        }

        if(input[n] == '('){
            charStack.push(input[n]); 
            n++;
        }

        if(input[n] == ')'){
            n1 = charStack.top(); 
            charStack.pop(); 
            while(n1 != '('){
                *o = n1; 
                o++; 
                n1 = charStack.top(); 
                charStack.pop(); 
            }
            n++; 
        }

        if(input[n] == '+' || input[n] == '-' || input[n] == '*' || input[n] == '/' || input[n] == '%'){
            if(charStack.empty() == true){
                charStack.push(input[n]);
            }else{
                n1 = charStack.top(); 
                charStack.pop(); 
                while(priority(n1) >= priority(input[n])){
                    *o = n1; 
                    o++;
                    n1 = charStack.top(); 
                    charStack.pop(); 
                }
                charStack.push(n1); 
                charStack.push(input[n]); 
            }
            n++; 
        }
    }
    while(!charStack.empty()){
        *o = charStack.top(); 
        o++; 
        charStack.pop(); 
    }
    *o = '\0'; 

    cout << output << endl; 

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野鹿林 2024-10-15 21:08:54

在弹出运算符代码中的元素之前,不检查堆栈是否为空。这是问题的一部分。

顺便说一句,它应该是 int main() 而不是 void,并且您不需要将事物与 true 进行比较:charStack.empty() == truecharStack.empty() 相同。

You don't check whether the stack is empty before popping an element in the code for operators. That's part of the problem.

By the way, it should be int main() instead of void, and you don't need to compare things with true: charStack.empty() == true is the same as charStack.empty().

晨与橙与城 2024-10-15 21:08:54

请参阅我的内联评论。

// You can empty the stack here.
charStack.pop(); 

while(priority(n1) >= priority(input[n])){
    ...

    // BUG: This line will crash if the stack is empty.
    // You need to check for an empty stack.
    n1 = charStack.top(); 

See my comments inline.

// You can empty the stack here.
charStack.pop(); 

while(priority(n1) >= priority(input[n])){
    ...

    // BUG: This line will crash if the stack is empty.
    // You need to check for an empty stack.
    n1 = charStack.top(); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文