c++ - 将中缀转换为后缀表示法

标签 c++ postfix-notation

我正在对后缀表示法进行中缀。我的程序可以编译,尽管出于某种原因它不会接受任何中缀表达式,只接受后缀表达式。这与我想做的相反。这是我的程序:

#include <iostream>
#include <string>
#include <sstream>
#include "stack"
using namespace std;

string infixexpr (istream&  in)
{
    //Holds value in computation
    stack<string> postfixstack;
    //used to to read in characters from the expression
    char ch;
    // used to read in numbers from expression
    int num;
    // Used to remove infix expressions from stack
    string lexpr, rexpr;
    ch = in.peek();
    while ( ch != EOF)
    {
        //If we have a whitespace character skip it and continue with
        // the end of the loop.
        if(isspace(ch))
        {
            ch = in.get();
            ch =in.peek();
            continue;
        }

        //nonspace character is next to input stream
        // if the next character is a number read it and convert it
        // to string then put the string onto the postfix stack
        if (isdigit (ch))
        {
            in >> num;
            // use to convert string
            ostringstream numberstr;
            // convert to number using sstream
            numberstr << num;
            // Push the representing string onto stack0
            postfixstack.push(numberstr.str());
            ch = in.peek();
            continue;
        }

        // if operator pop the two postfix expressions
        // stored on the stack, put the operator after
        postfixstack.pop();
        lexpr = postfixstack.top();
        postfixstack.pop();

        if (ch == '+' || ch == '-' || + ch == '*' || ch == '/' || ch == '%')
            postfixstack.push(rexpr + " " + lexpr + " " + ch);
        else
        {
            cout << "Error in input expression" << endl;
            exit(1);
        }
        ch = in.get();
        ch = in.peek();
    }
    return postfixstack.top();
}

int main()
{
    string input;
    cout <<  "Enter a infix expression to convert to postfix,"
         << " \nor a blank line to quit the program:";
    getline(cin,input);

    while (input.size() != 0 )
    {
        //convert string to a string stream
        istringstream inputExpr(input);
        cout << "the infix equavilent is: "
             << infixexpr(inputExpr) << endl;
            cout << "Enter a infix Expression to evaluate: ";
            getline(cin,input);
    }

    return 0;
}

例如程序运行是这样的:

  • 如果我输入 56 2 +(在每个数字或运算符后添加空格)
  • 我会得到 56 2 +,这正是我想要的。但是如果我输入
  • 56 + 2,程序会崩溃。

如果您想查看我的堆栈类和 header ,如果这是问题所在,请告诉我。我可以在回复下发帖。

最佳答案

哦,天哪,从哪里开始。 你应该把这个带到Code Review Stack Exchange相反,但让我们开始吧:

  • 您没有描述“崩溃”,所以我们不知道您观察到的故障是什么。
  • 您的“如果下一个字符是数字,则读取它并将其转换为字符串,然后将字符串放入后缀堆栈”代码将操作数压入堆栈。但在 Dijkstra 的 shunting-yard algorithm ,您似乎正在尝试实现,只有运算符进入堆栈。
  • 您的“if operator pop 存储在堆栈中的两个后缀表达式,将运算符放在后面”代码无法防止从空堆栈中弹出项目。
  • 同样的代码也从堆栈中弹出两项,但你只压入了一项——“56 + 2”中的“56”。既然你逼我猜,我猜这就是程序崩溃的地方。
  • 同样的代码还将结果压入堆栈,这是另一个如何不实现调车场算法的示例。

关于c++ - 将中缀转换为后缀表示法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20307452/

相关文章:

c++ - 包含 header 如何导致链接器错误?

c++ - 前缀括号的中缀

java - 使用堆栈和运算符优先级的中缀到后缀

c++ - 父线程终止时子线程是否退出

c++ - posix timer_create() 函数在 linux 上导致内存泄漏

c++ - 避免许多类型转换

c++ - C++中的不明确访问错误

coding-style - 我可以写 {x,a,b}//Do[...,#]& 而不是 Do[...,{x,a,b}] 吗?

c# - 解析表达式(带有自定义函数和操作)

c++ - 使用后缀表示法在 C++ 中输入数字