-
Notifications
You must be signed in to change notification settings - Fork 156
[4기 - 박현지] 1~2주차 과제: 계산기 구현 미션 제출합니다. #185
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: hyeon-z
Are you sure you want to change the base?
Changes from all commits
8fbbc9c
5285c7f
9dfea2a
a07a25e
1e84600
4356910
e27148f
6f6052c
ca62996
91a5c98
99af777
983947b
b06aaab
7e7d953
3ec8441
b35fc5d
672c2fe
91755d8
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,9 @@ | ||
# | ||
# https://help.github.com/articles/dealing-with-line-endings/ | ||
# | ||
# Linux start script should use lf | ||
/gradlew text eol=lf | ||
|
||
# These are Windows script files and should use crlf | ||
*.bat text eol=crlf | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
# Created by https://www.toptal.com/developers/gitignore/api/java,gradle,intellij+all | ||
# Edit at https://www.toptal.com/developers/gitignore?templates=java,gradle,intellij+all | ||
|
||
### Intellij+all ### | ||
# 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 | ||
|
||
# AWS User-specific | ||
.idea/**/aws.xml | ||
|
||
# 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 | ||
|
||
# SonarLint plugin | ||
.idea/sonarlint/ | ||
|
||
# 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 | ||
|
||
### Intellij+all Patch ### | ||
# Ignore everything but code style settings and run configurations | ||
# that are supposed to be shared within teams. | ||
|
||
.idea/* | ||
|
||
!.idea/codeStyles | ||
!.idea/runConfigurations | ||
|
||
### Java ### | ||
# 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* | ||
replay_pid* | ||
|
||
### Gradle ### | ||
.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 | ||
|
||
# Avoid ignore Gradle wrappper properties | ||
!gradle-wrapper.properties | ||
|
||
# Cache of project | ||
.gradletasknamecache | ||
|
||
# Eclipse Gradle plugin generated files | ||
# Eclipse Core | ||
.project | ||
# JDT-specific (Eclipse Java Development Tools) | ||
.classpath | ||
|
||
### Gradle Patch ### | ||
# Java heap dump | ||
*.hprof | ||
|
||
# End of https://www.toptal.com/developers/gitignore/api/java,gradle,intellij+all |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* This file was generated by the Gradle 'init' task. | ||
* | ||
* This generated file contains a sample Java application project to get you started. | ||
* For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle | ||
* User Manual available at https://docs.gradle.org/8.1.1/userguide/building_java_projects.html | ||
*/ | ||
|
||
plugins { | ||
// Apply the application plugin to add support for building a CLI application in Java. | ||
id 'application' | ||
} | ||
|
||
repositories { | ||
// Use Maven Central for resolving dependencies. | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
// Use JUnit Jupiter for testing. | ||
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.1' | ||
testImplementation "org.assertj:assertj-core:3.11.1" | ||
|
||
// This dependency is used by the application. | ||
implementation 'com.google.guava:guava:31.1-jre' | ||
} | ||
|
||
// Apply a specific Java toolchain to ease working on different environments. | ||
java { | ||
toolchain { | ||
languageVersion = JavaLanguageVersion.of(11) | ||
} | ||
} | ||
|
||
application { | ||
// Define the main class for the application. | ||
mainClass = 'java.calculator.Main' | ||
} | ||
|
||
tasks.named('test') { | ||
// Use JUnit Platform for unit tests. | ||
useJUnitPlatform() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* This Java source file was generated by the Gradle 'init' task. | ||
*/ | ||
|
||
import calculator.Calculator; | ||
|
||
import java.io.IOException; | ||
|
||
public class Main { | ||
public static void main(String[] args) throws IOException { | ||
Calculator calculator = new Calculator(); | ||
calculator.run(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package calculator; | ||
|
||
import function.Calculation; | ||
import function.Storage; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
|
||
import static print.Choice.printChoice; | ||
import static validator.Validator.checkChoiceNum; | ||
|
||
public class Calculator { | ||
Calculation calculation; | ||
Storage storage; | ||
|
||
public Calculator() { | ||
this.calculation = new Calculation(); | ||
this.storage = new Storage(); | ||
} | ||
|
||
public void run() throws IOException { | ||
BufferedReader br; | ||
|
||
while (true) { | ||
printChoice(); | ||
|
||
br = new BufferedReader(new InputStreamReader(System.in)); | ||
int choice = checkChoiceNum(Integer.parseInt(br.readLine())); | ||
System.out.println(); | ||
|
||
if (choice == 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. 매직넘버보단 의미있는 상수, 열거체 등을 사용해주세요! |
||
break; | ||
} else if (choice == 1) { | ||
System.out.println(storage.print()); | ||
} else { | ||
br = new BufferedReader(new InputStreamReader(System.in)); | ||
String expression = br.readLine(); | ||
Comment on lines
+37
to
+38
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. 버퍼리더 close하는곳이 없네여! 버퍼는 쓰고 닫는 습관이 있으면 좋다고 생각합니다. 리더를 열고 닫는 곳은 선택에 맡기겠습니다 ㅎㅎ |
||
String result = convertByType(calculation.calculatePostfix(calculation.convertPostfix(expression))); | ||
|
||
storage.store(expression + " = " + result); | ||
System.out.println(result); | ||
System.out.println(); | ||
} | ||
} | ||
} | ||
|
||
private String convertByType(double result) { | ||
if (result == (int) result) { | ||
return String.valueOf((int) result); | ||
} | ||
return String.valueOf(result); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package function; | ||
|
||
import operator.Operator; | ||
|
||
import java.util.Stack; | ||
|
||
import static validator.Validator.checkValidOperator; | ||
|
||
public class Calculation { | ||
Operator operator; | ||
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. 접근제한자가 따로 없는 이유가 있을까요? |
||
private static final String positiveRegularExp = "\\d+(\\.\\d+)?"; | ||
|
||
public Calculation() { | ||
this.operator = new Operator(); | ||
} | ||
|
||
private static int priority(char operator) { | ||
if (operator == '(' || operator == ')') { | ||
return 0; | ||
} else if (operator == '+' || operator == '-') { | ||
return 1; | ||
} else if (operator == '*' || operator == '/') { | ||
return 2; | ||
} | ||
return -1; | ||
} | ||
|
||
public String convertPostfix(String expression) { | ||
Stack<Character> stack = new Stack<>(); | ||
StringBuilder sb = new StringBuilder(); | ||
|
||
String[] split = expression.split(" "); | ||
|
||
for (String now : split) { | ||
Comment on lines
+29
to
+34
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. stack,sb,split,now등 의미를 알기가 힘드네요! |
||
if (isDigit(now)) { | ||
sb.append(now).append(" "); | ||
continue; | ||
} | ||
|
||
char c = checkValidOperator(now.charAt(0)); | ||
|
||
switch (c) { | ||
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. 매직넘버라 바꿔주세요! |
||
case '(': | ||
stack.add(c); | ||
break; | ||
|
||
case ')': | ||
while (!stack.isEmpty() && !(stack.peek() == '(')) { | ||
sb.append(stack.pop()).append(" "); | ||
} | ||
stack.pop(); | ||
break; | ||
default: | ||
while (!stack.isEmpty() && priority(stack.peek()) >= priority(c)) { | ||
sb.append(stack.pop()).append(" "); | ||
} | ||
stack.add(c); | ||
break; | ||
} | ||
} | ||
|
||
while (!stack.isEmpty()) { | ||
sb.append(stack.pop()).append(" "); | ||
} | ||
|
||
return sb.toString(); | ||
} | ||
|
||
public double calculatePostfix(String postfix) { | ||
String[] pfs = postfix.split(" "); | ||
Stack<String> stack = new Stack<>(); | ||
|
||
for (String pf : pfs) { | ||
if (isDigit(pf)) { | ||
stack.add(pf); | ||
continue; | ||
} | ||
|
||
char op = pf.charAt(0); | ||
double numEnd = Double.parseDouble(stack.pop()); | ||
double numFront = Double.parseDouble(stack.pop()); | ||
|
||
String result = ""; | ||
switch (op) { | ||
case '+': | ||
result = String.valueOf(operator.add(numFront, numEnd)); | ||
break; | ||
case '-': | ||
result = String.valueOf(operator.subtract(numFront, numEnd)); | ||
break; | ||
case '*': | ||
result = String.valueOf(operator.multiply(numFront, numEnd)); | ||
break; | ||
case '/': | ||
result = String.valueOf(operator.divide(numFront, numEnd)); | ||
break; | ||
} | ||
|
||
stack.add(result); | ||
} | ||
|
||
return Double.parseDouble(stack.pop()); | ||
} | ||
|
||
private static boolean isDigit(String str) { | ||
return str.matches(positiveRegularExp); | ||
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. Pattern 컴파일 후 사용하면 더 성능적으로 좋습니다 |
||
} | ||
} |
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.
굳이 필요할까 싶습니다! checkChoiceNum 이후 필요한거면 거기에 넣어도 될것같아여