Skip to content

Commit fbef879

Browse files
committed
feat: 후위 표기법 & 정규화 구현
1 parent 540f883 commit fbef879

File tree

3 files changed

+93
-13
lines changed

3 files changed

+93
-13
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package calculator.engine.model;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Stack;
6+
import java.util.regex.Pattern;
7+
8+
public class InitCalculator {
9+
public static List<String> calculate(String infixCalculator) {
10+
Stack<String> stack = new Stack<>();
11+
List<String> postfixExpression = new ArrayList<>();
12+
13+
for (String ch : infixCalculator.split(" ")) {
14+
if(Pattern.matches("[0-9]+",ch)){
15+
postfixExpression.add(ch);
16+
} else if (ch.equals("(")) {
17+
stack.push(ch);
18+
while (!stack.isEmpty() && !stack.peek().equals("(")) {
19+
postfixExpression.add(stack.pop());
20+
}
21+
stack.pop();
22+
} else {
23+
while (!stack.isEmpty() && getPrecedence(ch) <= getPrecedence(stack.peek())) {
24+
postfixExpression.add(stack.pop());
25+
}
26+
stack.push(ch);
27+
}
28+
}
29+
while (!stack.isEmpty()) {
30+
postfixExpression.add(stack.pop());
31+
}
32+
return postfixExpression;
33+
}
34+
private static int getPrecedence(String operator) {
35+
switch (operator) {
36+
case "+":
37+
case "-":
38+
return 1;
39+
case "*":
40+
case "/":
41+
return 2;
42+
default:
43+
return 0;
44+
}
45+
}
46+
}

src/main/java/calculator/engine/model/Operator.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,30 @@
22

33
import calculator.engine.enums.Operators;
44

5-
public class Operator {
5+
class Operator {
66
public int operate(int num1, int num2, Operators operators){
77
switch (operators){
88
case PLUS:
9-
return PlusCalcul(num1,num2);
9+
return add(num1,num2);
1010
case MINUS:
11-
return MinusCalcul(num1,num2);
11+
return substract(num1,num2);
1212
case MULTIPLICATION:
13-
return MultiCalcul(num1,num2);
13+
return multiply(num1,num2);
1414
case DIVISION:
15-
return DiviCalcul(num1,num2);
15+
return divide(num1,num2);
1616
}
1717
return 0;
1818
}
19-
20-
private int PlusCalcul(int num1,int num2) {
19+
private int add(int num1,int num2) {
2120
return num1 + num2;
2221
}
23-
24-
private int MinusCalcul(int num1,int num2) {
22+
private int substract(int num1,int num2) {
2523
return num1 - num2;
2624
}
27-
28-
private int DiviCalcul(int num1, int num2){
25+
private int divide(int num1, int num2){
2926
return num1 / num2;
3027
}
31-
private int MultiCalcul(int num1,int num2){
28+
private int multiply(int num1,int num2){
3229
return num1 * num2;
3330
}
34-
}
31+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,41 @@
11
package calculator.engine.model;
22

3+
import java.util.List;
4+
import java.util.Stack;
5+
36
public class PostfixCalculator {
7+
public int calculate(List<String> postfixExpression) {
8+
Stack<Integer> operandStack = new Stack<>();
9+
10+
for (String operand : postfixExpression) {
11+
if (operand.matches("^?\\d*$")) {
12+
operandStack.push(Integer.parseInt(operand));
13+
} else {
14+
int operand2 = operandStack.pop();
15+
int operand1 = operandStack.pop();
16+
int result = 0;
17+
18+
switch (operand) {
19+
case "+":
20+
result = operand1 + operand2;
21+
break;
22+
case "-":
23+
result = operand1 - operand2;
24+
break;
25+
case "*":
26+
result = operand1 * operand2;
27+
break;
28+
case "/":
29+
if (operand2 == 0) {
30+
throw new ArithmeticException("Divide by zero!");
31+
}
32+
result = operand1 / operand2;
33+
break;
34+
}
35+
36+
operandStack.push(result);
37+
}
38+
}
39+
return operandStack.pop();
40+
}
441
}

0 commit comments

Comments
 (0)