c++ - c++中的调车场算法

标签 c++ infix-notation postfix-notation shunting-yard

我需要一个接受中缀字符串(如“3 + 4 * 9”)并将其转换为后缀(如“4 9 * 3 +”)的函数。

在您将括号放入括号内之前,我一直在使用它。我整天都在研究它,无法弄清楚我做错了什么 - 有新思维的人可以看到它吗?我觉得我真的很接近!

谢谢!这是代码:

    string ExpressionManager::infixToPostfix(string infixExpression)
    {
cout << "itop Testing : " << infixExpression << endl;
string posnums = "0123456789";
string posops = "+-*/%(){}[]";
string onlyops = "+-/%*";
string space = " ";
string openbra = "([{";
string closebra = ")]}";
stack <string> nums;
stack <string> ops;
string output = "";

//check to make sure expression is valid

if (!(isBalanced(infixExpression)))
{
    cout << "Infix Expression isn't balanced!" << endl;
    return "invalid";
}


for (int i=0; i<infixExpression.size(); i++)
{
    if ((posnums.find(infixExpression[i])!=string::npos) || (posops.find(infixExpression[i])!=string::npos) || (space.find(infixExpression[i])!=string::npos))
    {}

    else
    {
        cout << "Invalid character " << infixExpression[i] << " found." << endl;
        return "invalid";
    }
}


int numcount = 0;
int opcount = 0;
//Numbers vs. Operators
for (int i = 0; i < infixExpression.size(); i++)
{
    if (posnums.find(infixExpression[i]) != -1)
    {

        while(infixExpression[i] != ' ')
        {
            if (i == (infixExpression.size()-1))
                break;
            i++;
        }
        numcount++;
    }

    if (onlyops.find(infixExpression[i]) != -1)
    {
        opcount++;
    }
}


if (opcount == (numcount - 1))
{
}
else
{
    cout << "Bad operators to numbers ratio!" << endl;
    return "invalid";
}

//Get rid of proceeding whitespaces.
int safety = 0;
int net = infixExpression.size();

while (infixExpression[0] == ' ')
{
    infixExpression.erase(0,1);
    safety++;

    if (safety>=net)
        break;
}

//cout << "At this point, it is " << postfixExpression << endl;

//the fun part! Set up stacks

for (int i =0; i< infixExpression.size(); i++)
{
    cout << "It gets hung up on character " << infixExpression[i] << endl;
    if(openbra.find(infixExpression[i]) != -1)
    {

        string word = "";
        word += infixExpression[i];
        ops.push(word);

        cout << "Pushing onto stack: " << word << endl;
    }
    else if(closebra.find(infixExpression[i]) != -1)
    {
            cout << "contents of remaining ops stack: "<< endl;
            stack <string> temp;
            temp = ops;
                for (int j = 0; j < temp.size(); j++)
                {
                    cout << "\t" << temp.top() << endl;
                    temp.pop();
                }

        while (openbra.find(ops.top()) == -1)
        {


            output += " " + ops.top();
            cout << "Pushing from stack: " << ops.top() << endl;
            ops.pop();
        }

        cout << "Pushing from stack: " << ops.top() << endl;
        ops.pop();

    }

    else if (posnums.find(infixExpression[i]) != -1)
    {

        string word = "";
        while (infixExpression[i] != ' ')
        {
            word += infixExpression[i];
            i++;
            if (i== infixExpression.size())
                break;
        }
        output += " " + word;

    }

    else if (onlyops.find(infixExpression[i]) != -1)
    {

        if (ops.size() == 0)
        {
            string word = "";
        word += infixExpression[i];
        ops.push(word);
        }
        else
        {
        int o1p = 0;
        int o2p = 0;

        if ((infixExpression[i] == '+') || (infixExpression[i] == '-'))
            o1p = 0;
        else
            o1p = 1;

        if ((ops.top() == "+") || (ops.top() == "-"))
            o2p = 0;
        else
            o2p = 1;

        while ((ops.size() > 0) && (o1p <= o2p))
        {
            output += " " + ops.top();
            cout << "(odd) Pushing from stack: " << ops.top() << endl;

        if ((ops.top() == "+") || (ops.top() == "-"))
            o2p = 0;
        else
            o2p = 1;

        if (ops.size() > 0)
        {
            ops.pop();
        }
        else
        {
            break;
        }
        }

        string word = "";
        word += infixExpression[i];
        ops.push(word);
        }
    }

}

while (output[0] == ' ')
{
    output.erase(0,1);
}

return output;
    }

最佳答案

调车场算法我个人觉得还是要好好学习一下

因为你说输出像 "4 9 * 3 +",但我读到的算法和堆栈操作,应该是(像 "9 4 * 3 +")

重要的是,对数和算子进行分类后,根据设定的条件将算子出栈,全部从算子栈中弹出,压入数栈中

关于c++ - c++中的调车场算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12851033/

相关文章:

c++ - 使用 std::map 评估表达式树

c++ - 将字 rune 字 { 输出到 fstream C++

scala - 如何制作一个右关联的中缀运算符?

java - 使用java在中缀到后缀应用程序中获取错误输出

java - 比较变量时前缀和后缀递增

java - 后缀到中缀 - 括号

c++ - 如果出现 CE_FRAME 错误,com 端口通信有什么问题?

c++ - 确保全局记录器对象的生命周期比其他全局对象长

c++ - 无法理解此 boost 条件变量示例

python - 使用 python 计算数学表达式而不使用多个函数的算法