1.括号匹配思路
使用栈来实现括号匹配,首先在栈中入栈一个特殊符号(如本篇测试中的#),用这特殊符号来解决括号匹配结束栈中还有括号和栈中没有左括号但表达式的下一个还是右括号的情况;然后再进行表达式中的括号匹配,通过switch语句,出现左括号进栈,出现右括号时,先出栈栈顶元素并赋值给一个临时变量,再通过对该变量的检查来知道此次匹配是否成功,如成功则继续进行下一次循环,否则直接返回false;最后再通过特殊符号检查栈中括号是否匹配完。
public static boolean bracketMatching(String paraString) {
CharStack tempStack = new CharStack();
tempStack.push('#');
char tempChar, temppopedChar;
for (int i = 0; i < paraString.length(); i++) {
tempChar = paraString.charAt(i);
switch (tempChar) {
case '(':
case '[':
case '{':
tempStack.push(tempChar);
break;
case ')':
temppopedChar = tempStack.pop();
if (temppopedChar != '(') {
return false;
} // Of if
break;
case ']':
temppopedChar = tempStack.pop();
if (temppopedChar != '[') {
return false;
} // Of if
break;
case '}':
temppopedChar = tempStack.pop();
if (temppopedChar != '{') {
return false;
} // Of if
break;
default:
// Do nothing.
}// Of switch
} // Of for i
temppopedChar = tempStack.pop();
if (temppopedChar != '#') {
return false;
} // Of if
return true;
}// Of bracketMatching
其中charAt(i) 函数是获取字符串中i位置的字符。
2.括号匹配的简单测试
package datastructure.stack;
/**
* Char stack. I do use Stack because it is already defined in java.
*
* @author Yunhua Hu yunhuahu0528@163.com.
*/
public class CharStack {
/**
* The depth.
*/
public static final int MAX_DEPTH = 10;
/**
* The actual depth.
*/
int depth;
/**
* The data.
*/
char[] data;
/**
*********************
* Construct an empty char stack.
*********************
*/
public CharStack() {
depth = 0;
data = new char[MAX_DEPTH];
}// Of the first constructor
/**
*********************
* Overrides the method claimed in Object, the superclass of any class.
*********************
*/
public String toString() {
String resultString = "";
for (int i = 0; i < depth; i++) {
resultString += data[i];
} // Of for i;
return resultString;
}// Of toString
/**
*********************
* Push an element.
*
* @param paraChar
* The given char.
* @return Success or not.
*********************
*/
public boolean push(char paraChar) {
if (depth == MAX_DEPTH) {
System.out.println("Stack full.");
return false;
} // Of if
data[depth] = paraChar;
depth++;
return true;
}// Of push
/**
*********************
* Pop an element.
*
* @return The popped char.
*********************
*/
public char pop() {
if (depth == 0) {
System.out.println("Nothing to pop.");
return '\0';
} // Of if
char resultChar = data[depth - 1];
depth--;
return resultChar;
}// Of pop
/**
*********************
* Is the bracket matching?
*
* @param paraString
* The given expression.
* @return Match or not.
*********************
*/
public static boolean bracketMatching(String paraString) {
// Step 1. Initialize the stack through pushing a '#' at the bottom.
CharStack tempStack = new CharStack();
tempStack.push('#');
char tempChar, temppopedChar;
// Step 2. Process the string. For a string, length() is a method
// instead of a member variable.
for (int i = 0; i < paraString.length(); i++) {
tempChar = paraString.charAt(i);
switch (tempChar) {
case '(':
case '[':
case '{':
tempStack.push(tempChar);
break;
case ')':
temppopedChar = tempStack.pop();
if (temppopedChar != '(') {
return false;
} // Of if
break;
case ']':
temppopedChar = tempStack.pop();
if (temppopedChar != '[') {
return false;
} // Of if
break;
case '}':
temppopedChar = tempStack.pop();
if (temppopedChar != '{') {
return false;
} // Of if
break;
default:
// Do nothing.
}// Of switch
} // Of for i
temppopedChar = tempStack.pop();
if (temppopedChar != '#') {
return false;
} // Of if
return true;
}// Of bracketMatching
/**
*********************
* The entrance of the program.
*
* @param args
* Not used now.
*********************
*/
public static void main(String args[]) {
CharStack tempStack = new CharStack();
for (char ch = 'a'; ch < 'm'; ch++) {
tempStack.push(ch);
System.out.println("The current stack is: " + tempStack);
} // Of for i
char tempChar;
for (int i = 0; i < 12; i++) {
tempChar = tempStack.pop();
System.out.println("Poped: " + tempChar);
System.out.println("The current stack is: " + tempStack);
} // Of for i
boolean tempMatch;
String tempExpression = "[2 + (1 - 3)] * 4";
tempMatch = bracketMatching(tempExpression);
System.out.println("Is the expression " + tempExpression + " brecket matching? " + tempMatch);
tempExpression = "( ) )";
tempMatch = bracketMatching(tempExpression);
System.out.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);
tempExpression = "()()(())";
tempMatch = bracketMatching(tempExpression);
System.out.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);
tempExpression = "({}[])";
tempMatch = bracketMatching(tempExpression);
System.out.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);
tempExpression = ")(";
tempMatch = bracketMatching(tempExpression);
System.out.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);
}// Of main
}// Of CharStack
输出: