第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

需要幫助使用堆棧將后綴轉(zhuǎn)換為 Infix

需要幫助使用堆棧將后綴轉(zhuǎn)換為 Infix

眼眸繁星 2022-09-07 16:15:35
我編寫了代碼,將后綴轉(zhuǎn)換為完全括號(hào)的后綴,作為我家庭作業(yè)的一部分,但此代碼只能將后綴表達(dá)式轉(zhuǎn)換為個(gè)位數(shù)。我需要幫助轉(zhuǎn)換包含 2 位或更多位數(shù)字的中綴表達(dá)式。//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貢獻(xiàn)1909條經(jīng)驗(yàn) 獲得超7個(gè)贊

這是你如何做到這一點(diǎn)。


首先,請(qǐng)注意一點(diǎn)。這行代碼是多余的:


private static final String DIGITS = "0123456789";

如果你想檢查一個(gè)字符是否是數(shù)字,你可以簡(jiǎn)單地做到這一點(diǎn)


Character.isDigit();

但為了簡(jiǎn)單起見(jiàn),我保留了這條線。


現(xiàn)在,回到你的代碼。為了提供解析多位數(shù)字的功能,您所要做的就是在遇到數(shù)字時(shí)循環(huán)訪問(wèn)輸入字符串,直到第一個(gè)非數(shù)字字符。


我對(duì)你的代碼進(jìn)行了一些更改,以向您展示它應(yīng)該如何工作的基本想法:


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


查看完整回答
反對(duì) 回復(fù) 2022-09-07
  • 1 回答
  • 0 關(guān)注
  • 110 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)