C++ 使用堆栈将前缀转换为后缀
我被告知要编写一个程序,使用堆栈将前缀形式转换为后缀形式。C++ 使用堆栈将前缀转换为后缀,c++,data-structures,stack,prefix,postfix-notation,C++,Data Structures,Stack,Prefix,Postfix Notation,我被告知要编写一个程序,使用堆栈将前缀形式转换为后缀形式。 当我使用纸和铅笔来实现该功能时,我现在的输出应该是正确的。但是,命令窗口中显示的结果很奇怪 实际产量: prefix : A postfix : A prefix : +*AB/CD postfix : AB*CD/+ prefix : +-*$ABCD//EF+GH postfix : AB$C*D-EF/GH+/H prefix : +D/E+$*ABCF postfix : DEAB*C$F+/F prefix
当我使用纸和铅笔来实现该功能时,我现在的输出应该是正确的。但是,命令窗口中显示的结果很奇怪 实际产量:
prefix : A
postfix : A
prefix : +*AB/CD
postfix : AB*CD/+
prefix : +-*$ABCD//EF+GH
postfix : AB$C*D-EF/GH+/H
prefix : +D/E+$*ABCF
postfix : DEAB*C$F+/F
prefix : /-*A+BCD-E+FG
postfix : ABC+DEFG+-+FG
正确输出:
prefix : A
postfix : A
prefix : +*AB/CD
postfix : AB*CD/+
prefix : +-*$ABCD//EF+GH
postfix : AB$C*D-EF/GH+/+
prefix : +D/E+$*ABCF
postfix : DEAB*C$F+/+
prefix : /-*A+BCD-E+FG
postfix : ABC+*D-EFG+-/
代码:
无效前缀到后缀(字符串和前缀,字符串和后缀)
{
//将输入前缀表达式转换为后缀格式
postfix=prefix;//将后缀字符串初始化为与前缀字符串相同的长度
堆栈S;
第十项;
int k=0;//用于访问后缀字符串的字符的索引
for(int i=0;i
看来您对OOP的基础知识很熟悉,所以我建议您采取更干净的方法。
在我看来,最好先从前缀生成一棵树,然后通过左-右-深度优先迭代得到后缀
硬部分是生成树,首先考虑有一个称为tNodo:
的结构class TNode
{
private:
TNode* _left;
TNode* _right;
public:
TNode* Parent;
char Symbol;
bool IsOperand;
TNode(char symbol , bool isOperand)
{
Symbol = symbol;
IsOperand = isOperand;
Parent = NULL;
_left = NULL;
_right = NULL;
}
void SetRight(TNode* node)
{
_right = node;
node->Parent = this;
}
void SetLeft(TNode* node)
{
_left = node;
node->Parent = this;
}
TNode* GetLeft()
{
return _left;
}
TNode* GetRight()
{
return _right;
}
};
下面是树生成器:
TNode* PostfixToTree(string prefix)
{
TNode* root = NULL;
TNode* nodeIter = NULL;
char c;
for(int i=0 ; i<prefix.length() ; i++)
{
c = prefix[i];
if(root == NULL)
{
if(!isOperand(c))
{
root = new TNode(c,false);
nodeIter = root;
}
else return NULL;
}
else
{
while(true)
{
if(nodeIter->GetLeft() == NULL && !isOperand(nodeIter->Symbol))
{
nodeIter->SetLeft(new TNode(c,isOperand(c)));
nodeIter = nodeIter->GetLeft();
break;
}
else if(nodeIter->GetRight() == NULL && !isOperand(nodeIter->Symbol))
{
nodeIter->SetRight(new TNode(c,isOperand(c)));
nodeIter = nodeIter->GetRight();
break;
}
else
{
while(isOperand(nodeIter->Symbol) ||
nodeIter->GetRight()!=NULL && nodeIter->GetLeft()!=NULL &&
nodeIter->Parent!=NULL)
{
nodeIter = nodeIter->Parent;
}
}
}
}
}
return root;
}
TNode*postfix树(字符串前缀)
{
TNode*root=NULL;
TNode*nodeIter=NULL;
字符c;
对于(int i=0;iGetLeft()==NULL&&!等标量(nodeIter->Symbol))
{
nodeIter->SetLeft(新的TNode(c,等规数(c));
nodeIter=nodeIter->GetLeft();
打破
}
else if(nodeIter->GetRight()==NULL&&!isOperand(nodeIter->Symbol))
{
nodeIter->SetRight(新的TNode(c,isOperand(c));
nodeIter=nodeIter->GetRight();
打破
}
其他的
{
while(等标号(节点号->符号)||
nodeIter->GetRight()!=NULL&&nodeIter->GetLeft()!=NULL&&
节点编辑器->父节点!=NULL)
{
节点编辑器=节点编辑器->父节点;
}
}
}
}
}
返回根;
}
最后是从树中生成后缀的函数
string TreeToPostfix(TNode* root)
{
string postfix = "";
stack<TNode*> nodeStack;
nodeStack.push(root);
while(!nodeStack.empty())
{
TNode* top = nodeStack.top();
nodeStack.pop();
postfix = top->Symbol + postfix;
if(top->GetLeft()!=NULL)
nodeStack.push(top->GetLeft());
if(top->GetRight()!=NULL)
nodeStack.push(top->GetRight());
}
return postfix;
}
字符串树后缀(TNode*root)
{
字符串后缀=”;
堆栈节点堆栈;
nodeStack.push(根);
而(!nodeStack.empty())
{
TNode*top=nodeStack.top();
nodeStack.pop();
后缀=顶部->符号+后缀;
if(top->GetLeft()!=NULL)
nodeStack.push(top->GetLeft());
如果(顶部->GetRight()!=NULL)
nodeStack.push(top->GetRight());
}
返回后缀;
}
string TreeToPostfix(TNode* root)
{
string postfix = "";
stack<TNode*> nodeStack;
nodeStack.push(root);
while(!nodeStack.empty())
{
TNode* top = nodeStack.top();
nodeStack.pop();
postfix = top->Symbol + postfix;
if(top->GetLeft()!=NULL)
nodeStack.push(top->GetLeft());
if(top->GetRight()!=NULL)
nodeStack.push(top->GetRight());
}
return postfix;
}