Skip to content
Merged
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
9 changes: 9 additions & 0 deletions .gitattributes
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

145 changes: 145 additions & 0 deletions .gitignore
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
43 changes: 43 additions & 0 deletions app/build.gradle
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()
}
14 changes: 14 additions & 0 deletions app/src/main/java/Main.java
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();
}
}
12 changes: 12 additions & 0 deletions app/src/main/java/Print/Choice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package Print;

public class Choice {
public static void printChoice() {
System.out.println("0. 종료");
System.out.println("1. 조회");
System.out.println("2. 계산");

System.out.println();
System.out.print("선택: ");
}
}
48 changes: 48 additions & 0 deletions app/src/main/java/calculator/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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 {
public void run() throws IOException {
BufferedReader br;
Storage storage = new Storage();
Calculation calculation = new Calculation();

while (true) {
printChoice();

br = new BufferedReader(new InputStreamReader(System.in));
int choice = checkChoiceNum(Integer.parseInt(br.readLine()));
System.out.println();

if (choice == 0) {
break;
} else if (choice == 1) {
System.out.println(storage.print());
} else {
br = new BufferedReader(new InputStreamReader(System.in));
String expression = br.readLine();
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);
}
}
99 changes: 99 additions & 0 deletions app/src/main/java/function/Calculation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package function;

import operator.Operator;

import java.util.Stack;

import static validator.Validator.checkPositiveNum;
import static validator.Validator.checkValidOperator;

public class Calculation {
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) {
if (checkPositiveNum(now)) {
sb.append(now).append(" ");
continue;
}

char c = checkValidOperator(now.charAt(0));

switch (c) {
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<>();
Operator operator = new Operator();

for (String pf : pfs) {
if (checkPositiveNum(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());
}
}
Loading