繁体   English   中英

后缀到栈中缀。

[英]Postfix to infix with stacks.

我正在一个项目中,从后修复符号转换为完全带括号的中缀符号。 我遇到的问题是,它以与打印相反的顺序打印/存储为:

 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)

我的读取数据的代码是:

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

我不知道它在代码的哪个位置反转了它。 我知道堆栈是先进先出的。任何帮助将不胜感激。

堆栈顶部将在右侧具有您想要的最新操作数。 当前实现将其放在运算符的左侧。

            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