forked from SEA-CS-Salinas/stacks-lab-2-JDovalina
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyntaxCheckRunner.java
More file actions
86 lines (65 loc) · 2.34 KB
/
SyntaxCheckRunner.java
File metadata and controls
86 lines (65 loc) · 2.34 KB
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
//(c) A+ Computer Science
//www.apluscompsci.com
//Name - Jacob Dovalina
/*Lab Description : Read in a group of symbols and check to see if the appropriate opening symbol correctly matches up with the appropriate closing symbol.
The opening symbols are “{(<[“ and the appropriate closing symbols are “})>]“.
You must read in and analyze each group.
If you were to read in {[]}, you would have a correct balance of opening and closing symbols.
If you were to read in {[}], you would not have a correct balance of opening and closing symbols.
Sample Data :
(abc(*def)
[{}]
[
[{<()>}]
{<html[value=4]*(12)>{$x}}
[one]<two>{three}(four)
car(cdr(a)(b)))
car(cdr(a)(b))
Sample Output :
(abc(*def) is incorrect.
[{}] is correct.
[ is incorrect.
[{<()>}] is correct.
{<html[value=4]*(12)>{$x}} is correct.
[one]<two>{three}(four) is correct.
car(cdr(a)(b))) is incorrect.
car(cdr(a)(b)) is correct.
*/
import java.util.Stack;
public class SyntaxCheckRunner {
public static void main(String[] args) {
// Sample test cases
String[] testCases = {
"(abc(*def)",
"[{}]",
"[",
"[{<()>}]",
"{<html[value=4]*(12)>{$x}}",
"[one]<two>{three}(four)",
"car(cdr(a)(b)))",
"car(cdr(a)(b))"
};
for (String testCase : testCases) {
boolean result = isBalanced(testCase);
System.out.println(testCase + " is " + (result ? "correct." : "incorrect."));
}
}
public static boolean isBalanced(String symbols) {
Stack<Character> stack = new Stack<>();
String opening = "{([<";
String closing = "})]>";
for (char ch : symbols.toCharArray()) {
if (opening.indexOf(ch) != -1) {
// Push opening symbols onto the stack
stack.push(ch);
} else if (closing.indexOf(ch) != -1) {
// Check if stack is empty or the top of the stack doesn't match
if (stack.isEmpty() || closing.indexOf(ch) != opening.indexOf(stack.pop())) {
return false;
}
}
}
// If the stack is empty, the string is balanced
return stack.isEmpty();
}
}