-
Notifications
You must be signed in to change notification settings - Fork 151
[4기 - 한희나] 1~2주차 과제 : 계산기 미션 제출합니다. #158
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: heenahan
Are you sure you want to change the base?
Changes from 13 commits
823468f
9f99b95
c78db4c
4e840f1
27c0031
bb2ba06
104c54b
91ae6e6
dd89153
73a262d
e4a7702
88acf06
46aed93
ef913ec
679a107
b00a4fb
9a70dfa
0abe54e
fa9691b
91ee3a9
a8161ac
6b41677
f0aff99
5e74365
752d38d
d2e0f24
26995f4
8984cd5
6ce753c
3a2acbf
8ec3a90
d59e1c0
b813f7c
f781b5f
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,218 @@ | ||
| # Ignore Gradle project-specific cache directory | ||
| .gradle | ||
|
|
||
| # Ignore Gradle build output directory | ||
| build | ||
| ### Java template | ||
| # Compiled class file | ||
| *.class | ||
|
|
||
| # Log file | ||
| *.log | ||
|
|
||
| # BlueJ files | ||
| *.ctxt | ||
|
|
||
| # Mobile Tools for Java (J2ME) | ||
| .mtj.tmp/ | ||
|
|
||
| # Package Files # | ||
| *.jar | ||
| *.war | ||
| *.nar | ||
| *.ear | ||
| *.zip | ||
| *.tar.gz | ||
| *.rar | ||
|
|
||
| # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml | ||
| hs_err_pid* | ||
|
|
||
| ### JetBrains template | ||
| # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider | ||
| # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 | ||
|
|
||
| # User-specific stuff | ||
| .idea/**/workspace.xml | ||
| .idea/**/tasks.xml | ||
| .idea/**/usage.statistics.xml | ||
| .idea/**/dictionaries | ||
| .idea/**/shelf | ||
|
|
||
| # Generated files | ||
| .idea/**/contentModel.xml | ||
|
|
||
| # Sensitive or high-churn files | ||
| .idea/**/dataSources/ | ||
| .idea/**/dataSources.ids | ||
| .idea/**/dataSources.local.xml | ||
| .idea/**/sqlDataSources.xml | ||
| .idea/**/dynamic.xml | ||
| .idea/**/uiDesigner.xml | ||
| .idea/**/dbnavigator.xml | ||
|
|
||
| # Gradle | ||
| .idea/**/gradle.xml | ||
| .idea/**/libraries | ||
|
|
||
| # Gradle and Maven with auto-import | ||
| # When using Gradle or Maven with auto-import, you should exclude module files, | ||
| # since they will be recreated, and may cause churn. Uncomment if using | ||
| # auto-import. | ||
| # .idea/artifacts | ||
| # .idea/compiler.xml | ||
| # .idea/jarRepositories.xml | ||
| # .idea/modules.xml | ||
| # .idea/*.iml | ||
| # .idea/modules | ||
| # *.iml | ||
| # *.ipr | ||
|
|
||
| # CMake | ||
| cmake-build-*/ | ||
|
|
||
| # Mongo Explorer plugin | ||
| .idea/**/mongoSettings.xml | ||
|
|
||
| # File-based project format | ||
| *.iws | ||
|
|
||
| # IntelliJ | ||
| out/ | ||
|
|
||
| # mpeltonen/sbt-idea plugin | ||
| .idea_modules/ | ||
|
|
||
| # JIRA plugin | ||
| atlassian-ide-plugin.xml | ||
|
|
||
| # Cursive Clojure plugin | ||
| .idea/replstate.xml | ||
|
|
||
| # Crashlytics plugin (for Android Studio and IntelliJ) | ||
| com_crashlytics_export_strings.xml | ||
| crashlytics.properties | ||
| crashlytics-build.properties | ||
| fabric.properties | ||
|
|
||
| # Editor-based Rest Client | ||
| .idea/httpRequests | ||
|
|
||
| # Android studio 3.1+ serialized cache file | ||
| .idea/caches/build_file_checksums.ser | ||
|
|
||
| ### Java template | ||
| # Compiled class file | ||
| *.class | ||
|
|
||
| # Log file | ||
| *.log | ||
|
|
||
| # BlueJ files | ||
| *.ctxt | ||
|
|
||
| # Mobile Tools for Java (J2ME) | ||
| .mtj.tmp/ | ||
|
|
||
| # Package Files # | ||
| *.jar | ||
| *.war | ||
| *.nar | ||
| *.ear | ||
| *.zip | ||
| *.tar.gz | ||
| *.rar | ||
|
|
||
| # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml | ||
| hs_err_pid* | ||
|
|
||
| ### JetBrains template | ||
| # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider | ||
| # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 | ||
|
|
||
| # User-specific stuff | ||
| .idea/**/workspace.xml | ||
| .idea/**/tasks.xml | ||
| .idea/**/usage.statistics.xml | ||
| .idea/**/dictionaries | ||
| .idea/**/shelf | ||
|
|
||
| # Generated files | ||
| .idea/**/contentModel.xml | ||
|
|
||
| # Sensitive or high-churn files | ||
| .idea/**/dataSources/ | ||
| .idea/**/dataSources.ids | ||
| .idea/**/dataSources.local.xml | ||
| .idea/**/sqlDataSources.xml | ||
| .idea/**/dynamic.xml | ||
| .idea/**/uiDesigner.xml | ||
| .idea/**/dbnavigator.xml | ||
|
|
||
| # Gradle | ||
| .idea/**/gradle.xml | ||
| .idea/**/libraries | ||
|
|
||
| # Gradle and Maven with auto-import | ||
| # When using Gradle or Maven with auto-import, you should exclude module files, | ||
| # since they will be recreated, and may cause churn. Uncomment if using | ||
| # auto-import. | ||
| # .idea/artifacts | ||
| # .idea/compiler.xml | ||
| # .idea/jarRepositories.xml | ||
| # .idea/modules.xml | ||
| # .idea/*.iml | ||
| # .idea/modules | ||
| # *.iml | ||
| # *.ipr | ||
|
|
||
| # CMake | ||
| cmake-build-*/ | ||
|
|
||
| # Mongo Explorer plugin | ||
| .idea/**/mongoSettings.xml | ||
|
|
||
| # File-based project format | ||
| *.iws | ||
|
|
||
| # IntelliJ | ||
| out/ | ||
|
|
||
| # mpeltonen/sbt-idea plugin | ||
| .idea_modules/ | ||
|
|
||
| # JIRA plugin | ||
| atlassian-ide-plugin.xml | ||
|
|
||
| # Cursive Clojure plugin | ||
| .idea/replstate.xml | ||
|
|
||
| # Crashlytics plugin (for Android Studio and IntelliJ) | ||
| com_crashlytics_export_strings.xml | ||
| crashlytics.properties | ||
| crashlytics-build.properties | ||
| fabric.properties | ||
|
|
||
| # Editor-based Rest Client | ||
| .idea/httpRequests | ||
|
|
||
| # Android studio 3.1+ serialized cache file | ||
| .idea/caches/build_file_checksums.ser | ||
|
|
||
| ### Gradle template | ||
| .gradle | ||
| **/build/ | ||
| !src/**/build/ | ||
|
|
||
| # Ignore Gradle GUI config | ||
| gradle-app.setting | ||
|
|
||
| # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) | ||
| !gradle-wrapper.jar | ||
|
|
||
| # Cache of project | ||
| .gradletasknamecache | ||
|
|
||
| # # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 | ||
| # gradle/wrapper/gradle-wrapper.properties | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| /* | ||
| * This Java source file was generated by the Gradle 'init' task. | ||
| */ | ||
| package com.programmers; | ||
|
|
||
| import com.programmers.controller.CalculatorController; | ||
|
|
||
| public class App { | ||
|
|
||
| public static void main(String[] args) { | ||
| CalculatorController calculatorController = new CalculatorController(); | ||
| calculatorController.run(); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package com.programmers.calculator; | ||
|
|
||
| import java.util.*; | ||
|
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.
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. 아 ide에서 자동으로 와일드카드로 변경되지 못하도록 바꾸겠습니다!! |
||
|
|
||
| public class Calculator { | ||
|
|
||
| public List<String> changeInfixToPostfix(List<String> infix) { | ||
|
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. 여기서 List infix를 받는게 아니라 Expression을 받아서 처리하는 방향에 대해서 어떻게 생각하시나요 ?
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. 수식을 클래스로 감쌌으니 해당 클래스를 파라미터로 넘겨주는게 좋다는 말씀이시죠?? 그렇게 한다면 메시지가 분명해져 좋을 것 같아요..!! |
||
| List<String> postfix = new ArrayList<>(); | ||
| Stack<String> stack = new Stack<>(); | ||
|
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. |
||
|
|
||
| for (String s : infix) { | ||
| if (s.matches("\\d+")) { // 숫자일 경우 | ||
| postfix.add(s); | ||
| } else { // 숫자 아닐 경우 | ||
| int priority = getPriority(s); | ||
| // 스택이 비었거나 | ||
| while (!stack.isEmpty()) { | ||
| String topOperator = stack.peek(); | ||
| int topPriority = getPriority(topOperator); | ||
|
|
||
| // top의 우선 순위가 높거나 같을 경우 | ||
| if (topPriority >= priority) postfix.add(stack.pop()); | ||
| // top의 우선 순위가 낮을 경우 | ||
| else break; | ||
| } | ||
| stack.push(s); // 연산자 넣음 | ||
| } | ||
| } | ||
|
|
||
| // 스택에 남아있는 연산자 모두 집어 넣음 | ||
| while (!stack.isEmpty()) postfix.add(stack.pop()); | ||
|
|
||
| return postfix; | ||
| } | ||
|
|
||
| public Integer calcPostfix(List<String> postfix) { | ||
|
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. calcPostfix 보다 풀네이밍을 사용하는건 어떨까요? |
||
| Stack<Integer> stack = new Stack<>(); | ||
|
|
||
| for (String s : postfix) { | ||
| // 숫자일 경우 스택에 넣음 | ||
| if (s.matches("\\d+")) stack.push(Integer.parseInt(s)); | ||
|
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. 숫자인지 판단하는 로직을 메서드로 추출하면 좀 더 읽기 편해질 것 같아요. 또한, 판단하는 정규식을 상수로 만들어줘도 좋구요. |
||
| else { // 연산자 일 경우 | ||
| Integer op1 = stack.pop(); | ||
| Integer op2 = stack.pop(); | ||
|
|
||
| Operator calc = Operator.of(s); | ||
| Integer result = calc.getFunc().apply(op2, op1); | ||
|
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. get을 사용해서 다시 꺼낼 필요없이 |
||
|
|
||
| stack.push(result); | ||
| } | ||
| } | ||
|
|
||
| return stack.pop(); | ||
| } | ||
|
|
||
| public Integer getPriority(String operator) { | ||
| // * 와 / 는 우선순위 높음 | ||
| if (operator.equals("*") || operator.equals("/")) return 1; | ||
| // 그 외 낮음 | ||
| return -1; | ||
| } | ||
|
|
||
|
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. Operator라는 객체를 통해 상태와 행위를 모아주신 점 아주 좋습니다. 💯 |
||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,42 @@ | ||||||||||||||||||
| package com.programmers.calculator; | ||||||||||||||||||
|
|
||||||||||||||||||
| import java.util.Arrays; | ||||||||||||||||||
| import java.util.function.BiFunction; | ||||||||||||||||||
|
|
||||||||||||||||||
| public enum Operator { | ||||||||||||||||||
|
|
||||||||||||||||||
| PLUS("+", (a, b) -> { return a + b; }), | ||||||||||||||||||
| MINUS("-", (a, b) -> { return a - b; }), | ||||||||||||||||||
| DIVIDE("/", (a, b) -> { | ||||||||||||||||||
| try { | ||||||||||||||||||
| return a / b; | ||||||||||||||||||
| } catch (ArithmeticException e) { | ||||||||||||||||||
| throw e; | ||||||||||||||||||
| } | ||||||||||||||||||
| }), | ||||||||||||||||||
| MUTIPLY("*", (a, b) -> { return a * b; }); | ||||||||||||||||||
|
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. 현재 예외가 발생하면 잡아서 다시 throw를 해주고 있는데요.
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. 멘토님 말씀을 보고 의미가 없는 코드라고 생각해 ArithmeticException이 발생하면 custom exception인 DivisionByZeroException에 메시지를 담아 던지도록 바꾸었습니다..!! |
||||||||||||||||||
|
|
||||||||||||||||||
| private String operator; | ||||||||||||||||||
| private BiFunction<Integer, Integer, Integer> func; | ||||||||||||||||||
|
|
||||||||||||||||||
| Operator(String operator, BiFunction<Integer, Integer, Integer> func) { | ||||||||||||||||||
| this.operator = operator; | ||||||||||||||||||
| this.func = func; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| public String getOperator() { | ||||||||||||||||||
| return operator; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| public BiFunction<Integer, Integer, Integer> getFunc() { | ||||||||||||||||||
| return func; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| public static Operator of(String operator) { | ||||||||||||||||||
| return Arrays.stream(Operator.values()) | ||||||||||||||||||
| .filter(o -> o.getOperator().equals(operator)) | ||||||||||||||||||
| .findFirst() | ||||||||||||||||||
| .orElseThrow(NullPointerException::new); | ||||||||||||||||||
|
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.
Suggested change
로 사용해도 괜찮지 않을까요 ? getOperator()가 없어도 operator에 접근할 수 있으니까요.
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. 그러네요 바로 operator에 접근할 수 있네요!! 그리고 NotFoundOperatorException이라는 custom exception을 만들어 직관적으로 예외 상황을 나타냈습니다 |
||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| } | ||||||||||||||||||
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.
아주 사소한 의견입니다만,
CalculatorApplication 과 같은 구체 네이밍은 어떨까요?