亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

需要幫助使用堆棧將后綴轉換為 Infix

需要幫助使用堆棧將后綴轉換為 Infix

眼眸繁星 2022-09-07 16:15:35
我編寫了代碼,將后綴轉換為完全括號的后綴,作為我家庭作業的一部分,但此代碼只能將后綴表達式轉換為個位數。我需要幫助轉換包含 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)))
查看完整描述

1 回答

?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

這是你如何做到這一點。


首先,請注意一點。這行代碼是多余的:


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)))


查看完整回答
反對 回復 2022-09-07
  • 1 回答
  • 0 關注
  • 112 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號