-
Notifications
You must be signed in to change notification settings - Fork 411
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
base: slowth-kim
Are you sure you want to change the base?
Step2 | 문자열 계산기 #1773
Conversation
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.
안녕하세요 도희님! 2단계도 잘 진행해주셨습니다.
간결하게 잘 진행해주셔서 코멘트 드릴게 많지 않은데요~
아주 간단한 코멘트만 남겨보았으니 확인해주세요 😄
궁금하신 점 편하게 질문주세요~
|
||
- [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 |
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.
요구사항 정리 👍
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("+", "-", "*", "/") | ||
} |
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.
0으로 나누기 불가하다는 예외처리까지 👍
사칙연산 기호를 정적으로 관리하고 싶다는 니즈가 느껴지는데요,
이를 enum class 를 활용하여 따로 관리해보는 건 어떨까요?
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 |
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.
while 대신 for 문을 사용해도 충분해보이는걸요? 😉
작업내용