-
Notifications
You must be signed in to change notification settings - Fork 410
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
Step2 | 문자열 계산기 #1773
Open
Slowth-KIM
wants to merge
8
commits into
next-step:slowth-kim
Choose a base branch
from
Slowth-KIM:step2
base: slowth-kim
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Step2 | 문자열 계산기 #1773
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
87a1da1
add test cases.
Slowth-KIM 07a14fb
test failed 기본 사칙 연산
Slowth-KIM 2ed85e1
test passed 기본 사칙 연산
Slowth-KIM 7e2a062
marked passed test case.
Slowth-KIM f584904
test passed 예외 처리.
Slowth-KIM 59166e5
marked passed test case.
Slowth-KIM 98d5f7b
test passed 복합 연산.
Slowth-KIM 6d1f9db
marked passed test case.
Slowth-KIM File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,22 @@ | ||
# kotlin-racingcar | ||
# kotlin-racingcar | ||
|
||
## 문자열 계산기 테스트 케이스 목록 | ||
|
||
기본 연산 테스트 | ||
|
||
- [x] 덧셈: "2 + 3" = 5 | ||
- [x] 뺄셈: "5 - 3" = 2 | ||
- [x] 곱셈: "4 * 3" = 12 | ||
- [x] 나눗셈: "8 / 2" = 4 | ||
|
||
예외 처리 테스트 | ||
|
||
- [x] null 입력 처리 | ||
- [x] 빈 문자열 입력 처리 | ||
- [x] 잘못된 연산자 입력 처리 ("2 @ 3") | ||
|
||
복합 연산 테스트 | ||
|
||
- [x] 두 개의 연산: "2 + 3 * 4" = 20 | ||
- [x] 세 개의 연산: "2 + 3 * 4 / 2" = 10 | ||
- [x] 동일 우선순위 연산: "1 + 2 + 3" = 6 | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package step2 | ||
|
||
class StringCalculator { | ||
fun calculate(expression: String): Int { | ||
validateInput(expression) | ||
|
||
val tokens = expression.trim().split(" ") | ||
|
||
var result = tokens[0].toInt() | ||
|
||
var i = 1 | ||
while (i < tokens.size - 1) { | ||
val operator = tokens[i] | ||
val number = tokens[i + 1].toInt() | ||
|
||
result = performOperation(result, operator, number) | ||
i += 2 | ||
} | ||
|
||
return result | ||
Comment on lines
+11
to
+20
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. while 대신 for 문을 사용해도 충분해보이는걸요? 😉 |
||
} | ||
|
||
private fun validateInput(expression: String) { | ||
if (expression.isBlank()) { | ||
throw IllegalArgumentException("입력값이 null이거나 빈 문자열입니다.") | ||
} | ||
|
||
val tokens = expression.trim().split(" ") | ||
if (tokens.size < 3 || tokens.size % 2 == 0) { | ||
throw IllegalArgumentException("올바르지 않은 수식 형식입니다.") | ||
} | ||
|
||
tokens.filterIndexed { index, _ -> index % 2 == 1 } | ||
.forEach { operator -> | ||
if (operator !in validOperators) { | ||
throw IllegalArgumentException("사칙연산 기호가 아닙니다.") | ||
} | ||
} | ||
} | ||
|
||
private fun performOperation(a: Int, operator: String, b: Int): Int { | ||
return when (operator) { | ||
"+" -> a + b | ||
"-" -> a - b | ||
"*" -> a * b | ||
"/" -> if (b != 0) a / b else throw IllegalArgumentException("0으로 나눌 수 없습니다.") | ||
else -> throw IllegalArgumentException("지원하지 않는 연산자입니다.") | ||
} | ||
} | ||
|
||
companion object { | ||
private val validOperators = setOf("+", "-", "*", "/") | ||
} | ||
Comment on lines
+41
to
+53
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. 0으로 나누기 불가하다는 예외처리까지 👍 |
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package step2 | ||
|
||
import io.kotest.assertions.throwables.shouldThrow | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.data.forAll | ||
import io.kotest.data.row | ||
import io.kotest.matchers.shouldBe | ||
|
||
|
||
class StringCalculatorTest : FunSpec({ | ||
val calculator = StringCalculator() | ||
|
||
context("기본 사칙 연산") { | ||
test("덧셈 연산을 수행한다") { | ||
calculator.calculate("2 + 3") shouldBe 5 | ||
} | ||
|
||
test("뺄셈 연산을 수행한다") { | ||
calculator.calculate("5 - 3") shouldBe 2 | ||
} | ||
|
||
test("곱셈 연산을 수행한다") { | ||
calculator.calculate("4 * 3") shouldBe 12 | ||
} | ||
|
||
test("나눗셈 연산을 수행한다") { | ||
calculator.calculate("8 / 2") shouldBe 4 | ||
} | ||
} | ||
|
||
|
||
context("예외 상황 처리") { | ||
test("null 또는 빈 문자열 입력시 예외가 발생한다") { | ||
shouldThrow<IllegalArgumentException> { | ||
calculator.calculate("") | ||
}.message shouldBe "입력값이 null이거나 빈 문자열입니다." | ||
|
||
shouldThrow<IllegalArgumentException> { | ||
calculator.calculate(" ") | ||
}.message shouldBe "입력값이 null이거나 빈 문자열입니다." | ||
} | ||
|
||
test("잘못된 연산자 입력시 예외가 발생한다") { | ||
forAll( | ||
row("2 @ 3"), | ||
row("4 # 5"), | ||
row("6 $ 7") | ||
) { expression -> | ||
shouldThrow<IllegalArgumentException> { | ||
calculator.calculate(expression) | ||
}.message shouldBe "사칙연산 기호가 아닙니다." | ||
} | ||
} | ||
} | ||
|
||
context("복합 연산") { | ||
test("여러 개의 연산을 순서대로 처리한다") { | ||
forAll( | ||
row("2 + 3 * 4 / 2", 10), | ||
row("1 + 2 + 3", 6), | ||
row("10 - 2 * 3", 24) | ||
) { expression, expected -> | ||
calculator.calculate(expression) shouldBe expected | ||
} | ||
} | ||
} | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
요구사항 정리 👍