[英]Iterator not dereferencable stack (expression tree) & loop does not work properly
I am trying to convert infix to postfix and prefix by using tree in c++. 我正在尝试通过在C ++中使用树将中缀转换为后缀和前缀。 I pass the infix expression to the createTree function and in the code I use 2 stack node(to store the operand and operator that has been poped from the stack s) and stack s(to store the operator).
我将infix表达式传递给createTree函数,并在代码中使用2个堆栈节点(用于存储从堆栈s中弹出的操作数和运算符)和堆栈s(用于存储运算符)。 The code works finely when I input expression that has same precedence operators or the first operator has higher precedence than the second one.
当我输入具有相同优先级运算符的表达式或第一个运算符的优先级高于第二个运算符的代码时,该代码可以正常工作。 ex: 1*2/3 or 1/2+3.
例如:1 * 2/3或1/2 + 3。 But it will debug when I input expressions which the first operator has lower precedence than the second one.
但是,当我输入第一个运算符的优先级低于第二个运算符的优先级时,它将进行调试。 for example : 1+2/3.
例如:1 + 2/3。 I try to debug it and found DEBUG_ERROR("iterator not dereferencable");
我尝试对其进行调试,发现DEBUG_ERROR(“迭代器不可取消引用”); I try to find in the internet and all those problems related to vector which I have never used before.
我试图在互联网上找到所有以前从未使用过的与向量有关的问题。 I hope you guys could help me
我希望你们能帮助我
node *CreateTree(string post)
{
node *temp, *op1 , *op2;
stack<node*> tree;
stack<char> s;
int i = 0;
while (!tree.empty())
{
tree.pop();
}
for(i = 0; i<post.length();i++)
{
if (post[i]==' ') continue;
else if(IsOperand(post[i]))
{
node *temp = new node();
temp->in = post[i];
temp->left = NULL;
temp->right = NULL;
for(int j=i+1;j<post.length();j++)
{
if (IsOperand(post[j]))
{
temp->in+=post[j];
i = j;
}
else break;
}
tree.push(temp);
}
else if(isOperator(post[i]))
{
while(!s.empty() && s.top()!='(' && s.top()!='{' && s.top()!='[' && !hasHigherPrecedence(post[i],s.top()))
{
node *op1;
node *op2;
op2 = tree.top();
tree.pop();
op1 = tree.top();
tree.pop();
node *temp = new node();
temp->in = s.top();
temp->left = op1;
temp->right = op2;
root = temp;
s.pop();
tree.push(root);
}
s.push(post[i]);
}
else if(post[i]=='(')
{
s.push(post[i]);
}
else if(post[i]==')')
{
while(s.top()!='('&&!s.empty())
{
node *op1;
node *op2;
op2 = tree.top();
tree.pop();
op1 = tree.top();
tree.pop();
node *temp = new node();
temp->in = s.top();
temp->left = op1;
temp->right = op2;
root = temp;
s.pop();
tree.push(root);
}
s.pop();
}
else if(post[i]=='[')
{
s.push(post[i]);
}
else if(post[i]==']')
{
while(s.top()!='['&&!s.empty())
{
node *op1;
node *op2;
op2 = tree.top();
tree.pop();
op1 = tree.top();
tree.pop();
node *temp = new node();
temp->in = s.top();
temp->left = op1;
temp->right = op2;
root = temp;
s.pop();
tree.push(root);
}
s.pop();
}
else if(post[i]=='{')
{
s.push(post[i]);
}
else if(post[i]=='}')
{
while(s.top()!='{'&&!s.empty())
{
node *op1;
node *op2;
op2 = tree.top();
tree.pop();
op1 = tree.top();
tree.pop();
node *temp = new node();
temp->in = s.top();
temp->left = op1;
temp->right = op2;
root = temp;
s.pop();
tree.push(root);
}
s.pop();
}
else cout<<post[i]<<" is not registered as operator or operand";
}
while (!s.empty())
{
node *op1;
node *op2;
op2 = tree.top();
tree.pop();
op1 = tree.top();
tree.pop();
node *temp = new node();
temp->in = s.top();
temp->left = op1;
temp->right = op2;
root = temp;
s.pop();
}
return root;
}
below is the functions that are called in code above 下面是上面代码中调用的函数
bool isOperator(char a)
{
return(a=='+' || a=='*' || a=='^' || a=='/' ||
a=='-' || a=='%')? true:false;
}
int getWeight(char op)
{
int weight;
switch(op)
{
case '+' : case '-':
weight = 1;
break;
case '*':
case '/':
case '%':
weight = 2;
break;
case '^':
weight = 3;
break;
}
return weight;
}
bool hasHigherPrecedence(char a, char top)
{
int aW = getWeight(a);
int tW = getWeight(top);
return (aW > tW)? true : false;
}
I have found my answer i just forget to insert tree.push(root) 我找到了答案,只是忘记插入tree.push(root)
while (!s.empty())
{
node *op1;
node *op2;
op2 = tree.top();
tree.pop();
op1 = tree.top();
tree.pop();
node *temp = new node();
temp->in = s.top();
temp->left = op1;
temp->right = op2;
root = temp;
tree.push(root);//I forgot this
s.pop();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.