[英]Infix to Postfix Using Stacks C++: Error Code 6
My assignment is to implement stacks using singly linked lists to convert a string which is in infix form to postfix form. 我的任务是使用单链接列表实现堆栈,以将以中缀形式转换为后缀形式的字符串。 For simplicity, this string does not contain any spaces.
为简单起见,此字符串不包含任何空格。
My algorithm in a nutshell is: 简而言之,我的算法是:
read character from infix string 从中缀字符串读取字符
create a temp node with the character and its associated precedence in the order of operations 按照操作顺序,使用字符及其关联的优先级创建一个临时节点
push it onto the stack if it is an operation and not a number/if it is a number, automatically append it to the postfix string 如果是操作而不是数字,则将其压入堆栈/如果是数字,则将其自动附加到后缀字符串
every time a character is pushed onto the stack, if the top node of the stack has a higher precedence than the temp node of the next character, pop it from the stack and append it to the postfix string. 每次将一个字符压入堆栈时,如果堆栈的顶部节点的优先级高于下一个字符的临时节点的优先级,请从堆栈中将其弹出,并将其附加到后缀字符串中。
These steps work when doing an infix to postfix by hand. 手动对后缀进行缀缀时,这些步骤有效。 Whenever I try to run my code, I keep getting an error 6 SIGABRT.
每当我尝试运行代码时,都会不断出现错误6 SIGABRT。 My code should be easy to understand.
我的代码应该很容易理解。 Can anyone tell me what this error means, why I am getting it, and how to fix it so that my code outputs the postfix string properly?
谁能告诉我这个错误的含义,为什么得到它,以及如何解决它,以便我的代码正确输出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 = 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;
}
Why do you do "delete v" in push? 为什么要在推送中“删除v”? This deletes the node you have just created.
这将删除您刚刚创建的节点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.