Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 175 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
# Created by https://www.toptal.com/developers/gitignore/api/java,intellij,gradle
# Edit at https://www.toptal.com/developers/gitignore?templates=java,intellij,gradle

### Intellij ###
# 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

.idea
gradle
gradlew
gradlew.bat

# 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 Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721

# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr

# Sonarlint plugin
# https://plugins.jetbrains.com/plugin/7973-sonarlint
.idea/**/sonarlint/

# SonarQube Plugin
# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin
.idea/**/sonarIssues.xml

# Markdown Navigator plugin
# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced
.idea/**/markdown-navigator.xml
.idea/**/markdown-navigator-enh.xml
.idea/**/markdown-navigator/

# Cache file creation bug
# See https://youtrack.jetbrains.com/issue/JBR-2257
.idea/$CACHE_FILE$

# CodeStream plugin
# https://plugins.jetbrains.com/plugin/12206-codestream
.idea/codestream.xml

# Azure Toolkit for IntelliJ plugin
# https://plugins.jetbrains.com/plugin/8053-azure-toolkit-for-intellij
.idea/**/azureSettings.xml

### 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,intellij,gradle
27 changes: 27 additions & 0 deletions calculator/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
plugins {
id 'java'
}

group 'com.programmers'
version '1.0-SNAPSHOT'

repositories {
mavenCentral()
}

dependencies {
implementation 'org.junit.jupiter:junit-jupiter:5.8.1'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'

compileOnly 'org.projectlombok:lombok:1.18.28'
annotationProcessor 'org.projectlombok:lombok:1.18.28'
testCompileOnly 'org.projectlombok:lombok:1.18.28'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.28'

implementation "org.assertj:assertj-core:3.11.1"
}

test {
useJUnitPlatform()
}
2 changes: 2 additions & 0 deletions calculator/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rootProject.name = 'calculator'

15 changes: 15 additions & 0 deletions calculator/src/main/java/com/programmers/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.programmers;

import com.programmers.engine.model.CalculationFormula;
import com.programmers.engine.model.Menu;

public class App {

public static void main(String[] args) {
Menu menu = new Menu();
CalculationFormula calculationFormula = new CalculationFormula();
Calculator calculator = new Calculator(menu, menu, calculationFormula, menu);

calculator.run();
}
}
41 changes: 41 additions & 0 deletions calculator/src/main/java/com/programmers/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.programmers;

import com.programmers.engine.io.Input;
import com.programmers.engine.io.Output;
import com.programmers.engine.model.CalculationFormula;
import com.programmers.engine.model.Menu;
import lombok.AllArgsConstructor;

@AllArgsConstructor
public class Calculator implements Runnable {

public static final int HISTORY = 1;
public static final int CALCULATE = 2;
public static final int EXIT = 3;
private final Input input;
private final Output output;
private final CalculationFormula calculationFormula;
private final Menu menu;
Comment on lines +15 to +18
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interface를 썼는데 굳이 Menu를 또 받는 이유가 궁금합니다.


@Override
public void run() {
while (true) {
output.showMenu();

switch (input.selectOption()) {
case HISTORY:
calculationFormula.showResult();
continue;
case CALCULATE:
String formula = menu.getInfix();
calculationFormula.calculate(formula);
continue;
case EXIT:
output.exit();
return;
default:
output.incorrectOption();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.programmers.engine.converter;

import java.util.Stack;

public class InfixToPostfix {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다 static을 쓴 이유가 따로 있나요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다시 생각해보니 static을 사용할 이유가 없어보입니다. 수정하겠습니다!


private static int getPriority(char operator) {
switch (operator) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
}
return -1;
}
Comment on lines +7 to +17
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

우선순위는 Operator Enum에 있어도 될것같아여, 내부 변수라던가 활용하면 좋겠네요!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋은 의견 감사합니다!


public static String InfixToPostfix(String infix) {
StringBuilder postfixStringBuilder = new StringBuilder();
Stack<Character> operatorStack = new Stack<>();

for (int i = 0; i < infix.length(); i++) {
char ch = infix.charAt(i);

if (ch == ' ') {
continue;
}

if (isDigit(ch)) {
postfixStringBuilder.append(ch);
} else {
while (isPriority(operatorStack, ch)) {
postfixStringBuilder.append(operatorStack.pop());
}
operatorStack.push(ch);
}
}

while (!operatorStack.isEmpty()) {
postfixStringBuilder.append(operatorStack.pop());
}
return postfixStringBuilder.toString();
}

private static boolean isDigit(char ch) {
return Character.isDigit(ch);
}

private static boolean isPriority(Stack<Character> operatorStack, char ch) {
return !operatorStack.isEmpty() && getPriority(ch) <= getPriority(
operatorStack.peek());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.programmers.engine.converter;

import com.programmers.engine.model.Operator;
import java.util.Stack;

public class PostfixToAnswer {

public static double PostfixToAnswer(String postfix) {
Stack<Double> operandStack = new Stack<>();

for (int i = 0; i < postfix.length(); i++) {
char ch = postfix.charAt(i);

if (ch == ' ') {
continue;
}

if (isDigit(ch)) {
operandStack.push((double) (ch - '0'));
} else {
Operator operator = Operator.fromSymbol(ch);
double rightOperand = operandStack.pop();
double leftOperand = operandStack.pop();
double result = operator.calculate(leftOperand, rightOperand);
Comment on lines +14 to +24
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ch를 통해서 validation 했다면 switch도 좋아보이네요.� 하지만 수정은 하실필요없습니다. switch쓰는게 java에서는 안쓰는곳도 많은걸로알고있어서..


operandStack.push(result);
}
}
return operandStack.pop();
}

private static boolean isDigit(char ch) {
return Character.isDigit(ch);
}
}

10 changes: 10 additions & 0 deletions calculator/src/main/java/com/programmers/engine/io/Input.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.programmers.engine.io;

public interface Input {

int selectOption();

String getInfix();

String getReplacedInfix(String infix);
}
10 changes: 10 additions & 0 deletions calculator/src/main/java/com/programmers/engine/io/Output.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.programmers.engine.io;

public interface Output {

void showMenu();

void exit();

void incorrectOption();
}
Loading