Skip to content
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
wants to merge 8 commits into
base: slowth-kim
Choose a base branch
from

Conversation

Slowth-KIM
Copy link

작업내용

  • 테스트 케이스 markdown 추가
  • 문자열 계산기 구현

@Slowth-KIM Slowth-KIM changed the base branch from main to slowth-kim December 2, 2024 13:21
Copy link
Member

@Hyeon9mak Hyeon9mak left a comment

Choose a reason for hiding this comment

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

안녕하세요 도희님! 2단계도 잘 진행해주셨습니다.
간결하게 잘 진행해주셔서 코멘트 드릴게 많지 않은데요~
아주 간단한 코멘트만 남겨보았으니 확인해주세요 😄
궁금하신 점 편하게 질문주세요~

Comment on lines +5 to +22

- [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
Copy link
Member

Choose a reason for hiding this comment

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

요구사항 정리 👍

Comment on lines +41 to +53
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("+", "-", "*", "/")
}
Copy link
Member

Choose a reason for hiding this comment

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

0으로 나누기 불가하다는 예외처리까지 👍
사칙연산 기호를 정적으로 관리하고 싶다는 니즈가 느껴지는데요,
이를 enum class 를 활용하여 따로 관리해보는 건 어떨까요?

Comment on lines +11 to +20
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
Copy link
Member

Choose a reason for hiding this comment

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

while 대신 for 문을 사용해도 충분해보이는걸요? 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants