1 /*將中綴表達式--轉化為后綴表達式--屬於棧的一種應用 2 *具體思路: 3 *1.掃描字符串,遇到操作數字符直接不管,存到一個字符串里邊 4 *2.操作符優先級比較--定義了方法 5 * 棧中優先級高:出棧存進字符串,當前的操作符進棧 6 * 當前操作符優先級高,直接進棧 7 * 對於左邊括號直接進棧,右邊括號涉及棧中出棧 8 *3.掃描完了在將棧中的出棧和操作數存儲在一起 9 *轉化完直觀的表現:操作數相對位置不變,操作符相對位置相反 10 * */ 11 public class IntoSuffixExpression { 12 private CharStack stack; 13 private String input; 14 private String output = ""; 15 16 public IntoSuffixExpression(String in) { 17 input = in; 18 stack = new CharStack(input.length()); 19 } 20 21 //轉化為后綴表達式 22 public String doTrans(){ 23 for(int i = 0; i < input.length();i++){ 24 char ch = input.charAt(i); 25 stack.displayStack("For " + ch + " "); 26 /*分析讀到的元素的類型--操作符/操作數(默認添加到輸出字符串中) 27 *對於操作符分類並進行優先級比較 乘除優先級高於加減*/ 28 switch(ch){ 29 case '+': 30 case '-': 31 gotOper(ch,1);//需要判斷優先級prec1:設定的是當前的優先級 32 break; 33 case '*': 34 case '/': 35 gotOper(ch,2);//需要判斷優先級 36 break; 37 case '(': 38 stack.push(ch);//左括號優先級最高直接進棧 39 break; 40 case ')': 41 gotParent(ch);//碰到右邊括號涉及出棧 42 break; 43 default: 44 output = output + ch;//是操作數就直接存在輸出字符串中 45 break; 46 } 47 } 48 //打印戰中的操作符返回后綴表達式字符串 49 while(!stack.isEmpty()){ 50 stack.displayStack("while"); 51 output = output + stack.pop(); 52 } 53 stack.displayStack("end"); 54 return output; 55 } 56 57 //比較當前的操作符與棧里面的操作符優先級--進行不同的操作 58 public void gotOper(char opThis, int prec1) { 59 while(!stack.isEmpty()){ 60 char opTop = stack.pop(); 61 if(opTop == '('){ //如果棧中頂是左邊括號就進去跳出循環 62 stack.push(opTop); 63 break; 64 } 65 else{ 66 int prec2; 67 //記錄棧中操作符優先級--加減優先級是1乘除優先級是2 68 if(opTop == '+' || opTop == '-'){ 69 prec2 = 1; 70 } 71 else{ 72 prec2 = 2; 73 } 74 //棧中優先級小結束比較--當前執行進棧操作 75 if(prec2 < prec1){ 76 stack.push(opTop); 77 break; 78 } 79 //棧中優先級大就出棧存儲在字符串中 80 else{ 81 output = output + opTop; 82 } 83 } 84 } 85 //棧為空就直接進棧或者遇到左邊括號也是直接進棧或者棧存儲比棧中優先級小的 86 stack.push(opThis); 87 } 88 89 //越到右邊括號進行出棧的操作--直到遇到左邊括號 90 public void gotParent(char ch){ 91 while(!stack.isEmpty()){ 92 char chx = stack.pop(); 93 if(chx == '('){ 94 break; 95 } 96 else{ 97 output = output + chx; 98 } 99 } 100 } 101 102 103 104 }
1 /*計算后綴表達式的值--也用的是棧 2 * */ 3 public class CalculateSuffixExpression { 4 private MyStack stack; 5 private String input; 6 7 //注意這里傳入的字符串是后綴的表達式 8 public CalculateSuffixExpression(String in) { 9 input = in; 10 } 11 12 //數字進棧,操作符棧中元素出棧對應相應的操作 13 public long calculateValue(){ 14 stack = new MyStack(20); 15 char ch; 16 int j; 17 long num1,num2,interAns; 18 for(j = 0;j < input.length();j++){ 19 ch = input.charAt(j); 20 stack.displayStack("" + ch + ""); 21 if(ch >= '0' && ch <= '9'){ //操作數 22 stack.push((long)(ch - '0')); 23 } 24 else{ 25 num2 = stack.pop(); 26 num1 = stack.pop(); 27 switch(ch){ 28 case'+': 29 interAns = num1 + num2; 30 break; 31 case'-': 32 interAns = num1 - num2; 33 break; 34 case'*': 35 interAns = num1 * num2; 36 break; 37 case'/': 38 interAns = num1 / num2; 39 break; 40 default: 41 interAns = 0; 42 } 43 stack.push(interAns); 44 }//end else 45 }//end for 46 interAns = stack.pop(); 47 return interAns; 48 }//end method 49 50 public static void main(String[] args){ 51 //轉化后綴表達式 52 String s = "3+(6*9)+2-6/(3-1)"; 53 IntoSuffixExpression is = new IntoSuffixExpression(s); 54 String out = is.doTrans(); 55 56 //計算后綴表達式 57 CalculateSuffixExpression cse = new CalculateSuffixExpression(out); 58 long result = cse.calculateValue(); 59 System.out.println(result); 60 61 } 62 63 }