繁体   English   中英

将中缀转换为后缀表示法

[英]Converting infix to 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,程序将崩溃。

如果您想查看我的堆栈类和标头,如果有问题,请告诉我。 我可以在回复中发布它。

哦,天哪,从哪里开始。 您可能应该将其带到Code Review Stack Exchange ,但让我们开始吧:

  • 您没有描述“崩溃”,所以我们不知道您正在观察到什么故障。
  • 您的“ 如果下一个字符是数字,则将其读取并将其转换为字符串,然后将字符串放入后缀堆栈 ”代码将操作数推入堆栈。 但是在Dijkstra的shunting-yard算法中 (您似乎正在尝试实现),只有运算符进入堆栈。
  • 您的if运算符会弹出存储在堆栈中的两个后缀表达式,将运算符放在 “代码之后无法防止将项目从空堆栈中弹出。
  • 相同的代码还会从堆栈中弹出两个项目,但是您只压入了一个-“ 56 + 2”中的“ 56”。 由于您是在强迫我猜测,因此我猜测这是程序崩溃的地方。
  • 相同的代码还将结果压入堆栈,这是不实现分流码算法的另一个示例。

暂无
暂无

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

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