Skip to content

Commit b06aaab

Browse files
authored
Merge pull request #1 from hyeon-z/hyeon-z
feat: 계산기 구현
2 parents d46ad09 + 983947b commit b06aaab

File tree

21 files changed

+915
-0
lines changed

21 files changed

+915
-0
lines changed

.gitattributes

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#
2+
# https://help.github.com/articles/dealing-with-line-endings/
3+
#
4+
# Linux start script should use lf
5+
/gradlew text eol=lf
6+
7+
# These are Windows script files and should use crlf
8+
*.bat text eol=crlf
9+

.gitignore

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# Created by https://www.toptal.com/developers/gitignore/api/java,gradle,intellij+all
2+
# Edit at https://www.toptal.com/developers/gitignore?templates=java,gradle,intellij+all
3+
4+
### Intellij+all ###
5+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
6+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
7+
8+
# User-specific stuff
9+
.idea/**/workspace.xml
10+
.idea/**/tasks.xml
11+
.idea/**/usage.statistics.xml
12+
.idea/**/dictionaries
13+
.idea/**/shelf
14+
15+
# AWS User-specific
16+
.idea/**/aws.xml
17+
18+
# Generated files
19+
.idea/**/contentModel.xml
20+
21+
# Sensitive or high-churn files
22+
.idea/**/dataSources/
23+
.idea/**/dataSources.ids
24+
.idea/**/dataSources.local.xml
25+
.idea/**/sqlDataSources.xml
26+
.idea/**/dynamic.xml
27+
.idea/**/uiDesigner.xml
28+
.idea/**/dbnavigator.xml
29+
30+
# Gradle
31+
.idea/**/gradle.xml
32+
.idea/**/libraries
33+
34+
# Gradle and Maven with auto-import
35+
# When using Gradle or Maven with auto-import, you should exclude module files,
36+
# since they will be recreated, and may cause churn. Uncomment if using
37+
# auto-import.
38+
# .idea/artifacts
39+
# .idea/compiler.xml
40+
# .idea/jarRepositories.xml
41+
# .idea/modules.xml
42+
# .idea/*.iml
43+
# .idea/modules
44+
# *.iml
45+
# *.ipr
46+
47+
# CMake
48+
cmake-build-*/
49+
50+
# Mongo Explorer plugin
51+
.idea/**/mongoSettings.xml
52+
53+
# File-based project format
54+
*.iws
55+
56+
# IntelliJ
57+
out/
58+
59+
# mpeltonen/sbt-idea plugin
60+
.idea_modules/
61+
62+
# JIRA plugin
63+
atlassian-ide-plugin.xml
64+
65+
# Cursive Clojure plugin
66+
.idea/replstate.xml
67+
68+
# SonarLint plugin
69+
.idea/sonarlint/
70+
71+
# Crashlytics plugin (for Android Studio and IntelliJ)
72+
com_crashlytics_export_strings.xml
73+
crashlytics.properties
74+
crashlytics-build.properties
75+
fabric.properties
76+
77+
# Editor-based Rest Client
78+
.idea/httpRequests
79+
80+
# Android studio 3.1+ serialized cache file
81+
.idea/caches/build_file_checksums.ser
82+
83+
### Intellij+all Patch ###
84+
# Ignore everything but code style settings and run configurations
85+
# that are supposed to be shared within teams.
86+
87+
.idea/*
88+
89+
!.idea/codeStyles
90+
!.idea/runConfigurations
91+
92+
### Java ###
93+
# Compiled class file
94+
*.class
95+
96+
# Log file
97+
*.log
98+
99+
# BlueJ files
100+
*.ctxt
101+
102+
# Mobile Tools for Java (J2ME)
103+
.mtj.tmp/
104+
105+
# Package Files #
106+
*.jar
107+
*.war
108+
*.nar
109+
*.ear
110+
*.zip
111+
*.tar.gz
112+
*.rar
113+
114+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
115+
hs_err_pid*
116+
replay_pid*
117+
118+
### Gradle ###
119+
.gradle
120+
**/build/
121+
!src/**/build/
122+
123+
# Ignore Gradle GUI config
124+
gradle-app.setting
125+
126+
# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
127+
!gradle-wrapper.jar
128+
129+
# Avoid ignore Gradle wrappper properties
130+
!gradle-wrapper.properties
131+
132+
# Cache of project
133+
.gradletasknamecache
134+
135+
# Eclipse Gradle plugin generated files
136+
# Eclipse Core
137+
.project
138+
# JDT-specific (Eclipse Java Development Tools)
139+
.classpath
140+
141+
### Gradle Patch ###
142+
# Java heap dump
143+
*.hprof
144+
145+
# End of https://www.toptal.com/developers/gitignore/api/java,gradle,intellij+all

app/build.gradle

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* This file was generated by the Gradle 'init' task.
3+
*
4+
* This generated file contains a sample Java application project to get you started.
5+
* For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
6+
* User Manual available at https://docs.gradle.org/8.1.1/userguide/building_java_projects.html
7+
*/
8+
9+
plugins {
10+
// Apply the application plugin to add support for building a CLI application in Java.
11+
id 'application'
12+
}
13+
14+
repositories {
15+
// Use Maven Central for resolving dependencies.
16+
mavenCentral()
17+
}
18+
19+
dependencies {
20+
// Use JUnit Jupiter for testing.
21+
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.1'
22+
testImplementation "org.assertj:assertj-core:3.11.1"
23+
24+
// This dependency is used by the application.
25+
implementation 'com.google.guava:guava:31.1-jre'
26+
}
27+
28+
// Apply a specific Java toolchain to ease working on different environments.
29+
java {
30+
toolchain {
31+
languageVersion = JavaLanguageVersion.of(11)
32+
}
33+
}
34+
35+
application {
36+
// Define the main class for the application.
37+
mainClass = 'java.calculator.Main'
38+
}
39+
40+
tasks.named('test') {
41+
// Use JUnit Platform for unit tests.
42+
useJUnitPlatform()
43+
}

app/src/main/java/Main.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* This Java source file was generated by the Gradle 'init' task.
3+
*/
4+
5+
import calculator.Calculator;
6+
7+
import java.io.IOException;
8+
9+
public class Main {
10+
public static void main(String[] args) throws IOException {
11+
Calculator calculator = new Calculator();
12+
calculator.run();
13+
}
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package Print;
2+
3+
public class Choice {
4+
public static void printChoice() {
5+
System.out.println("0. 종료");
6+
System.out.println("1. 조회");
7+
System.out.println("2. 계산");
8+
9+
System.out.println();
10+
System.out.print("선택: ");
11+
}
12+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package calculator;
2+
3+
import function.Calculation;
4+
import function.Storage;
5+
6+
import java.io.BufferedReader;
7+
import java.io.IOException;
8+
import java.io.InputStreamReader;
9+
10+
import static Print.Choice.printChoice;
11+
import static validator.Validator.checkChoiceNum;
12+
13+
public class Calculator {
14+
public void run() throws IOException {
15+
BufferedReader br;
16+
Storage storage = new Storage();
17+
Calculation calculation = new Calculation();
18+
19+
while (true) {
20+
printChoice();
21+
22+
br = new BufferedReader(new InputStreamReader(System.in));
23+
int choice = checkChoiceNum(Integer.parseInt(br.readLine()));
24+
System.out.println();
25+
26+
if (choice == 0) {
27+
break;
28+
} else if (choice == 1) {
29+
System.out.println(storage.print());
30+
} else {
31+
br = new BufferedReader(new InputStreamReader(System.in));
32+
String expression = br.readLine();
33+
String result = convertByType(calculation.calculatePostfix(calculation.convertPostfix(expression)));
34+
35+
storage.store(expression + " = " + result);
36+
System.out.println(result);
37+
System.out.println();
38+
}
39+
}
40+
}
41+
42+
private String convertByType(double result) {
43+
if (result == (int) result) {
44+
return String.valueOf((int) result);
45+
}
46+
return String.valueOf(result);
47+
}
48+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package function;
2+
3+
import operator.Operator;
4+
5+
import java.util.Stack;
6+
7+
import static validator.Validator.checkPositiveNum;
8+
import static validator.Validator.checkValidOperator;
9+
10+
public class Calculation {
11+
private static int priority(char operator) {
12+
if (operator == '(' || operator == ')') {
13+
return 0;
14+
} else if (operator == '+' || operator == '-') {
15+
return 1;
16+
} else if (operator == '*' || operator == '/') {
17+
return 2;
18+
}
19+
return -1;
20+
}
21+
22+
public String convertPostfix(String expression) {
23+
Stack<Character> stack = new Stack<>();
24+
StringBuilder sb = new StringBuilder();
25+
26+
String[] split = expression.split(" ");
27+
28+
for (String now : split) {
29+
if (checkPositiveNum(now)) {
30+
sb.append(now).append(" ");
31+
continue;
32+
}
33+
34+
char c = checkValidOperator(now.charAt(0));
35+
36+
switch (c) {
37+
case '(':
38+
stack.add(c);
39+
break;
40+
41+
case ')':
42+
while (!stack.isEmpty() && !(stack.peek() == '(')) {
43+
sb.append(stack.pop()).append(" ");
44+
}
45+
stack.pop();
46+
break;
47+
default:
48+
while (!stack.isEmpty() && priority(stack.peek()) >= priority(c)) {
49+
sb.append(stack.pop()).append(" ");
50+
}
51+
stack.add(c);
52+
break;
53+
}
54+
}
55+
56+
while (!stack.isEmpty()) {
57+
sb.append(stack.pop()).append(" ");
58+
}
59+
60+
return sb.toString();
61+
}
62+
63+
public double calculatePostfix(String postfix) {
64+
String[] pfs = postfix.split(" ");
65+
Stack<String> stack = new Stack<>();
66+
Operator operator = new Operator();
67+
68+
for (String pf : pfs) {
69+
if (checkPositiveNum(pf)) {
70+
stack.add(pf);
71+
continue;
72+
}
73+
74+
char op = pf.charAt(0);
75+
double numEnd = Double.parseDouble(stack.pop());
76+
double numFront = Double.parseDouble(stack.pop());
77+
78+
String result = "";
79+
switch (op) {
80+
case '+':
81+
result = String.valueOf(operator.add(numFront, numEnd));
82+
break;
83+
case '-':
84+
result = String.valueOf(operator.subtract(numFront, numEnd));
85+
break;
86+
case '*':
87+
result = String.valueOf(operator.multiply(numFront, numEnd));
88+
break;
89+
case '/':
90+
result = String.valueOf(operator.divide(numFront, numEnd));
91+
break;
92+
}
93+
94+
stack.add(result);
95+
}
96+
97+
return Double.parseDouble(stack.pop());
98+
}
99+
}

0 commit comments

Comments
 (0)