forked from shijiebei2009/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBracketsApp.java
89 lines (80 loc) · 2.49 KB
/
BracketsApp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package cn.codepub.algorithms.stack;
import cn.codepub.algorithms.utils.StackX;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* <p>
* Created with IntelliJ IDEA. 2016/1/8 20:17
* </p>
* <p>
* ClassName:BracketsApp
* </p>
* <p>
* Description:判断括号是否匹配,For Example:(((())))
* </P>
*
* @author Wang Xu
* @version V1.0.0
* @since V1.0.0
*/
public class BracketsApp {
public static void main(String[] args) throws IOException {
String input;
while (true) {
System.out.println("Enter string containing delimiters:");
System.out.flush();
input = getString();
if (input.equals("")) {
break;
}
BracketChecker theChecker = new BracketChecker(input);
theChecker.check();
}
}
public static String getString() throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String str = br.readLine();
return str;
}
}
class BracketChecker {
private String input;
public BracketChecker(String in) {
this.input = in;
}
public void check() {
int stackSize = input.length();
StackX<Character> theStack = new StackX(stackSize);
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 + 1));
}
} else {
System.out.println("Error:" + ch + " at " + (j + 1));
}
default:
break;
}
}
if (!theStack.isEmpty()) {
System.out.println("Error:missing right delimiter");
} else {
System.out.println("It is legitimate!");
}
}
}