【问题标题】:Need help for conversion of Postfix to Infix using stack需要帮助使用堆栈将 Postfix 转换为 Infix
【发布时间】:2019-03-27 05:24:54
【问题描述】:

作为作业的一部分,我编写了将后缀转换为全括号中缀的代码,但此代码只能将中缀表达式转换为单个数字。我需要帮助来转换具有 2 位或更多位数字的中缀表达式。

//Here's my code. My class doesn't use collection in JAVA.
//Classes and Interfaces for stack, list, and tree are provided.
private static final String DIGITS = "0123456789";

public static String convertPostfixtoInfix(String toPostfix)
{
    LinkedStack<String> s = new LinkedStack<>();

    for(int i=0; i<toPostfix.length(); i++)
    {
        if(DIGITS.indexOf(toPostfix.charAt(i)) != -1)
        {
            s.push(toPostfix.charAt(i)+"");
        }
        else if(toPostfix.charAt(i) == " ");{}//do nothing for blank.
        else
        {
            String temp = "";
            temp += toPostfix.charAt(i);

            String num1 = s.top();
            s.pop();
            String num2 = s.top();
            s.pop();
            s.push("(" + num2 + temp + num1 + ")");
        }
    }

    return s.top();//top() is same as peek() method.
}

例如,使用此代码,

输入:4 5 - 9 2 1 + / *

输出:((4-5)*(9/(2+1)))

输入:40 5 - 9 20 1 + / *

输出:(9*(2/(0+1)))

【问题讨论】:

  • 您可能想要显示一个示例后缀输入。

标签: java stack postfix-notation


【解决方案1】:

你可以这样做。

首先,请注意。这行代码是多余的:

private static final String DIGITS = "0123456789";

如果你想检查一个字符是否是一个数字,你可以简单地用

Character.isDigit();

但为简单起见,我保留了这一行。

现在,回到您的代码。为了提供解析多个数字的功能,您所要做的就是遍历输入字符串,当遇到数字时,直到第一个非数字字符。

我对你的代码做了一些修改,向你展示了它应该如何工作的基本概念:

private static final String DIGITS = "0123456789";

public static String convertPostfixtoInfix(String toPostfix)
{
    LinkedStack<String> s = new LinkedStack<>();
    StringBuilder digitBuffer = new StringBuilder();  

    /* I've changed the 'for' to 'while' loop, 
       because we have to increment i variable inside the loop, 
       which is considered as a bad practice if done inside 'for' loop
    */
    int i = 0;
    while(i < toPostfix.length()) 
    {
        if(DIGITS.indexOf(toPostfix.charAt(i)) != -1)
        {
            //when a digit is encountered, just loop through toPostfix while the first non-digit char is encountered ...
            while (DIGITS.indexOf(toPostfix.charAt(i)) != -1) {
                digitBuffer.append(toPostfix.charAt(i++)); //... and add it to the digitBuffer
            }
            s.push(digitBuffer.toString());
            digitBuffer.setLength(0); //erase the buffer
        }
        //this if-else can also be replace with only one "if (toPostfix.charAt(i) != ' ')"
        else if(toPostfix.charAt(i) == ' ');{}//do nothing for blank.
        else
        {
            String temp = "";
            temp += toPostfix.charAt(i);

            String num1 = s.top();
            s.pop();
            String num2 = s.top();
            s.pop();
            s.push("(" + num2 + temp + num1 + ")");
        }
        i++;
    }

    return s.top();//top() is same as peek() method.
}

输入:40 5 - 9 20 1 + / *
输出:((40-5)*(9/(20+1)))

【讨论】:

    猜你喜欢
    • 2019-11-30
    • 2013-12-13
    • 2018-11-24
    • 2019-12-23
    • 2021-11-06
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多