-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNaloga6.java
150 lines (129 loc) · 4.79 KB
/
Naloga6.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import java.io.*;
import java.util.*;
public class Naloga6 {
private String inputFilename;
private String outputFilename;
private BufferedReader reader;
private PrintWriter writer;
private Deque<String> outputStack = new ArrayDeque<>();
private Deque<String> operatorStack = new ArrayDeque<>();
private Map<String, Integer> operatorPrecedences = new HashMap<>();
public static void main(String[] args) throws Exception {
Naloga6 program = new Naloga6(args[0], args[1]);
program.initialise();
program.callShuntingYardAlg();
program.cleanUp();
}
public int getTreeHeight(int currentTreeHeight, Deque<String> remainingStack) {
String token = remainingStack.pop();
if (!isOperator(token)) {
return currentTreeHeight + 1;
}
if (token.equals("NOT")) {
return getTreeHeight(currentTreeHeight + 1, remainingStack);
}
int leftHeight = getTreeHeight(currentTreeHeight + 1, remainingStack);
int rightHeight = getTreeHeight(currentTreeHeight + 1, remainingStack);
return Math.max(leftHeight, rightHeight);
}
public void callShuntingYardAlg() throws Exception {
StringTokenizer input = new StringTokenizer(reader.readLine());
Deque<String> tokenStack = new ArrayDeque<>();
while (input.hasMoreTokens()) {
addInputToTokenStack(tokenStack, input.nextToken());
}
while (!tokenStack.isEmpty()) {
evaluateToken(tokenStack.pop());
}
while (!operatorStack.isEmpty()) {
if (Objects.equals(operatorStack.peek(), "(")) {
throw new Exception("Parentheses do not match!");
}
String operator = operatorStack.pop();
outputStack.push(operator);
}
print();
}
private void addInputToTokenStack(Deque<String> tokenStack, String token) {
if (token.startsWith("(")) {
tokenStack.push(")");
addInputToTokenStack(tokenStack, token.substring(1));
return;
}
if (token.endsWith(")")) {
addInputToTokenStack(tokenStack, token.substring(0, token.length() - 1));
tokenStack.push("(");
return;
}
tokenStack.push(token);
}
private void evaluateToken(String token) throws Exception {
if (isOperator(token)) {
while (isTheTopOfTheOperatorStackAnOperator() && hasHigherPrecedence(token, operatorStack.peek())) {
String topOperator = operatorStack.pop();
outputStack.push(topOperator);
}
operatorStack.push(token);
} else if (token.equals("(")) {
operatorStack.push(token);
} else if (token.equals(")")) {
while (!operatorStack.peek().equals("(")) {
if (operatorStack.isEmpty()) {
throw new Exception("Parentheses do not match!");
}
String operator = operatorStack.pop();
outputStack.push(operator);
}
if (!Objects.equals(operatorStack.peek(), "(")) {
throw new Exception("Parentheses do not match!");
}
operatorStack.pop();
} else {
outputStack.push(token);
}
}
private boolean isTheTopOfTheOperatorStackAnOperator() {
String token = operatorStack.peek();
return token != null && isOperator(token);
}
private boolean hasHigherPrecedence(String o1, String o2) {
return operatorPrecedences.get(o1) > operatorPrecedences.get(o2);
}
private boolean isOperator(String token) {
return token.equals("OR") || token.equals("AND") || token.equals("NOT");
}
public void print() {
writer.println(String.join(",", outputStack));
writer.println(getTreeHeight(0, outputStack));
writer.flush();
}
public Naloga6(String inputFilename, String outputFilename) {
this.inputFilename = inputFilename;
this.outputFilename = outputFilename;
}
public void cleanUp() {
try {
reader.close();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void initialise() {
initialiseReaders();
initialiseOperatorPrecedences();
}
private void initialiseOperatorPrecedences() {
operatorPrecedences.put("NOT", 1);
operatorPrecedences.put("AND", 2);
operatorPrecedences.put("OR", 3);
}
private void initialiseReaders() {
try {
reader = new BufferedReader(new FileReader(inputFilename));
writer = new PrintWriter(outputFilename);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}