---
tags: README
---
# ๐งฎ ๊ณ์ฐ๊ธฐ [STEP 2]
> ํผ์ฐ์ฐ์์ ์ฐ์ฐ์ ๋ฆฌ์คํธ๋ฅผ ๋ฌธ์์ด ํํ๋ก ์ ๋ฌํ๋ฉด ์ด๋ฅผ ์ฐ์ฐ์ ๊ธฐ์ค์ผ๋ก ๋ถ๋ฆฌํ๊ณ ์ซ์(Double)๋ก ๋ณํํ์ฌ ์ฐ์ฐ์ ์ํํ๋ ๋ก์ง์ ๊ตฌํํฉ๋๋ค.
>> **๋น๊ณ ์ฌํญ**
>> 1. <details><summary>(Toggle) <U>์ ํด์ง UML</U>์ ๊ธฐ๋ฐ์ผ๋ก ํ์
์ ์์ฑํ์์ต๋๋ค.</summary><div markdown="1"><img src="https://hackmd.io/_uploads/ByY8jQJvn.png"></div></details>
>> 2. STEP 1์์ ๊ตฌํํ Queue๋ฅผ ํ์ฉํ์์ต๋๋ค.
<br><br>
## ๋ชฉ์ฐจ๐
1. [ํ์์๊ฐ](##ํ์์๊ฐ๐ฉโ๐ป)
2. [ํ์๋ผ์ธ](##ํ์๋ผ์ธ๐๏ธ)
3. [๋ค์ด์ด๊ทธ๋จ](##๋ค์ด์ด๊ทธ๋จ๐)
4. [ํธ๋ฌ๋ธ์ํ
](##ํธ๋ฌ๋ธ์ํ
๐จ)
5. [์ฐธ๊ณ ์๋ฃ](##์ฐธ๊ณ ์๋ฃ๐)
6. [ํ๊ณ ](##ํ๊ณ ๐)
<br><br>
## ํ์์๊ฐ๐ฉโ๐ป
| <img src="https://hackmd.io/_uploads/r1rWKewLn.png" width="200"/> |
| :-: |
| [**maxhyunm**](https://github.com/maxhyunm)<br/> |
<br><br>
## ํ์๋ผ์ธ๐๏ธ
์์
์งํ ๊ธฐ๊ฐ | 23.06.01.(๋ชฉ) ~ 23.06.08.(๊ธ)
| ๋ ์ง | ์งํ ์ฌํญ |
| -------- | -------- |
| 23.06.01.(๋ชฉ) | Operator ํ์
์์ฑ<br/>โณ add(), subtract(), multiply(), divide() ๋ฉ์๋ ํ
์คํธ / ๊ตฌํ / ๋ฆฌํฉํ ๋ง <br/><br/>Formula ํ์
์์ฑ<br/>โณ result() ๋ฉ์๋ ํ
์คํธ / ๊ตฌํ / ๋ฆฌํฉํ ๋ง<br/><br/>EspressionParsor ํ์
์์ฑ<br/>โณ parse() ๋ฉ์๋ ํ
์คํธ / ๊ตฌํ / ๋ฆฌํฉํ ๋ง<br/>โณ componentsByOperators() ๋ฉ์๋ ํ
์คํธ / ๊ตฌํ / ๋ฆฌํฉํ ๋ง|
| 23.06.02.(๊ธ) |ExpressionParser ๋ฆฌํฉํ ๋ง, ํ
์คํธ ์ถ๊ฐ<br/>CalculatorItemNode์ ์ ๋ค๋ฆญ ์ค์ ์ถ๊ฐ<br/>ํ์ผ ์์น ์ ๋ฆฌ |
| 23.06.03.(ํ ) |์ ๋ค๋ฆญ ํ์
๋ช
์์ (T->Element) |
| 23.06.05.(์) |ํ์์๋ ๋ณ์ ์ ๊ฑฐ, ๊ฐํ ์ถ๊ฐ |
| 23.06.08.(๋ชฉ) |README ์์ฑ |
<br><br>
## ํธ๋ฌ๋ธ์ํ
๐จ
### 1๏ธโฃ ํ์
์บ์คํ
<br>
๐ **๋ฌธ์ ์ **
>์ฐ์ฐ์ ์ํ Queue๋ CalculateItem ํํ๋ก ์์๋ฅผ ์ ์ฅํ๊ณ , ์ค์ ์ฐ์ฐ์ CalculateItem์ ์ค์ํ๋ Double๊ณผ Operator ํ์
์ ๊ธฐ๋ฐ์ผ๋ก ์ด๋ฃจ์ด์ง๋ค ๋ณด๋ ๋งค๋ฒ ๋ค์ด์บ์คํ
์ ํด์ผ ํ๋ ๋ฒ๊ฑฐ๋ก์์ด ์์์ต๋๋ค.
```swift=
while true {
do {
guard let rhs = try operands.dequeue() as? Double else {
throw CalculatorError.invalidInput
}
...
guard let newOperator = try operators.dequeue() as? Operator else {
throw CalculatorError.invalidOperator
}
...
```
๐ **ํด๊ฒฐ๋ฐฉ๋ฒ**
>QueueType์ associatedtype์ ์ถ๊ฐํ๊ณ , ํด๋น ํ๋กํ ์ฝ์ ์ค์ํ๋ ํ์
๋ค๊ณผ CalculateItemNode ํ์
์ ์ ๋ค๋ฆญ์ ์ถ๊ฐํ์ฌ ํ์
์บ์คํ
๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์์ต๋๋ค.
```swift=
protocol QueueType {
associatedtype Item where Item: CalculateItem
mutating func enqueue(_ value: Item)
mutating func dequeue() throws -> Item
mutating func removeAll()
}
```
```swift=
class CalculatorItemNode<Element: CalculateItem> {
private(set) var value: Element
private(set) var next: CalculatorItemNode?
init(_ value: Element) {
self.value = value
}
...
```
---
### 2๏ธโฃ parse() ๋ง์ด๋์ค ๋ถํธ ์ฒ๋ฆฌ<br>
๐ **๋ฌธ์ ์ **<br>
>๋นผ๊ธฐ ์ฐ์ฐ์์ ๋ง์ด๋์ค๋ถํธ๋ฅผ ๊ตฌ๋ถํ๋ ๋ฐฉ๋ฒ์ ์์ด ๊ณ ๋ฏผ์ด ๋ง์์ต๋๋ค. ์ฒ์์๋ ๋ง์ด๋์ค ๋ถํธ๋ฅผ "!"๋ก ๋ณํํ์ฌ ์งํ ํ ๋ค์ ๋ณํํ๋ ๋ฐฉ์์ผ๋ก ๊ตฌํํ์ง๋ง, ๋๋ฌด ๋นํจ์จ์ ์ด์์ต๋๋ค.
```swift=
static func parse(from input: String) throws -> Formula {
...
let operatorWithMinus = [(before: "+-", after: "+!"),
(before: "--", after: "-!"),
(before: "/-", after: "/!"),
(before: "*-", after: "*!")]
let inputValue = operatorWithMinus.reduce(input) { inputString, operatorType in
inputString.replacingOccurrences(of: operatorType.before, with: operatorType.after)
}
...
}
static private func componentsByOperators(from input: String) -> [String] {
...
inputValues = inputValues.replacingOccurrences(of: "!", with: "-")
...
}
```
๐ **ํด๊ฒฐ๋ฐฉ๋ฒ** <br>
> ์คํ ๋ฆฌ๋ณด๋ ๋ฒํผ ํ์ดํ๊ฐ์ผ๋ก ๋ค์ด๊ฐ ๋นผ๊ธฐ ์ฐ์ฐ์๊ฐ ๋ง์ด๋์ค ๋ถํธ์ ๋น์ทํ ํํ์ด์ง๋ง ์ด์ง ๋ค๋ฅธ ๋ฌธ์์ด๋ก ๋์ด ์๋ค๋ ์ฌ์ค์ ์์๊ณ , ์ด ๋ฌธ์์ด์ ๊ทธ๋๋ก ํ์ฉํ๋ฉด ์์ฐ์ค๋ฝ๊ฒ ๋ง์ด๋์ค ๋ถํธ์ ๊ตฌ๋ถ์ด ๋ ๊ฒ์ด๋ผ๋ ์๊ฐ์ด ๋ค์์ต๋๋ค. ๋๋ถ์ ๋ณต์กํ๋ ๋ก์ง์ด ๋งค์ฐ ๊ฐ๊ฒฐํด์ก์ต๋๋ค.
```swift=
static func parse(from input: String) -> Formula {
...
let operandComponents = self.componentsByOperators(from: input).compactMap { Double($0) }
let operatorComponents = input.compactMap { Operator(rawValue: $0) }
...
}
```
---
### 3๏ธโฃ ExpressionParser ๋ฉ์๋ ๊ธฐ๋ฅ ๋ถ๋ฆฌ<br>
๐ **๋ฌธ์ ์ **<br>
>์ด๋ฏธ ์์ฑ๋ UML์ ํ ๋๋ก ์งํํ๋ค ๋ณด๋ ๊ฐ ๋ฉ์๋์ ์ญํ ์ด ์ด๋ค ๊ฒ์ธ์ง ํ์คํ ํ์
ํ๊ธฐ๊ฐ ์ด๋ ค์ ์ต๋๋ค. ๋ํ 1๋ฒ์ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ ์๋ ๋ง์ด๋์ค ๋ถํธ ๋ถ๋ถ๊น์ง ํฌํจํ์ฌ ๋ก์ง์ด ๋์ฑ ๋ณต์กํ๊ณ , ๊ธฐ๋ฅ๋ณ๋ก ๋ฉ์๋๊ฐ ์ ๋๋ก ๊ตฌ๋ณ๋ ๋๋์ด ๋ค์ง ์์์ต๋๋ค. ํนํ componentsByOperators() ๊ฐ์ ๊ฒฝ์ฐ ์ฐ์ฐ์๋ฅผ ํ ๋๋ก ๋ฌธ์์ด์ ์ชผ๊ฐ๋ ๊ธฐ๋ฅ์ ํ๋ ๋ฉ์๋๊ฐ ๋ง๋์ง ๋ช
ํํ์ง ์์ ๋ณด์์ต๋๋ค.
```swift
static private func componentsByOperators(from input: String) -> [String] {
let allOperators = Operator.allCases.map { $0.rawValue }
var inputValues = allOperators.reduce(input) { inputString, operatorType in
inputString.split(with: operatorType).joined(separator: "|")
}
inputValues = inputValues.replacingOccurrences(of: "!", with: "-")
let result = inputValues.split(with: "|")
return result
}
```
๐ **ํด๊ฒฐ๋ฐฉ๋ฒ** <br>
>๋ฉ์๋์ ์ด๋ฆ์ ํ ๋๋ก ์ ํํ ์ด๋ค ์ญํ ์ ํด์ผํ๋์ง๋ฅผ ์ฌ์ ๋ฆฌํ๊ณ , ์ด๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋ถํ์ํ ๋ก์ง์ ์ณ๋ด์ด ์ ๋ฆฌํ์์ต๋๋ค. ์ด ๊ณผ์ ์ ๊ฑฐ์น๋ฉด์ ์ ์ฒด์ ์ธ ๋ก์ง์ด ์ด๋ค ๋ฐฉํฅ์ผ๋ก ์ด๋ฃจ์ด์ ธ์ผ ํ๋์ง๋ ์กฐ๊ธ ๋ ๋ช
ํํด์ก์ต๋๋ค. ๋ํ return์์ ๋ฐ๋ก ํจ์ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํดํ ์ ์๋๋ก ํ์ฌ ๋ถํ์ํ ๋ณ์ ์ฌ์ฉ์ ์ค์์ต๋๋ค.
```swift=
static private func componentsByOperators(from input: String) -> [String] {
return Operator.allCases.reduce([input]) { resultArray, operatorItem in
resultArray.map { $0.split(with:operatorItem.rawValue) }.flatMap { $0 }
}
}
extension String {
func split(with target: Character) -> [String] {
return components(separatedBy: String(target))
}
}
```
---
### ๐ค ๊ณ ๋ฏผํ๋ ์ <br>
> ์ด๋ฏธ ์ ๋ฆฌ๋ UML์ ํ ๋๋ก ํ์
๊ณผ ๋ฉ์๋์ ์ญํ ์ ์ถ๋ก ํ๋ฉฐ ์์
ํ๋ ๋ฐฉ์์ด ์ต์ํ์ง ์์์ ํ์๋ณด๋ค ๋๋์ ์ธ ๋ฒ์๋ก ๋ฆฌํฉํ ๋ง์ ๋ฐ๋ณตํ๋ ์คํ
์
๋๋ค. ์ฒ์์๋ TDD๋ฅผ ์ค์ํ๋ ค ํ์ง๋ง, ์ด ๊ณผ์ ์์ ์์ฐ์ค๋ฝ๊ฒ ์ ๊ตฌํ ํ ์ ๋ํ
์คํธ ํ์์ผ๋ก ์งํํ๊ฒ ๋์์ต๋๋ค. ์ด ๋ถ๋ถ์ ๊ฐ์ ํ๊ธฐ ์ํด์๋ ์์
์ ๋ค์ด๊ฐ๊ธฐ ์ ์ ๊ฐ ํ์
์ ๋ํ ์ถฉ๋ถํ ๊ณ ๋ฏผ๊ณผ ์์ธํ ์ ๋ฆฌ๊ฐ ํ์ํ ๊ฒ ๊ฐ๋ค๋ ์๊ฐ์ด ๋ค์์ต๋๋ค.
<br><BR>
## ์ฐธ๊ณ ์๋ฃ๐
- [The Swift Programming Language - Generics](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/generics/)
- [Apple Developer Documentation - reduce(_: _:)](https://developer.apple.com/documentation/swift/array/reduce(_:_:))
- [Apple Developer Documentation - map(_:)](https://developer.apple.com/documentation/swift/array/map(_:)-87c4d)
- [Apple Developer Documentation - flatMap(_:)](https://developer.apple.com/documentation/swift/array/flatmap(_:)-6chu8)
<br><BR>
## ํ๊ณ ๐
๐ ์ต์ํ์ง ์์๋ ์ ๋ค๋ฆญ ์ฌ์ฉ๊ณผ ๊ณ ์ฐจํจ์ ์ฌ์ฉ์ ํตํด ๋ค์ํ ๊ตฌํ์ ์ฑ๊ณตํ ์
๐ ์๊ฐ ๋ด์ ์ ๋ง๋ฌด๋ฆฌํ ์
๐ ์ฌ์ ์ ํ์
๊ณผ ๋ฉ์๋์ ๋ํ ํ์
์ ์ถฉ๋ถํ ์งํํ์ง ์์ ๋ง์ ์์ ์ ๊ฑฐ์น ์