本文先給出思路與方法,最后將給出完整代碼:
專注于為中小企業(yè)提供成都網(wǎng)站建設(shè)、成都網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)巴青免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
算法綜述:
一、中綴表達(dá)式轉(zhuǎn)后綴表達(dá)式:
1.中綴表達(dá)式要轉(zhuǎn)后綴表達(dá)式,首先需要兩個(gè)Stack(棧),其中一個(gè)應(yīng)用于存放字符,另一個(gè)用于存放數(shù)字。
2.讀到數(shù)字直接存入數(shù)字棧中,讀到字符時(shí),要咸魚(yú)棧內(nèi)前一元素(字符)進(jìn)行比較,當(dāng)當(dāng)前(要存入的字符)優(yōu)先級(jí)大于遷移字符時(shí)才存入,否則(>=)要仿佛將棧內(nèi)元素彈出,并依次存入數(shù)字棧中。
提示:‘(' 的優(yōu)先級(jí)默認(rèn)比所有字符都小。所有字符都可以存在它后面;同時(shí)夜筆所有字符都大,可以存在所有字符后面
3.遇到 ‘)'時(shí)將棧內(nèi)所有字符依次彈出,并存入數(shù)字棧中,知道遇到 ‘(' 為止
4.當(dāng)所有字符、數(shù)字訪問(wèn)完畢時(shí),棧內(nèi)很可能還會(huì)有剩余的字符,這是將他們一次彈出,并純?nèi)鐢?shù)字棧中
小技巧:
1.存放‘+',‘-'時(shí),如果只有當(dāng)前一個(gè)數(shù)位空或者‘(' 時(shí)才進(jìn)行存入操作,否則均彈出。
2.存放 ‘*',‘/' 時(shí),只有當(dāng)前一個(gè)數(shù)位 ‘*',‘/' 時(shí)才彈出其他情況下,均存入。
附上代碼:
/* * 中綴轉(zhuǎn)后綴 */ public void toPostfix() { // TODO Auto-generated method stub int sum = 0 ;//用于記入”()“總個(gè)數(shù) int j = 0 ;//用于讀到”)“時(shí)循環(huán)出棧 String outStack = null; charnum.push(null); for( int i = 0 ; i < calculateLength ; i ++){ if ( calculate[i].equals("(")) { charnum.push(calculate[i]); sum ++ ; }else if ( calculate[i].equals(")") ) { outStack = charnum.pop();//進(jìn)入前先出一個(gè) while ( !outStack.equals("(") ){ num.push(outStack); outStack = charnum.pop(); }//最后一次outStack正好接到”(“不入棧 sum ++ ; }else if (calculate[i].equals("*")) { outStack = charnum.pop(); charnum.push(outStack); while( ( outStack == "*" || outStack == "/" ) && !(outStack == null) ){ num.push(outStack); charnum.pop();//由于前第三行又將outStack存入棧中,座椅此處再次彈出 outStack = charnum.pop(); charnum.push(outStack); } charnum.push("*"); }else if (calculate[i].equals("/")) { outStack = charnum.pop(); charnum.push(outStack); while( ( outStack == "*" || outStack == "/" ) && !(outStack == null) ){ num.push(outStack); charnum.pop();//由于前第三行又將outStack存入棧中,座椅此處再次彈出 outStack = charnum.pop(); charnum.push(outStack); } charnum.push("/"); }else if (calculate[i].equals("+")) { outStack = charnum.pop(); charnum.push(outStack); while( !(outStack=="(") && !(outStack == null) ){ num.push(outStack); charnum.pop(); outStack = charnum.pop(); charnum.push(outStack); } charnum.push("+"); }else if (calculate[i].equals("-")) { outStack = charnum.pop(); charnum.push(outStack); while( !(outStack=="(") && !(outStack == null) ){ num.push(outStack); charnum.pop(); outStack = charnum.pop(); charnum.push(outStack); } charnum.push("-"); }else { num.push(calculate[i]); } } outStack = charnum.pop(); while ( outStack != null ) { num.push(outStack); outStack = charnum.pop(); } calculateLength = calculateLength - sum ; Stackzanshi = new Stack<>(); for(int i = 0 ; i < calculateLength ; i ++ ){ zanshi.push(num.pop()); } CalculateToZero(); for(int i = 0 ; i < calculateLength ;i ++ ){ calculate[i] = zanshi.pop(); } }
二、后綴表達(dá)式計(jì)算
后綴表達(dá)式計(jì)算只遵循一個(gè)原則:
首先將表達(dá)式存在棧中
遇到符號(hào)時(shí)彈出兩個(gè)相應(yīng)的數(shù)字,進(jìn)行計(jì)算后再次 存入棧內(nèi)
最后棧內(nèi)身下的唯一一個(gè)數(shù),就是所要求的結(jié)果
/* * 后綴表達(dá)式求值 */ public String postfix() { int a = 0 , b = 0;//棧中彈出的兩數(shù) int sum ;//求兩數(shù)運(yùn)算 for (int i = 0; i < calculateLength ; i++ ) { if (i == 0) { num.push(calculate[i]); }else if (calculate[i].equals("+") ) { b = Integer.parseInt(num.pop()); a = Integer.parseInt(num.pop()); sum = a + b ; num.push(String.valueOf(sum)); }else if (calculate[i].equals("-") ) { b = Integer.parseInt(num.pop()); a = Integer.parseInt(num.pop()); sum = a - b ; num.push(String.valueOf(sum)); }else if (calculate[i].equals("*") ) { b = Integer.parseInt(num.pop()); a = Integer.parseInt(num.pop()); sum = a * b ; num.push(String.valueOf(sum)); }else if (calculate[i].equals("/") ) { b = Integer.parseInt(num.pop()); a = Integer.parseInt(num.pop()); sum = a / b ; num.push(String.valueOf(sum)); }else if (calculate[i].equals("%") ) { b = Integer.parseInt(num.pop()); a = Integer.parseInt(num.pop()); sum = a / b ; num.push(String.valueOf(sum)); }else { num.push(calculate[i]); } } return num.pop(); } }
最后附上完整代碼
//注:代碼中有很多輸出 方便讀者實(shí)時(shí)查看運(yùn)算過(guò)程中 各內(nèi)容
// 這些輸出導(dǎo)致篇幅較長(zhǎng) 大家看明白后 刪去即可
public class Text { Stacknum = new Stack<>(); //后綴用棧 中轉(zhuǎn)后數(shù)字棧 Stack charnum = new Stack<>();//中轉(zhuǎn)后字符棧 String []calculate = new String[1000];//存字符串?dāng)?shù)組 int calculateLength = 0 ;//字符串?dāng)?shù)組長(zhǎng)度 public Text() { // TODO Auto-generated constructor stub } //轉(zhuǎn)字符串?dāng)?shù)組 public void toStringArray(String input) { char charArray[] = input.toCharArray(); int number = 0;//用于導(dǎo)入多位數(shù) int j = 0 ;//用于計(jì)入當(dāng)前字符串?dāng)?shù)組的位數(shù) System.out.println(charArray.length); for(int i = 0 ; i < charArray.length ; i++){ if(charArray[i] == '('){ calculate[j++] = "("; }else if(charArray[i] == ')'){ calculate[j++] = ")"; }else if (charArray[i] == '+') { calculate[j++] = "+"; }else if (charArray[i] == '-') { calculate[j++] = "-"; }else if (charArray[i] == '*') { calculate[j++] = "*"; }else if (charArray[i] == '/') { calculate[j++] = "/"; }else if (charArray[i] == '%') { calculate[j++] = "%"; }else if (charArray[i] == '#') { calculate[j++] = "#"; }else { String str=String.valueOf(charArray[i]); number = number * 10 + Integer.parseInt(str);System.out.println("123"); if( (i + 1) == charArray.length || charArray[i+1] < '0' || charArray[i+1] > '9'){ if ( (i + 1) != charArray.length && charArray[i+1] == ' ') { System.out.println("456");i++; } calculate[j++] = String.valueOf(number); System.out.println(number); number = 0 ; } } System.out.println("---z->" + calculate[i]); } calculateLength = j-- ;//不--會(huì)將‘#'存入 } public void outPutCalculate() { for(int i = 0 ; i < calculateLength ; i ++ ){ System.out.println(calculate[i]); } } public void CalculateToZero() { for(int i = 0 ; i < calculateLength ; i ++ ){ calculate[i]= calculate[999] ; } } //中綴轉(zhuǎn)后綴 public void toPostfix() { // TODO Auto-generated method stub System.out.println("789"); int sum = 0 ;//用于記入”()“總個(gè)數(shù) int j = 0 ;//用于讀到”)“時(shí)循環(huán)出棧 String outStack = null; charnum.push(null); System.out.println(calculateLength); for( int i = 0 ; i < calculateLength ; i ++){ System.out.println(calculate[i]);//----------------------------- if ( calculate[i].equals("(")) { charnum.push(calculate[i]); System.out.println("1-1 charpush " + calculate[i]);//----------------------------- sum ++ ; }else if ( calculate[i].equals(")") ) { System.out.println("2-1 charpush " + calculate[i]);//----------------------------- outStack = charnum.pop();//進(jìn)入前先出一個(gè) System.out.println("2-1 charpush " + outStack);//----------------------------- while ( !outStack.equals("(") ){ System.out.println("2-2 charpush " + outStack);//----------------------------- num.push(outStack); outStack = charnum.pop(); }//最后一次outStack正好接到”(“不入棧 System.out.println("qiangxing 1 = " + outStack ); // outStack = charnum.pop(); System.out.println("qiangxing 2 = " + outStack ); sum ++ ; }else if (calculate[i].equals("*")) { outStack = charnum.pop(); charnum.push(outStack); System.out.println("3-2 charpush " + outStack);//----------------------------- while( ( outStack == "*" || outStack == "/" ) && !(outStack == null) ){ System.out.println("3-1 charpush " + outStack);//----------------------------- num.push(outStack); charnum.pop();//由于前第三行又將outStack存入棧中,座椅此處再次彈出 outStack = charnum.pop(); charnum.push(outStack); } System.out.println("3-3 charpush " + outStack);//----------------------------- charnum.push("*"); }else if (calculate[i].equals("/")) { System.out.println("5-1-0 charpush " + "1-1-1-1-1-1-1-1");//----------------------------- outStack = charnum.pop(); System.out.println("5-1-1 charpush " + "2-2-2-2-2-2-22-2");//----------------------------- charnum.push(outStack); System.out.println("4-1 charpush " + outStack);//----------------------------- while( ( outStack == "*" || outStack == "/" ) && !(outStack == null) ){ System.out.println("4-2 charpush " + outStack);//----------------------------- num.push(outStack); charnum.pop();//由于前第三行又將outStack存入棧中,座椅此處再次彈出 outStack = charnum.pop(); charnum.push(outStack); } System.out.println("4-3 charpush " + outStack);//----------------------------- System.out.println("5-1-2 charpush " + outStack);//----------------------------- charnum.push("/"); }else if (calculate[i].equals("+")) { outStack = charnum.pop(); charnum.push(outStack); System.out.println("5-1 charpush " + outStack);//----------------------------- while( !(outStack=="(") && !(outStack == null) ){ System.out.println("5-2 charpush " + outStack);//----------------------------- num.push(outStack); charnum.pop(); outStack = charnum.pop(); charnum.push(outStack); } System.out.println("5-3 charpush " + outStack);//----------------------------- charnum.push("+"); }else if (calculate[i].equals("-")) { outStack = charnum.pop(); charnum.push(outStack); System.out.println("6-1 charpush " + outStack);//----------------------------- while( !(outStack=="(") && !(outStack == null) ){ System.out.println("6-2 charpush " + outStack);//----------------------------- num.push(outStack); charnum.pop(); outStack = charnum.pop(); charnum.push(outStack); } System.out.println("6-3 charpush " + outStack);//----------------------------- charnum.push("-"); }else { System.out.println("7-7 " + calculate[i]); num.push(calculate[i]); } } System.out.println("匹配結(jié)束" + outStack); outStack = charnum.pop(); System.out.println("finish 1 == " + outStack); while ( outStack != null ) { num.push(outStack); outStack = charnum.pop(); System.out.println("finish 2 == " + outStack); } calculateLength = calculateLength - sum ; System.out.println( "0.0.0.0 charpush " );//----------------------------- System.out.println("sum = " + sum + " calculate = " + calculateLength + "calculateLength-sum = " + (calculateLength-sum)); System.out.println("over ~~~~~0 "); Stack zanshi = new Stack<>(); // num.pop(); for(int i = 0 ; i < calculateLength ; i ++ ){ zanshi.push(num.pop()); // System.out.println(num.pop()); } CalculateToZero(); System.out.println("over ~~~~~1 "); for(int i = 0 ; i < calculateLength ;i ++ ){ calculate[i] = zanshi.pop(); } System.out.println("over ~~~~~2 "); for(int i = 0 ; i < calculateLength ;i ++ ){ System.out.println(calculate[i]); } System.out.println("over ~~~~~3 "); // num.push("#"); } //后綴計(jì)算 public String postfix() { int a = 0 , b = 0;//棧中彈出的兩數(shù) int sum ;//求兩數(shù)運(yùn)算 for (int i = 0; i < calculateLength ; i++ ) { // System.out.println("目前符號(hào):" + calculate[i]); if (i == 0) { num.push(calculate[i]); }else if (calculate[i].equals("+") ) { b = Integer.parseInt(num.pop()); a = Integer.parseInt(num.pop()); sum = a + b ; num.push(String.valueOf(sum)); }else if (calculate[i].equals("-") ) { b = Integer.parseInt(num.pop()); a = Integer.parseInt(num.pop()); sum = a - b ; num.push(String.valueOf(sum)); }else if (calculate[i].equals("*") ) { b = Integer.parseInt(num.pop()); a = Integer.parseInt(num.pop()); sum = a * b ; num.push(String.valueOf(sum)); }else if (calculate[i].equals("/") ) { b = Integer.parseInt(num.pop()); a = Integer.parseInt(num.pop()); sum = a / b ; num.push(String.valueOf(sum)); }else if (calculate[i].equals("%") ) { b = Integer.parseInt(num.pop()); a = Integer.parseInt(num.pop()); sum = a / b ; num.push(String.valueOf(sum)); }else { num.push(calculate[i]); } } return num.pop(); } }
結(jié)果如下:
一、前綴轉(zhuǎn)后綴并輸出
其中over前為轉(zhuǎn)化后的后綴表達(dá)式
over后為計(jì)算結(jié)果
public class Main { public static void main(String[] args) { Text text = new Text(); text.toStringArray("1+2*(3-1+2)-3"); text.outPutCalculate(); text.toPostfix(); System.out.println(text.postfix()); } }
二、后綴直接輸出
注意后綴表達(dá)式時(shí)
為了實(shí)現(xiàn)多位數(shù)運(yùn)算,連續(xù)輸入一串?dāng)?shù)時(shí) ,輸入完一個(gè)數(shù)加空格
如:要計(jì)算:"1+2*(3-1+2)-3" 則輸入:"1 2 3 1-2+*+3-"
例:
public class Main { public static void main(String[] args) { Text text = new Text(); text.toStringArray("1 2 3 1-2+*+3-"); text.outPutCalculate(); System.out.println(text.postfix()); } }
輸出結(jié)果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。