Day 15 —— Bracket Matching
1. Background
今天是学习java的第十五天,今天进行的是用栈实现字符串的括号匹配。
2. Description
2.1 String.length()
这个是java中string自带的方法。Returns the length of this string. The length is equal to the number of Unicode code units in the string.这是VScode对它的介绍,用途是返回字符串中字符的个数。
2.2 String.charAt()
这个也是String自带的方法。Returns the char
value at the specified index. An index ranges from 0
to length() - 1
. The first char
value of the sequence is at index 0
, the next at index 1
, and so on, as for array indexing.
If the char
value specified by the index is a surrogate, the surrogate value is returned.
3. Code
package datastructure;
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
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 element.
* @return Success or not.
* ************
*/
public boolean Push(char paraChar) {
if (depth == MAX_DEPTH) {
System.out.println("Stack full");
return false;
}
data[depth] = paraChar;
depth++;
return true;
} // Of Push
/**
*************
* Pop an element.
* @return The poped 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
/**
* 用栈实现字符串中的括号匹配。
* @param paraString The given String.
* @return Success or not.
*/
public static boolean bracketMatching(String paraString) {
// Step 1.
CharStack tempStack = new CharStack();
char tempChar, tempPopChar;
tempStack.Push('#');
// Step 2.
for (int i = 0; i < paraString.length(); i++) {
tempChar = paraString.charAt(i);
switch (tempChar) {
case '(':
case '[':
case '{':
tempStack.Push(tempChar);
break;
case ')':
tempPopChar = tempStack.Pop();
if (tempPopChar != '(') {
return false;
} // Of if
break;
case ']':
tempPopChar = tempStack.Pop();
if (tempPopChar != '[') {
return false;
} // Of if
break;
case '}':
tempPopChar = tempStack.Pop();
if (tempPopChar != '{') {
return false;
} // Of if
break;
default:
// Do nothing
} // Of switch
} // Of for i
tempPopChar = tempStack.Pop();
if (tempPopChar != '#') {
return false;
} // Of if
return true;
} // Of bracketMatching
/**
* The entrance of program.
*
* @param args Not used now.
*/
public static void main(String[] args) {
CharStack tempStack = new CharStack();
for (char i = 'a'; i < 'm'; i++) {
tempStack.Push(i);
System.out.println("The 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 stack is: " + tempStack);
} // Of for i
boolean tempMatch;
String tempExpression = "[2 + (1 - 3)] * 4";
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);
tempExpression = ")(";
tempMatch = bracketMatching(tempExpression);
System.out.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);
} // Of main
} // Of CharStack
运行结果: