Repository navigation
계산기 [STEP2] 바드 #259
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] 바드 #259
Changes from all commits
a1b0bbd
e858cb9
2a8aa87
3c5b512
5e976a6
d75b2fb
f1d1c8b
01eef00
a0a86f5
9046d8a
a55f714
f5dcff8
923d247
af5f010
4ab5bd4
a5b027b
762d3d2
996565b
246833b
251a16a
8889676
908efaa
db2bcf2
95a8f39
63f21fb
47fef64
6e498d7
cd51a0f
0b3abdc
1cebe3b
ea890bf
470118b
300891a
2612cac
819c209
43ddcfe
7c1188a
e62b8c2
5b1e771
4ce4647
aa0ce9b
380efe3
ba58cf6
805b769
c94e67c
d02ecdd
98ddc56
de398ee
394db33
42fcfe2
5f807db
c262dcf
aa81a73
b8b8778
c1d1a0c
989610d
19a8877
f3cb41d
cded86a
5a99bca
af63b41
f9d5f4c
ea8d728
c012a17
1ca638d
3ed1c88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,20 +8,23 @@ | |
| struct CalculatorItemQueue<T>: Queue, CalculateItem { | ||
| private(set) var enQueueStack: Array<T> = [] | ||
| private(set) var deQueueStack: Array<T> = [] | ||
| private(set) var joinedQueue: Array<T> = [] | ||
|
Member
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. 요거 실제로 필요한 변수인가요~?🙋🏻
Author
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. joinedQueue라는 변수를 만들어준 이유는 한번 더 검증을 해보자는 의미에서 만들어준 변수입니다.
Member
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. 네 불필요한 변수일거라 생각듭니다..!
Member
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. 그 로직에 확신을 갖기위해 테스트 코드를 짜는것이라 생각들고 확신을 갖기 위해 여러 테스트 케이스를 거칠거라고 생각들어서요! |
||
|
|
||
| public mutating func enQueue(_ input: T) { | ||
| mutating func enQueue(_ input: T) { | ||
| enQueueStack.append(input) | ||
| } | ||
|
|
||
| public mutating func deQueue() throws -> T? { | ||
| if enQueueStack.isEmpty { | ||
| throw QueueError.empty | ||
| } | ||
| else { | ||
| mutating func deQueue() throws -> T { | ||
| if deQueueStack.isEmpty { | ||
| deQueueStack = enQueueStack.reversed() | ||
| enQueueStack.removeAll() | ||
| } | ||
| return deQueueStack.popLast() | ||
| guard let deQueue = deQueueStack.popLast() else { | ||
| throw QueueError.unknown | ||
| } | ||
|
|
||
| joinedQueue = enQueueStack + deQueueStack | ||
| return deQueue | ||
| } | ||
|
|
||
| public mutating func clearAllStacks() { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // | ||
| // OperatorError.swift | ||
| // Calculator | ||
| // | ||
| // Created by 김동용 on 2022/05/21. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| enum OperatorError: LocalizedError { | ||
| case devideFail | ||
| case unknown | ||
| } | ||
|
|
||
| extension OperatorError { | ||
| var errorDescription: String { | ||
| switch self { | ||
| case .devideFail: | ||
| return NSLocalizedString("NaN", | ||
| comment: "devide error") | ||
| case .unknown: | ||
| return NSLocalizedString("unknown error", | ||
| comment: "unknown error") | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // | ||
| // QueueError.swift | ||
| // Calculator | ||
| // | ||
| // Created by 김동용 on 2022/05/17. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| enum QueueError: LocalizedError { | ||
| case empty | ||
| case unknown | ||
| case wrongOperators | ||
| case wrongOperands | ||
| } | ||
|
|
||
| extension QueueError { | ||
| var errorDescription: String { | ||
| switch self { | ||
| case .empty: | ||
| return NSLocalizedString("empty", | ||
| comment: "deQueue stack is empty") | ||
| case .unknown: | ||
| return NSLocalizedString("unknown error", | ||
| comment: "unknown error") | ||
| case .wrongOperators: | ||
| return NSLocalizedString("operators error", | ||
| comment: "operators got error") | ||
| case .wrongOperands: | ||
| return NSLocalizedString("operands error", | ||
| comment: "operands got error") | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // | ||
| // ExpressionParser.swift | ||
| // Calculator | ||
| // | ||
| // Created by 김동용 on 2022/05/18. | ||
| // | ||
|
|
||
| enum ExpressionParser { | ||
| static func parse(from input: String) -> Formula { | ||
| var operandsQueue = CalculatorItemQueue<Double>() | ||
| var operatorQueue = CalculatorItemQueue<Operator>() | ||
|
|
||
| let operands = input.compactMap { Double(String($0)) } | ||
| let operators = componentsByOperators(from: input) | ||
| .filter{$0.count == 1} | ||
| .compactMap{ Operator(rawValue: Character($0)) } | ||
|
Comment on lines
+15
to
+16
Member
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. 요기 로직 설명 한번 해주실 수 있을까요?🙋🏻
Author
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. 연산자는 |
||
|
|
||
| operands.forEach { operandsQueue.enQueue($0) } | ||
| operators.forEach { operatorQueue.enQueue($0) } | ||
|
|
||
| return Formula(operands: operandsQueue, | ||
| operators: operatorQueue) | ||
| } | ||
|
|
||
| static private func componentsByOperators(from input: String) -> [String] { | ||
| return input.split(with: " ") | ||
|
Member
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. 문자열을 공백 기준으로 쪼개주는데 그렇다면 추후 UI에서 버튼 클릭후 연산자 혹은 피연산자 입력 시 공백이 같이 무조건 들어가게 되는건가요?
Author
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. 연산자버튼이 눌릴 때 연산자 앞뒤로 공백을 더해주면 된다고 생각했는데 다시 한번 생각해보겠습니다. |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // | ||
| // Operator.swift | ||
| // Calculator | ||
| // | ||
| // Created by 김동용 on 2022/05/18. | ||
| // | ||
|
|
||
| enum Operator: Character, CaseIterable { | ||
|
Member
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. CaseIterable은 어떤 용도로 쓰이는것이며 해당 프로젝트에서는 어떻게 쓰이게 되나요~?🙋🏻
Author
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. caseIterable을 채택함으로써 열거형 케이스를 배열처럼 사용할 수 있는 것으로 알고 있습니다.
Author
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. 해당 프로젝트에서 넣어준 이유를 생각하면 caseIterable을 사용할려면 피연산자와 연산자를 나누는 로직을 변경해봐야 할 것 같습니다.
Author
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. 아니면 현재 상황에서는 쓰일 일이 없지만 연산자 버튼을 컬렉션 타입으로 만들어준다면 |
||
| case add = "+" | ||
| case subtract = "-" | ||
| case divide = "/" | ||
| case multiply = "*" | ||
|
|
||
| func calculate(lhs: Double, rhs: Double) throws -> Double { | ||
| switch self { | ||
| case .add: | ||
| return add(lhs: lhs, rhs: rhs) | ||
| case .subtract: | ||
| return subtract(lhs: lhs, rhs: rhs) | ||
| case .divide: | ||
| return try divide(lhs: lhs, rhs: rhs) | ||
|
Member
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. 띄어쓰기가 뎁스가 한단계 더 된것 같습니다🙋🏻 |
||
| case .multiply: | ||
| return multiply(lhs: lhs, rhs: rhs) | ||
| } | ||
| } | ||
|
|
||
| private func add(lhs: Double, rhs: Double) -> Double { | ||
| return lhs + rhs | ||
| } | ||
|
|
||
| private func subtract(lhs: Double, rhs: Double) -> Double { | ||
| return lhs - rhs | ||
| } | ||
|
|
||
| private func divide(lhs: Double, rhs: Double) throws -> Double { | ||
| guard rhs != 0 else { throw OperatorError.devideFail } | ||
| return lhs / rhs | ||
| } | ||
|
|
||
| private func multiply(lhs: Double, rhs: Double) -> Double { | ||
| return lhs * rhs | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| // | ||
| // Double.swift | ||
|
Member
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. 얘도 기본 Double 타입의 extension이면 파일명의 컨벤션을 맞춰볼 수 있을것 같습니다🙋🏻 |
||
| // Calculator | ||
| // | ||
| // Created by 김동용 on 2022/05/20. | ||
| // | ||
|
|
||
| extension Double: CalculateItem { } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // | ||
| // String.swift | ||
| // Calculator | ||
| // | ||
| // Created by 김동용 on 2022/05/20. | ||
| // | ||
|
|
||
| extension String { | ||
| func split(with target: Character) -> [String] { | ||
| return self.split(separator: target).map { String($0) } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // | ||
| // Formula.swift | ||
| // Calculator | ||
| // | ||
| // Created by 김동용 on 2022/05/18. | ||
| // | ||
|
|
||
| struct Formula { | ||
| var operands = CalculatorItemQueue<Double>() | ||
| var operators = CalculatorItemQueue<Operator>() | ||
|
|
||
| mutating func result() throws -> Double { | ||
| guard var lastResult = try? operands.deQueue() else { | ||
| throw QueueError.wrongOperands | ||
| } | ||
|
|
||
| while !operands.joinedQueue.isEmpty { | ||
| guard let inputNumber = try? operands.deQueue() else { | ||
| throw QueueError.wrongOperands | ||
| } | ||
|
|
||
| guard let inputOperator = try? operators.deQueue() else { | ||
| throw QueueError.wrongOperators | ||
| } | ||
|
|
||
| do { | ||
| lastResult = try inputOperator.calculate(lhs: lastResult, | ||
| rhs: inputNumber) | ||
| } catch { | ||
| OperatorError.devideFail | ||
|
Member
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. 음 에러가 여기서 catch를 선언해주는게 맞을까요~?🙋🏻
Author
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. throw를 넣어주고 던져주었습니다.
Member
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. 아마 저 30번째 라인에서 워닝 메시지가 나올텐데 어떤건지 한번 보시면 어떻게 잘못쓰였는지 확인해보실 수 있을거 같아요..!
Author
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.
Member
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. 아아 제가 코멘트를 잘못읽었습니다!
Author
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. 넵 감사합니다..! |
||
| } | ||
| } | ||
| return lastResult | ||
| } | ||
| } | ||
This file was deleted.


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.
각 테스트들을 별도의 타깃으로 만들어주셔서 사용한 의도가 궁금합니다🙋🏻
이렇다면 각 테스트할 실 코드의 모듈들의 타겟을 다 지정해주어야하는데 번거롭지 않을까요..?
이미 기존에 있던 CalculatorTests 타겟에서 각 파일로 생성함으로 구분지어줘도 충분할것 같다고 느껴졌어요🤔
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.
별도의 파일을 만들어볼 생각을 안하고 각 테스트 별로 테스트 타겟을 만들어야 하는줄 알았습니다..!