-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.java
More file actions
497 lines (417 loc) · 15.2 KB
/
main.java
File metadata and controls
497 lines (417 loc) · 15.2 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
* Main
*
* Ver. 1.0.0
*
* Free for using, without license
*
* Program which implements simple calculator based on Stack Model and
* converting expression into postfix form.
*/
import java.util.Scanner;
import java.util.ArrayList;
/**
* The Main class provides general functionality of the program.
*
* @author Artur Mukhutdinov
* @version 1.0.0 15 December 2022
*/
final class OptionalProject {
private OptionalProject() {
}
public static void main(String[] args) {
Converter converter = new Converter();
Scanner s = new Scanner(System.in);
System.out.println("Please, write your expression separated by spaces! (Example: (a + b) - c * d):"
+ "\n" + "Expression in infix (normal) form: ");
String expression = converter.convertInfixIntoPostfix(s.nextLine());
System.out.println("Postfix form of your expression: " + expression);
Calculator calculator = new Calculator();
Stack<String> stack = new Stack<>();
String[] separatedExpression = expression.split(" ");
for (String elem : separatedExpression) {
if (isSign(elem)) {
Signs signType = Signs.parseSign(elem.charAt(0));
makeOperation(signType, stack, calculator);
continue;
}
pushOperandToStack(elem, stack);
}
System.out.println("Result of your expression: " + stack.getFirstFromTop());
}
private static boolean isSign(String elem) {
return (elem.length() == 1 && Signs.parseSign(elem.charAt(0)) != null);
}
private static void makeOperation(Signs signType, Stack<String> stack, Calculator calculator) {
switch (signType) {
case PLUS -> {
String result = calculator.addition(stack.getFirstFromTop(), stack.getSecondFromTop());
stack.pop();
stack.pop();
stack.push(result);
}
case MINUS -> {
String result = calculator.subtraction(stack.getSecondFromTop(), stack.getFirstFromTop());
stack.pop();
stack.pop();
stack.push(result);
}
case MUL -> {
String result = calculator.multiplication(stack.getFirstFromTop(), stack.getSecondFromTop());
stack.pop();
stack.pop();
stack.push(result);
}
case DIV -> {
String result = calculator.division(stack.getSecondFromTop(), stack.getFirstFromTop());
stack.pop();
stack.pop();
stack.push(result);
}
default -> {
}
}
}
private static void pushOperandToStack(String operand, Stack<String> stack) {
stack.push(operand);
}
}
interface Calculations {
String addition(String operand1, String operand2);
String subtraction(String operand1, String operand2);
String multiplication(String operand1, String operand2);
String division(String operand1, String operand2);
}
class Calculator implements Calculations {
/**
* Method convert operands from string type of certain and make calculations.
* After this method return a string representation of the result.
* Result of this method - addition of two operands.
*
* @param operand1 string representation of operand 1.
* @param operand2 string representation of operand 2.
* @return string representation of the result.
*/
@Override
public String addition(String operand1, String operand2) {
try {
return String.valueOf(Double.parseDouble(operand1) + Double.parseDouble(operand2));
} catch (Exception e) {
System.out.println(e.getMessage());
System.exit(0);
}
return "";
}
/**
* Method convert operands from string type of certain and make calculations.
* After this method return a string representation of the result.
* Result of this method - subtraction of two operands.
*
* @param operand1 string representation of operand 1.
* @param operand2 string representation of operand 2.
* @return string representation of the result.
*/
@Override
public String subtraction(String operand1, String operand2) {
try {
return String.valueOf(Double.parseDouble(operand1) - Double.parseDouble(operand2));
} catch (Exception e) {
System.out.println(e.getMessage());
System.exit(0);
}
return "";
}
/**
* Method convert operands from string type of certain and make calculations.
* After this method return a string representation of the result.
* Result of this method - multiplication of two operands.
*
* @param operand1 string representation of operand 1.
* @param operand2 string representation of operand 2.
* @return string representation of the result.
*/
@Override
public String multiplication(String operand1, String operand2) {
try {
return String.valueOf(Double.parseDouble(operand1) * Double.parseDouble(operand2));
} catch (Exception e) {
System.out.println(e.getMessage());
System.exit(0);
}
return "";
}
/**
* Method convert operands from string type of certain and make calculations.
* After this method return a string representation of the result.
* Result of this method - division of two operands.
*
* @param operand1 string representation of operand 1.
* @param operand2 string representation of operand 2.
* @return string representation of the result.
*/
@Override
public String division(String operand1, String operand2) {
try {
return String.valueOf(Double.parseDouble(operand1) / Double.parseDouble(operand2));
} catch (Exception e) {
System.out.println(e.getMessage());
System.exit(0);
}
return "";
}
}
/**
* Implementation of simple stack model according to canonical stack definition.
*
* @param <T> Data type which would be stored in the stack.
*/
class Stack<T> {
/**
* Variable which are implements stack itself.
* Provide a functionality according to canonical stack model.
*/
private final ArrayList<T> stack = new ArrayList<>();
/**
* Add object on the top of the stack.
*
* @param object added on the top of the stack.
*/
public void push(T object) {
stack.add(object);
}
/**
* Remove object from the top of the stack.
*/
public void pop() {
stack.remove(this.depth() - 1);
}
/**
* Return string representation of the stack array and clear stack overall.
*
* @return String representation of the stack.
*/
public String uploadStackInString() {
StringBuilder string = new StringBuilder();
if (stack.size() == 0) return "";
int i = 0;
string.append(stack.get(i));
for (i = 1; i < this.depth(); i++) {
string.append(" ").append(stack.get(i));
}
stack.clear();
return string.toString();
}
/**
* Return string representation of the expression in the LAST GAINED BRACKETS and remove it from the stack.
*
* @return String representation of the expression in the LAST GAINED BRACKETS
*/
public String uploadExpressionInParentsInString() {
StringBuilder string = new StringBuilder();
int startIndex = stack.lastIndexOf("(");
int finishIndex = stack.indexOf(")");
for (int i = startIndex + 1; i < finishIndex; i++) {
if (i == startIndex + 1) {
string.append(stack.get(i));
continue;
}
string.append(" ").append(stack.get(i));
}
removeSubsequence(startIndex, finishIndex);
return string.toString();
}
private void removeSubsequence(int start, int finish) {
int length = finish - start + 1;
if (length == stack.size()) {
uploadStackInString();
length = 0;
}
while (length > 0) {
stack.remove(start);
length--;
}
}
/**
* Method return a size of the stack.
*
* @return int value of stack size.
*/
public int depth() {
return stack.size();
}
/**
* Method return first element of the stack.
*
* @return Object of T type from the top of the stack.
*/
public T getFirstFromTop() {
return stack.get(this.depth() - 1);
}
/**
* Method return second element of the stack.
*
* @return Object of T type from the second place counting from the top of the stack.
*/
public T getSecondFromTop() {
return stack.get(this.depth() - 2);
}
}
interface Conversions {
/**
* Function which implements simple translation from Infix --> Postfix form of expression notation.
*
* @param expression Expression given in string format.
* @return Given expression, converted into postfix form, also in string format.
*/
String convertInfixIntoPostfix(String expression);
}
/**
* Class provides a several conversions from one type to another.
* Can be updated.
*/
class Converter implements Conversions {
/**
* Variable accessed by whole class and behave itself a counter of amount
* left brackets from already read line.
*/
private static int leftParents = 0;
/**
* Variable accessed by whole class and behave itself a counter of amount
* right brackets from already read line.
*/
private static int rightParents = 0;
@Override
public String convertInfixIntoPostfix(String expression) {
StringBuilder postfixForm = new StringBuilder();
String[] separatedExpression = expression.split(" ");
Stack<String> operationsStack = new Stack<>();
try {
if (!checkForExpressionsCorrectness(separatedExpression)) {
throw new InvalidExpression();
}
for (int i = 0; i < separatedExpression.length; i++) {
String currentStringElem = separatedExpression[i];
if (i == 0 && !(isOperand(currentStringElem))) {
throw new InvalidExpression();
}
if (isOperand(currentStringElem) && (i % 2 == 0)) {
currentStringElem = deleteAndCheckForBrackets(currentStringElem, operationsStack);
if (postfixForm.length() == 0) {
postfixForm.append(currentStringElem);
continue;
}
postfixForm.append(" ").append(currentStringElem);
if (leftParents > 0 && rightParents > 0) {
while (Math.min(leftParents, rightParents) > 0) {
postfixForm
.append(" ")
.append(reversed(operationsStack.uploadExpressionInParentsInString()));
leftParents--;
rightParents--;
}
}
continue;
}
if (isSign(currentStringElem.charAt(0)) && (i % 2 != 0)) {
if (operationsStack.depth() == 0) {
operationsStack.push(currentStringElem);
continue;
}
if (
Signs.signsComparison(operationsStack.getFirstFromTop().charAt(0), currentStringElem.charAt(0))
) {
postfixForm.append(" ").append(reversed(operationsStack.uploadStackInString()));
operationsStack.push(currentStringElem);
} else {
operationsStack.push(currentStringElem);
}
continue;
}
throw new InvalidExpression();
}
} catch (Exception e) {
System.out.println(e.getMessage());
System.exit(0);
}
postfixForm.append(" ").append(reversed(operationsStack.uploadStackInString()));
return postfixForm.toString();
}
private boolean isOperand(String elem) {
if (elem.length() == 1) {
return Character.isDigit(elem.charAt(0));
}
return true;
}
private boolean isSign(Character elem) {
return (Signs.parseSign(elem) != null);
}
private String reversed(String string) {
return new StringBuilder(string).reverse().toString();
}
private boolean checkForExpressionsCorrectness(String[] separatedExpression) {
return separatedExpression.length % 2 != 0 && separatedExpression.length > 2;
}
private String deleteAndCheckForBrackets(String string, Stack<String> stack) {
String correctString = string;
if (string.charAt(0) == '(' && string.charAt(string.length() - 1) == ')') {
correctString = string.substring(string.lastIndexOf("(") + 1, string.indexOf(")"));
} else if (string.charAt(string.length() - 1) == ')') {
correctString = string.substring(0, string.indexOf(")"));
for (int i = 0; i < string.length() - string.indexOf(")"); i++) {
rightParents++;
stack.push(")");
}
} else if (string.charAt(0) == '(') {
correctString = string.substring(string.lastIndexOf("(") + 1);
for (int i = 0; i < string.lastIndexOf("(") + 1; i++) {
leftParents++;
stack.push("(");
}
}
return correctString;
}
}
enum Signs {
PLUS,
MINUS,
MUL,
DIV,
LEFT_BRACKET,
RIGHT_BRACKET;
private static final int ASCII_MUL = 42;
private static final int ASCII_PLUS = 43;
private static final int ASCII_MINUS = 45;
private static final int ASCII_DIV = 47;
private static final int ASCII_LEFT_BRACKET = 40;
private static final int ASCII_RIGHT_BRACKET = 41;
public static Signs parseSign(Character elem) {
return switch (elem) {
case (ASCII_MUL) -> Signs.MUL;
case (ASCII_PLUS) -> Signs.PLUS;
case (ASCII_MINUS) -> Signs.MINUS;
case (ASCII_DIV) -> Signs.DIV;
case (ASCII_LEFT_BRACKET) -> Signs.LEFT_BRACKET;
case (ASCII_RIGHT_BRACKET) -> Signs.RIGHT_BRACKET;
default -> null;
};
}
// If elem_1 < elem_2 == 0
// Else == 1
public static boolean signsComparison(Character elem1, Character elem2) {
Signs sign1 = parseSign(elem1);
Signs sign2 = parseSign(elem2);
if ((sign1 == PLUS || sign1 == MINUS) && (sign2 == MUL || sign2 == DIV)) {
return false;
}
if ((sign1 == LEFT_BRACKET || sign1 == RIGHT_BRACKET) || (sign2 == LEFT_BRACKET || sign2 == RIGHT_BRACKET)) {
return false;
}
return true;
}
}
class InvalidExpression extends Exception {
@Override
public String getMessage() {
return "Invalid expression\n";
}
}