-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCalculator.java
More file actions
74 lines (57 loc) · 2.15 KB
/
Calculator.java
File metadata and controls
74 lines (57 loc) · 2.15 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
package calculator;
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("계산할 수식을 입력하세요: ");
String input = sc.nextLine();
System.out.println("입력된 수식: " + input);
Calculator c = new Calculator();
int result = c.calculate(input);
}
public int calculate(String input) {
int num = 0;
int number = 0;
char operation = ' ';
StringBuilder sb = new StringBuilder();
input = input.trim().replaceAll("\\s+", "");
boolean isValid = input.matches("[0-9]+([+\\-*/][0-9]+)*");
if (!isValid) {
throw new IllegalArgumentException("잘못된 수식 형식입니다.");
}
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
if (Character.isDigit(ch)) {
sb.append(ch);
} else if (ch == '+' || ch == '-' || ch == '*' || ch == '/') {
if (sb.length() == 0) {
System.out.println("숫자 없이 연산자가 들어왔습니다!");
return 0;
}
number = Integer.parseInt(sb.toString());
if (operation == ' ') {
num = number;
} else if (operation == '+') {
num += number;
} else if (operation == '-') {
num -= number;
} else if (operation == '*') {
num *= number;
} else if (operation == '/') {
num /= number;
}
sb.setLength(0);
operation = ch;
}
}
if (sb.length() > 0) {
number = Integer.parseInt(sb.toString());
if (operation == '+') num += number;
else if (operation == '-') num -= number;
else if (operation == '*') num *= number;
else if (operation == '/') num /= number;
}
System.out.println("계산 결과: " + num);
return num;
}
}