- 
                Notifications
    You must be signed in to change notification settings 
- Fork 155
[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.*; | ||
|          | ||
|  | ||
| public class Calculator { | ||
|  | ||
| public List<String> changeInfixToPostfix(List<String> infix) { | ||
|          | ||
| List<String> postfix = new ArrayList<>(); | ||
| Stack<String> stack = new Stack<>(); | ||
|          | ||
|  | ||
| 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) { | ||
|          | ||
| Stack<Integer> stack = new Stack<>(); | ||
|  | ||
| for (String s : postfix) { | ||
| // 숫자일 경우 스택에 넣음 | ||
| if (s.matches("\\d+")) stack.push(Integer.parseInt(s)); | ||
|          | ||
| else { // 연산자 일 경우 | ||
| Integer op1 = stack.pop(); | ||
| Integer op2 = stack.pop(); | ||
|  | ||
| Operator calc = Operator.of(s); | ||
| Integer result = calc.getFunc().apply(op2, op1); | ||
|          | ||
|  | ||
| stack.push(result); | ||
| } | ||
| } | ||
|  | ||
| return stack.pop(); | ||
| } | ||
|  | ||
| public Integer getPriority(String operator) { | ||
| // * 와 / 는 우선순위 높음 | ||
| if (operator.equals("*") || operator.equals("/")) return 1; | ||
| // 그 외 낮음 | ||
| return -1; | ||
| } | ||
|  | ||
|          | ||
| } | ||
| 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; }); | ||||||||||||||||||
|          | ||||||||||||||||||
|  | ||||||||||||||||||
| 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); | ||||||||||||||||||
|          | ||||||||||||||||||
| return Arrays.stream(Operator.values()) | |
| .filter(o -> o.getOperator().equals(operator)) | |
| .findFirst() | |
| .orElseThrow(NullPointerException::new); | |
| return Arrays.stream(Operator.values()) | |
| .filter(o -> o.operator.equals(operator)) | |
| .findFirst() | |
| .orElseThrow(NullPointerException::new); | 
로 사용해도 괜찮지 않을까요 ? getOperator()가 없어도 operator에 접근할 수 있으니까요.
추가적으로 operator를 찾지못했을 때 NullPointerException 보단 다른 exception이 좋지않을까 싶습니다. (@가 들어올 경우에 NullPointerException이 적절한가? 의 관점에서)
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.
그러네요 바로 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 과 같은 구체 네이밍은 어떨까요?