-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
179 lines (150 loc) · 6.15 KB
/
Main.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import java.util.*;
import java.io.IOException;
import java.util.logging.*;
public class Main {
private static final Logger LOGGER = Logger.getLogger(Main.class.getName());
static {
try {
FileHandler fileHandler = new FileHandler("exceptions.log", true);
SimpleFormatter formatter = new SimpleFormatter();
fileHandler.setFormatter(formatter);
LOGGER.addHandler(fileHandler);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Calculator calculator = new Calculator();
PreOrderCalculator preOrderCalculator = new PreOrderCalculator();
PostOrderCalculator postOrderCalculator = new PostOrderCalculator();
int mode = 0; // 0 for normal, 1 for pre-order, 2 for post-order
while (true) {
try {
System.out.println("Enter an expression (e.g., 1 + 2), or 'q' to quit:");
String input = scanner.nextLine();
if (input.equalsIgnoreCase("q")) {
break;
}
Node root;
if (mode == 1) {
String[] tokens = input.split("\\s+");
Queue<String> tokenQueue = new LinkedList<>(Arrays.asList(tokens));
root = buildExpressionTreePreOrder(tokenQueue);
if (!tokenQueue.isEmpty()) {
throw new IllegalArgumentException("Invalid pre-order expression");
}
} else if (mode == 2) {
String[] tokens = input.split("\\s+");
Stack<String> tokenStack = new Stack<>();
tokenStack.addAll(Arrays.asList(tokens));
root = buildExpressionTreePostOrder(tokenStack);
if (!tokenStack.isEmpty()) {
throw new IllegalArgumentException("Invalid post-order expression");
}
} else {
root = buildExpressionTree(input);
}
switch (mode) {
case 0:
System.out.println("Result: " + calculator.calculate(root));
break;
case 1:
System.out.println("Pre-order: " + preOrderCalculator.calculatePreOrder(root));
break;
case 2:
System.out.println("Post-order: " + postOrderCalculator.calculatePostOrder(root));
break;
}
System.out.println("Enter 'n' for normal mode, 'p' for pre-order mode, 'po' for post-order mode:");
String modeInput = scanner.nextLine();
switch (modeInput.toLowerCase()) {
case "n":
mode = 0;
break;
case "p":
mode = 1;
break;
case "po":
mode = 2;
break;
default:
System.out.println("Invalid mode. Staying in current mode.");
}
} catch (IllegalArgumentException e) {
LOGGER.log(Level.SEVERE, "An error occurred", e);
System.out.println("Error: " + e.getMessage());
System.out.println("Please try again.");
}
}
scanner.close();
}
private static Node buildExpressionTree(String expression) {
String[] tokens = expression.split("\\s+");
Stack<String> operatorStack = new Stack<>();
Stack<Node> operandStack = new Stack<>();
for (String token : tokens) {
if (isOperator(token)) {
while (!operatorStack.isEmpty() && isOperator(operatorStack.peek())) {
Node right = operandStack.pop();
Node left = operandStack.pop();
Node node = new Node(operatorStack.pop().charAt(0));
node.left = left;
node.right = right;
operandStack.push(node);
}
operatorStack.push(token);
} else {
operandStack.push(new Node(Double.parseDouble(token)));
}
}
while (!operatorStack.isEmpty()) {
Node right = operandStack.pop();
Node left = operandStack.pop();
Node node = new Node(operatorStack.pop().charAt(0));
node.left = left;
node.right = right;
operandStack.push(node);
}
return operandStack.pop();
}
private static Node buildExpressionTreePreOrder(Queue<String> tokens) {
if (tokens.isEmpty()) {
return null;
}
String token = tokens.poll();
Node node;
if (isOperator(token)) {
node = new Node(token.charAt(0));
node.left = buildExpressionTreePreOrder(tokens);
node.right = buildExpressionTreePreOrder(tokens);
if (node.left == null || node.right == null) {
throw new IllegalArgumentException("Invalid pre-order expression");
}
} else {
node = new Node(Double.parseDouble(token));
}
return node;
}
private static Node buildExpressionTreePostOrder(Stack<String> tokens) {
if (tokens.isEmpty()) {
return null;
}
String token = tokens.pop();
Node node;
if (isOperator(token)) {
node = new Node(token.charAt(0));
node.right = buildExpressionTreePostOrder(tokens);
node.left = buildExpressionTreePostOrder(tokens);
if (node.left == null || node.right == null) {
throw new IllegalArgumentException("Invalid post-order expression");
}
} else {
node = new Node(Double.parseDouble(token));
}
return node;
}
private static boolean isOperator(String token) {
return "+-*/^~SC".contains(token);
}
}