2017-10-28 141 views
0

我的任務是實現使用單向鏈表將字符串以中綴形式轉換爲後綴形式的堆棧。爲了簡單起見,該字符串不包含任何空格。使用堆棧添加到Postfix C++:錯誤代碼6

我簡而言之算法是:從綴串

    1. 讀取字符創建操作

    2. 推的順序與字符一個臨時節點及其關聯優先如果它是一個操作而不是一個數字/如果它是一個數字,則將它自動附加到堆棧上

    3. 每次將字符壓入堆棧時,如果堆棧的頂層節點的優先級高於下一個字符的臨時節點的優先級,請從堆棧中彈出並將其附加到後綴字符串中。

    這些步驟在手工添加到postfix時有效。 每當我嘗試運行我的代碼時,我都會收到錯誤6 SIGABRT。我的代碼應該很容易理解。任何人都可以告訴我這個錯誤意味着什麼,爲什麼我得到它,以及如何解決它,以便我的代碼正確地輸出後綴字符串?

    #include <iostream> 
    #include <string> 
    
    using namespace std; 
    string postfix; //infix and postfix strings 
    
    //function to return true if a character is an operation 
    bool isoperator(char a) 
    { 
        if(a == '(' || ')' || '*' || '/' || '+' || '-') //might need to 
    change to "" instead of '' 
        { 
         return(true); 
        } 
        else 
        { 
         return(false); 
        } 
    } 
    
    //node class 
    class node 
    { 
    public: 
        char character; 
        //double number; 
        int level; //to check for precedence of operations 
        node *ptr; 
        void assignlevel() 
        { 
         switch(character) 
         { 
          case ')': 
           level = 3; 
           break; 
          case '(': 
           level = 0; 
           break; 
          case '+': 
           level = 1; 
           break; 
          case '-': 
           level = 1; 
           break; 
          case '*': 
           level = 2; 
           break; 
          case '/': 
           level = 2; 
           break; 
          default: 
           level = 0; 
         } 
    } 
    friend class stack; 
    }; 
    
    //stack class 
    class stack 
    { 
    public: 
        node *top, *temp; 
    
    //Constructor Function 
    stack() 
    { 
        top = new node; 
        top->character = '&'; 
        top->ptr = NULL; 
        top->level = 0; 
        temp = new node; 
        temp->ptr = NULL; 
    } 
    
    //Empty 
    bool empty() 
    { 
        return(top->character == '&'); 
    } 
    
    //Read character from string 
    void readchar(char a) 
    { 
        temp->character = a; 
        temp->assignlevel(); 
    } 
    
    //Check Precedence of top and temp 
    bool precedence() 
    { 
        return(top->level >= temp->level); 
    } 
    
    //Push function for infix to postfix 
    void push1(char a) 
    { 
        readchar(a); 
        if(isoperator(temp->character)) //Push onto stack if character is an operation 
        { 
         if(empty()) 
         { 
          top->character = temp->character; 
          top->assignlevel(); 
         } 
         else 
         { 
          node *v = new node; 
          v->character = temp->character; 
          v->level = temp->level; 
          v->ptr = top; 
          top = v; 
          delete v; 
         } 
        } 
        else //append to string if character is number 
        { 
         postfix += temp->character; 
        } 
    
        if(precedence()) //we check if we have to pop every time we push 
    onto the stack 
         { 
          pop1(); 
         } 
        } 
    
        void pop1() //Pop onto postfix string 
        { 
         postfix += top->character; 
         node *w = top->ptr; 
         delete &top; 
         top = w; 
         delete w; 
        } 
    }; 
    
    int main() 
    { 
        string infix = "2+3-5*(7+1)"; 
        stack op; 
        for(int i = 0; i < infix.size(); ++i) 
        { 
         op.push1(infix[i]); 
        } 
    
    
        for(int j = 0; j < infix.size(); j++) 
        { 
         cout << postfix[j]; 
        } 
        return 0; 
    } 
    
  • 回答

    0

    你爲什麼要在推送中「刪除v」?這會刪除剛剛創建的節點。

    +0

    是的,我意識到,過了一段時間。它擺脫了那個錯誤,現在我至少得到了一個字符串輸出 – arnavlohe15