| |
9. 50. 3. 栈:定界符匹配 |
|
This time we use the Stack class from Java library. |
Using stack to check matching brackets |
Examples: |
- c[d] // correct
- a{b[c]d}e // correct
- a{b(c]d}e // not correct; ] doesn't match (
- a[b{c}d]e} // not correct; nothing matches final }
- a{b(c) // not correct; Nothing matches opening {
|
import java.util.Stack;
class BracketChecker {
private String input;
public BracketChecker(String in) {
input = in;
}
public void check() {
Stack<Character> theStack = new Stack<Character>();
for (int j = 0; j < input.length(); j++) {
char ch = input.charAt(j);
switch (ch) {
case '{':
case '[':
case '(':
theStack.push(ch);
break;
case '}':
case ']':
case ')':
if (!theStack.isEmpty()) {
char chx = theStack.pop();
if ((ch == '}' && chx != '{') || (ch == ']' && chx != '[') || (ch == ')' && chx != '('))
System.out.println("Error: " + ch + " at " + j);
} else
System.out.println("Error: " + ch + " at " + j);
break;
default:
break;
}
}
if (!theStack.isEmpty()){
System.out.println("Error: missing right delimiter");
}
}
}
public class MainClass {
public static void main(String[] args) {
String input;
input = "[]]()()";
BracketChecker theChecker = new BracketChecker(input);
theChecker.check();
}
}
|
|
Error: ] at 2 |
|