-
Notifications
You must be signed in to change notification settings - Fork 904
Expand file tree
/
Copy pathStringCalculator.java
More file actions
55 lines (51 loc) · 1.67 KB
/
StringCalculator.java
File metadata and controls
55 lines (51 loc) · 1.67 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
package study;
import java.util.Scanner;
public class StringCalculator {
// 사칙연산
public int add(int a, int b){ return a + b;}
public int subtract(int a, int b){ return a - b;}
public int multiply(int a, int b){ return a * b;}
public int divide(int a, int b){
try {
return a / b;
} catch (Exception e) {
System.out.println("0으로 나눌 수 없습니다.");
}return 0;
}
// 분할 메소드
public String[] split(String str) {
return str.split(" ");
}
public int toInt(String input) {
return Integer.parseInt(input);
}
public int transform(String[] values) {
int result = toInt(values[0]);
for (int i = 1; i < values.length; i += 2) {
result = operator(result, values[i],toInt(values[i+1]));
}
return result;
}
// 더하기, 곱하기, 나누기, 빼기 함수
public int operator(int firstValue, String operator, int secondValue) {
switch (operator) {
case "+":
return add(firstValue, secondValue);
case "-":
return subtract(firstValue, secondValue);
case "*":
return multiply(firstValue, secondValue);
case "/":
return divide(firstValue, secondValue);
default:
System.out.println("사칙 연산자가 아닙니다.");
}
return 0;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String value = sc.nextLine();
StringCalculator T = new StringCalculator();
T.transform(T.split(value));
}
}