C++ 将中缀转换为后缀表示法

C++ 将中缀转换为后缀表示法,c++,postfix-notation,C++,Postfix Notation,我正在做中缀到后缀的表示法。我的程序进行编译,尽管出于某种原因,它不会接受任何中缀表达式,只接受后缀表达式。这与我想做的正好相反。这是我的节目: #include <iostream> #include <string> #include <sstream> #include "stack" using namespace std; string infixexpr (istream& in) { //Holds value in comp

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

#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;
}
#包括
#包括
#包括
#包括“堆栈”
使用名称空间std;
字符串infixexpr(istream&in)
{
//在计算中有价值
堆栈后固定堆栈;
//用于从表达式中读入字符
char ch;
//用于从表达式中读入数字
int-num;
//用于从堆栈中删除中缀表达式
字符串lexpr,rexpr;
ch=in.peek();
while(ch!=EOF)
{
//如果我们有一个空白字符,跳过它并继续
//循环的结束。
if(isspace(ch))
{
ch=in.get();
ch=in.peek();
继续;
}
//非空格字符位于输入流旁边
//如果下一个字符是数字,请读取并转换它
//将字符串放入后缀堆栈中
if(isdigit(ch))
{
在>>num中;
//用于转换字符串
ostringstream numberstr;
//使用sstream转换为数字
numberstr哦,天哪,从哪里开始。你可能应该把这个带到办公室,但是让我们开始:

  • 你没有描述“崩溃”,所以我们不知道你在观察什么样的失败
  • 您的“如果下一个字符是数字,请读取它并将其转换为字符串,然后将字符串放入后缀堆栈”代码将操作数推送到堆栈上。但在Dijkstra中,您似乎正在尝试实现,堆栈上只有运算符
  • “如果运算符弹出堆栈上存储的两个后缀表达式,则将运算符放在后面”代码不会防止从空堆栈弹出项
  • 同样的代码也会从堆栈中弹出两个项目,但您只在“56+2”中的“56”上推了一个项目。由于您强迫我猜测,我猜这就是程序崩溃的地方
  • 同样的代码也将结果推送到堆栈上,这是如何不实现调车场算法的另一个示例

代码审查是针对工作代码的。“它不会接受任何中缀表达式,只接受后缀表达式。这与我想做的正好相反。”-这是调试,代码审查可能会解决这个问题。从:“据我所知,代码行得通吗?”代码符合并运行。崩溃发生在lexpr=postfixstack.top()处;很抱歉没有提供详细信息。@user110182这是我猜测的。没有有效的中缀表达式可以在堆栈上留下两个项目。您是否单独测试了堆栈代码?它工作可靠吗?如果尝试从空堆栈中弹出一些内容,会发生什么情况?