-
Notifications
You must be signed in to change notification settings - Fork 0
문자열 계산기 #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
문자열 계산기 #1
Changes from 4 commits
b6cc2be
5744c54
b0ea518
80b5903
43666c5
f85c2b1
0ac24c2
5a4e20f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package stringCalculator; | ||
|
|
||
| public class FormulaSplitter { | ||
|
|
||
| public String[] split(String formula) { | ||
| return formula.split(" "); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package stringCalculator; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class InputProcessor { | ||
| private final Scanner scanner = new Scanner(System.in); | ||
|
|
||
| public String input() { | ||
| System.out.print("계산식을 입력하세요(예:2 + 3 * 4 / 5): "); | ||
| return scanner.nextLine(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package stringCalculator; | ||
|
|
||
| public enum Operator { | ||
| PLUS("+") { | ||
| @Override | ||
| int operation(int a, int b) { | ||
| return a + b; | ||
| } | ||
| }, | ||
| MINUS("-"){ | ||
| @Override | ||
| int operation(int a, int b) { | ||
| return a - b; | ||
| } | ||
| }, | ||
| MULTIPLY("*"){ | ||
| @Override | ||
| int operation(int a, int b) { | ||
| return a * b; | ||
| } | ||
| }, | ||
| DIVIDE("/"){ | ||
| @Override | ||
| int operation(int a, int b) { | ||
| if (b == 0) { | ||
| throw new IllegalArgumentException("0으로 나눌 수 없습니다."); | ||
| } | ||
| return a / b; | ||
| } | ||
|
kdongd marked this conversation as resolved.
|
||
| }; | ||
|
|
||
| private final String message; | ||
|
|
||
| Operator(String message) { | ||
| this.message = message; | ||
| } | ||
|
kdongd marked this conversation as resolved.
|
||
|
|
||
| abstract int operation(int a, int b); | ||
|
|
||
| public static Operator conversion(String message) { //입력받은 연산자로 enum 찾기 | ||
| for (Operator op : Operator.values()) { | ||
| if (op.message.equals(message)) { | ||
| return op; | ||
| } | ||
| } | ||
| throw new IllegalArgumentException("지원하지 않는 연산자: " + message); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package stringCalculator; | ||
|
|
||
| public class OutputProcessor { | ||
|
|
||
| public void output(int result) { | ||
| System.out.println("결과: " + result); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package stringCalculator; | ||
|
|
||
|
|
||
|
|
||
| public class StringCalculator { | ||
|
|
||
| public int calculate(String[] values) { | ||
| try { | ||
|
|
||
| int result; | ||
| int number = Integer.parseInt(values[0]); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 변수를 2개로 나눈 이유가 있을까요?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. number는 for문에서 중간 계산값을 저장, result는 number를 좀 더 쉽게 보여주기 위해 사용했습니다. |
||
|
|
||
| for (int i = 1; i < values.length; i += 2) { | ||
| Operator operator = Operator.conversion(values[i]); | ||
| int nextNumber = Integer.parseInt(values[i + 1]); | ||
|
|
||
| number = operator.operation(number, nextNumber); | ||
| } | ||
| result = number; | ||
| return result; | ||
| } catch (NumberFormatException e) { | ||
| throw new IllegalArgumentException("숫자가 아닌 값이 포함되어 있습니다." , e); | ||
| } catch (ArrayIndexOutOfBoundsException e) { | ||
| throw new IllegalArgumentException("잘못된 계산식입니다. 연산자와 숫자를 올바르게 입력하시오." , e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package stringCalculator; | ||
|
|
||
| public class StringCalculatorMain { | ||
| public static void main(String[] args) { | ||
| StringCalculator stringCalculator = new StringCalculator(); | ||
| InputProcessor inputProcessor = new InputProcessor(); | ||
| OutputProcessor outputProcessor = new OutputProcessor(); | ||
| FormulaSplitter formulaSplitter = new FormulaSplitter(); | ||
|
|
||
| String value = inputProcessor.input(); | ||
| String[] values = formulaSplitter.split(value); | ||
|
|
||
| int result = stringCalculator.calculate(values); | ||
| outputProcessor.output(result); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
formula가 null이나 빈값이 들어오면 어떻게 되나요?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
null값이 들어오면 NullPointerException이 발생
빈값이 들어오면 NumberFormatException이 발생합니다.
코드 수정 하겠습니다!