Day15 栈的应用(括号匹配)
1、 charAt() 就是返回指定索引位置的char值;
1 public char charAt(int index) { 2 if ((index < 0) || (index >= value.length)) { 3 throw new StringIndexOutOfBoundsException(index); 4 } 5 return value[index]; 6 }
2、在 bracketMatching(String paraString) 里,首先放一个 '#' 进去,之后 '#' 就会一直在栈底,for循环结束之后再取出一个字符,如果这个字符不是 '#' ,就说明匹配失败;
1 public class CharStack { 2 /** 3 * The depth. 4 */ 5 public static final int MAX_DEPTH = 10; 6 7 /** 8 * The actual depth. 9 */ 10 int depth; 11 12 /** 13 * The data 14 */ 15 char[] data; 16 17 public CharStack() { 18 depth = 0; 19 data = new char[MAX_DEPTH]; 20 }// Of the first constructor 21 22 /** 23 * Overrides the method claimed in Object, the superclass of any class. 24 */ 25 public String toString() { 26 String resultString = ""; 27 for (int i = 0; i < depth; i++) { 28 resultString += data[i]; 29 } // Of for i 30 31 return resultString; 32 }// Of toString 33 34 /** 35 * Push an element. 36 * 37 * @param paraChar The given char. 38 * @return Success or not. 39 */ 40 public boolean push(char paraChar) { 41 if (depth == MAX_DEPTH) { 42 System.out.println("Stack full."); 43 return false; 44 } // Of if 45 46 data[depth] = paraChar; 47 depth++; 48 49 return true; 50 }// Of push 51 52 /** 53 * Pop an element. 54 * 55 * @return The popped char. 56 */ 57 public char pop() { 58 if (depth == 0) { 59 System.out.println("Nothing to pop."); 60 return '\0'; 61 } // Of if 62 63 char resultChar = data[depth - 1]; 64 depth--; 65 66 return resultChar; 67 }// Of pop 68 69 public static boolean bracketMatching(String paraString) { 70 // Step 1. Initialize the stack through pushing a '#' at the bottom. 71 CharStack tempStack = new CharStack(); 72 tempStack.push('#'); 73 char tempChar, tempPoppedChar; 74 75 // Step 2. Process the string. For a string, length() is a method 76 // instead of a member variable 77 for (int i = 0; i < paraString.length(); i++) { 78 tempChar = paraString.charAt(i); 79 80 switch (tempChar) { 81 case '(': 82 case '[': 83 case '{': 84 tempStack.push(tempChar); 85 break; 86 case ')': 87 tempPoppedChar = tempStack.pop(); 88 if (tempPoppedChar != '(') { 89 return false; 90 }// Of if 91 break; 92 case ']': 93 tempPoppedChar = tempStack.pop(); 94 if (tempPoppedChar != '[') { 95 return false; 96 }// Of if 97 break; 98 case '}': 99 tempPoppedChar = tempStack.pop(); 100 if (tempPoppedChar != '{') { 101 return false; 102 }// Of if 103 break; 104 default: 105 // Do nothing. 106 }// Of Switch 107 }// Of for i 108 109 tempPoppedChar = tempStack.pop(); 110 if (tempPoppedChar != '#') { 111 return false; 112 }// Of if 113 114 return true; 115 }// Of bracketMatching 116 117 /** 118 * The entrance of the program. 119 * 120 * @param args 121 */ 122 public static void main(String args[]) { 123 CharStack tempStack = new CharStack(); 124 125 for (char ch = 'a'; ch < 'm'; ch++) { 126 tempStack.push(ch); 127 System.out.println("The current stack is: " + tempStack); 128 } // Of for ch 129 130 char tempChar; 131 for (int i = 0; i < 12; i++) { 132 tempChar = tempStack.pop(); 133 System.out.println("Popped: " + tempChar); 134 System.out.println("The current stack is: " + tempStack); 135 } // Of for i 136 137 boolean tempMatch; 138 String tempExpression = "[2 + (1 - 3)] * 4"; 139 tempMatch = bracketMatching(tempExpression); 140 System.out.println("Is the expression " + tempExpression + "bracket matching? " + tempMatch); 141 142 tempExpression = "( ))"; 143 tempMatch = bracketMatching(tempExpression); 144 System.out.println("Is the expression " + tempExpression + "bracket matching? " + tempMatch); 145 146 tempExpression = "()()(())"; 147 tempMatch = bracketMatching(tempExpression); 148 System.out.println("Is the expression " + tempExpression + "bracket matching? " + tempMatch); 149 150 tempExpression = "({}[])"; 151 tempMatch = bracketMatching(tempExpression); 152 System.out.println("Is the expression " + tempExpression + "bracket matching? " + tempMatch); 153 154 tempExpression = ")("; 155 tempMatch = bracketMatching(tempExpression); 156 System.out.println("Is the expression " + tempExpression + "bracket matching? " + tempMatch); 157 158 }// Of main 159 160 }// Of CharStack

浙公网安备 33010602011771号