简体   繁体   English

后缀到栈中缀。

[英]Postfix to infix with stacks.

I'm working on a project to convert from post fix notation to, fully parenthesized infix notation. 我正在一个项目中,从后修复符号转换为完全带括号的中缀符号。 The issue I am having is, it prints/stores in the reverse order it prints out as: 我遇到的问题是,它以与打印相反的顺序打印/存储为:

 For line: QQ=
(Q=Q)


 For line: ABC*D+*
((D+(C*B))*A)


 For line: AB+CD+EF%G--*
(((G-(F%E))-(D+C))*(B+A))


 For line: NT^G*NN+#
((N+N)#(G*(T^N)))


 For line: A
A


 For line: ABC*D+*
((D+(C*B))*A)

my code that reads the data in is: 我的读取数据的代码是:

void ReadData(string inString, ifstream& in)
{
    if (in.is_open())
    {
        stack<string> postToIn;

        for (unsigned int i = 0; i< inString.length(); i++)
        {
            if ((inString[i] != '+') && (inString[i] != '-') && (inString[i] != '/') && (inString[i] != '#') &&
                (inString[i] != '*') && (inString[i] != '%') && (inString[i] != '^') && (inString[i] != '='))
            {
                string charac(1,inString[i]);

                postToIn.push(charac);
            }
            else
            {
                string temp = "";
                temp += "(";
                temp += postToIn.top();
                postToIn.pop();
                temp += inString[i];
                temp += postToIn.top();
                postToIn.pop();
                temp += ")";
                postToIn.push(temp);
            }           
        }


        while (!postToIn.empty())
        {
            cout << postToIn.top();
            postToIn.pop();
        }
        cout << endl;       
    }
}

I can't tell where in my code it is reversing it. 我不知道它在代码的哪个位置反转了它。 I know the stacks are first out/ last in. Any help would be greatly appreciated. 我知道堆栈是先进先出的。任何帮助将不胜感激。

The stack top will have the most recent operand which is what you want on the right side. 堆栈顶部将在右侧具有您想要的最新操作数。 The current implementation puts it on the left side of the operator. 当前实现将其放在运算符的左侧。

            string temp = "";
            string temp2 = "";
            temp += "(";
            temp2 += postToIn.top(); // This is the recent operand. This needs to go on the right of the operator in infix notation
            postToIn.pop();
            temp += postToIn.top();
            postToIn.pop();
            temp += inString[i];
            temp += temp2;
            temp += ")";
            postToIn.push(temp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM