【问题标题】:Converting infix notation expression to postfix notation将中缀表示法表达式转换为后缀表示法
【发布时间】:2011-05-27 16:27:07
【问题描述】:

我正在为我的数据结构课程做作业,我必须将中缀表达式转换为后缀表达式。我差不多完成了,但是当我尝试输入 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; 

}

【问题讨论】:

    标签: c++ stack notation infix-notation


    【解决方案1】:

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

    对了,应该是int main()而不是void,而且你不需要和true比较:charStack.empty() == truecharStack.empty()是一样的。

    【讨论】:

      【解决方案2】:

      查看我的 cmets 内联。

      // 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(); 
      

      【讨论】:

      • 感谢您的回复。 :D 和上面的人一样,但我很感激。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-15
      • 2014-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多