1. 程式人生 > >表示式求值--棧

表示式求值--棧

  用棧的方式,將中綴轉為字尾,再進行求值。另外附加括號匹配的方法

  1 public class PostfixExpression {
  2     
  3     public static int lastvalue(String str){    //字尾表示式求值,傳入空格分割的字串
  4         Stack<Integer> stack = new Stack<Integer>();
  5         String[] data = str.split(" ");
  6         
  7         for
(int i = 0;i < data.length;i++){ 8 if(data[i].charAt(0) >= '0' && data[i].charAt(0) <= '9'){ 9 //將data[i]轉換為int資料 10 int m = Integer.parseInt(data[i]); 11 stack.push(m); 12 }else{ 13 int a = stack.pop();
14 int b = stack.pop(); 15 //構造b ? a計算結果,,將結果入棧 16 switch(data[i].charAt(0)){ 17 case '+':stack.push(b+a);break; 18 case '-':stack.push(b-a);break; 19 case '*':stack.push(b*a);break;
20 case '/':stack.push(b/a);break; 21 case '%':stack.push(b%a);break; 22 } 23 } 24 // System.out.println("---list:"+stack); 25 } 26 return stack.peek(); 27 } 28 public static String turnvalue(String s){ //中綴變換字尾 29 Stack<String> stack = new Stack<String>(); 30 StringBuffer sb = new StringBuffer(); 31 int i = 0; 32 while(i < s.length()){ 33 char c = s.charAt(i); 34 switch(c){ 35 case '+':case '-': 36 while(!stack.isEmpty() && !stack.peek().equals("(")) 37 sb.append(stack.pop()+" "); 38 stack.push(c + "");i++; 39 break; 40 case '*':case '/': 41 while(!stack.isEmpty() && (stack.peek().equals("*") || stack.peek().equals("/"))) 42 sb.append(stack.pop()+" "); 43 stack.push(c + "");i++; 44 break; 45 case '(': 46 stack.push(c + "");i++; 47 break; 48 case ')': 49 while(stack.peek() != null && !stack.peek().equals("(")) 50 sb.append(stack.pop() + " "); 51 stack.pop(); 52 i++;break; 53 default: 54 if(c == ';'){ 55 i++; 56 break; 57 } 58 while(i < s.length() && c >='0' && c <= '9'){ 59 sb.append(c);i++; 60 if(i < s.length()) 61 c = s.charAt(i); 62 } 63 sb.append(" "); 64 } 65 } 66 while(!stack.isEmpty()) 67 sb.append(stack.pop()+" "); 68 // System.out.println(sb); 69 70 return sb.toString(); 71 } 72 public static String match(String str) { //計算括號匹配 73 int x = 0; // 花括號的個數 74 int y = 0; // 方括號的個數 75 int z = 0; // 弧括號的個數 76 int k = 0; // 尖括號的個數 77 boolean isMatch = true; // 是否匹配 78 Stack<Character> stack = new Stack<Character>(); 79 80 if( str.charAt(0) == ')') 81 isMatch = false; 82 for (int i = 0; i < str.length(); i++) { 83 char ch = str.charAt(i); 84 stack.push(ch); 85 if (ch == '}') { 86 x++; 87 while(isMatch){ 88 if (stack.isEmpty()){ 89 isMatch = false; 90 break; 91 } 92 if(stack.pop() == '{') 93 break; 94 } 95 } 96 if (ch == ']') { 97 y++; 98 while(isMatch){ 99 if (stack.isEmpty()){ 100 isMatch = false; 101 break; 102 } 103 if(stack.pop() == '[') 104 break; 105 } 106 } 107 if (ch == ')') { 108 z++; 109 while(isMatch){ 110 if (stack.isEmpty()){ 111 isMatch = false; 112 break; 113 } 114 if(stack.pop() == '(') 115 break; 116 } 117 } 118 if (ch == '>') { 119 k++; 120 while(isMatch){ 121 if (stack.isEmpty()){ 122 isMatch = false; 123 break; 124 } 125 if(stack.pop() == '<') 126 break; 127 } 128 } 129 } 130 // System.out.println("list:"+stack); 131 if(!stack.isEmpty()) 132 isMatch = false; 133 134 StringBuffer sb = new StringBuffer(); 135 if (isMatch) 136 sb.append("配對。共有"+ x +"對{}、"+ y +"對[]、"+ z +"對()、"+ k +"對<>。"); 137 else 138 sb.append("不配對。"); 139 140 return sb.toString(); 141 } 142 }