需要帮助使用堆栈将后缀转换为 Infix

我编写了代码,将后缀转换为完全括号的后缀,作为我家庭作业的一部分,但此代码只能将后缀表达式转换为个位数。我需要帮助转换包含 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)))


眼眸繁星
浏览 100回答 1
1回答

jeck猫

这是你如何做到这一点。首先,请注意一点。这行代码是多余的:private static final String DIGITS = "0123456789";如果你想检查一个字符是否是数字,你可以简单地做到这一点Character.isDigit();但为了简单起见,我保留了这条线。现在,回到你的代码。为了提供解析多位数字的功能,您所要做的就是在遇到数字时循环访问输入字符串,直到第一个非数字字符。我对你的代码进行了一些更改,以向您展示它应该如何工作的基本想法:private static final String DIGITS = "0123456789";public static String convertPostfixtoInfix(String toPostfix){&nbsp; &nbsp; LinkedStack<String> s = new LinkedStack<>();&nbsp; &nbsp; StringBuilder digitBuffer = new StringBuilder();&nbsp;&nbsp;&nbsp; &nbsp; /* I've changed the 'for' to 'while' loop,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;because we have to increment i variable inside the loop,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;which is considered as a bad practice if done inside 'for' loop&nbsp; &nbsp; */&nbsp; &nbsp; int i = 0;&nbsp; &nbsp; while(i < toPostfix.length())&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if(DIGITS.indexOf(toPostfix.charAt(i)) != -1)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //when a digit is encountered, just loop through toPostfix while the first non-digit char is encountered ...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (DIGITS.indexOf(toPostfix.charAt(i)) != -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; digitBuffer.append(toPostfix.charAt(i++)); //... and add it to the digitBuffer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s.push(digitBuffer.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; digitBuffer.setLength(0); //erase the buffer&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //this if-else can also be replace with only one "if (toPostfix.charAt(i) != ' ')"&nbsp; &nbsp; &nbsp; &nbsp; else if(toPostfix.charAt(i) == ' ');{}//do nothing for blank.&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String temp = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp += toPostfix.charAt(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String num1 = s.top();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s.pop();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String num2 = s.top();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s.pop();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s.push("(" + num2 + temp + num1 + ")");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; }&nbsp; &nbsp; return s.top();//top() is same as peek() method.}输入: 40 5 - 9 20 1 + / *输出: ((40-5)*(9/(20+1)))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java