簡體   English   中英

使用堆棧C ++對Postfix的修復:錯誤代碼6

[英]Infix to Postfix Using Stacks C++: Error Code 6

我的任務是使用單鏈接列表實現堆棧,以將以中綴形式轉換為后綴形式的字符串。 為簡單起見,此字符串不包含任何空格。

簡而言之,我的算法是:

  1. 從中綴字符串讀取字符

  2. 按照操作順序,使用字符及其關聯的優先級創建一個臨時節點

  3. 如果是操作而不是數字,則將其壓入堆棧/如果是數字,則將其自動附加到后綴字符串

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

手動對后綴進行綴綴時,這些步驟有效。 每當我嘗試運行代碼時,都會不斷出現錯誤6 SIGABRT。 我的代碼應該很容易理解。 誰能告訴我這個錯誤的含義,為什么得到它,以及如何解決它,以便我的代碼正確輸出postfix字符串?

#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;
}

為什么要在推送中“刪除v”? 這將刪除您剛剛創建的節點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM