From 234edb642d5875001e11a4dc2027939f8ac4dfa7 Mon Sep 17 00:00:00 2001 From: yawoong2 Date: Thu, 22 Feb 2024 12:17:28 +0900 Subject: [PATCH 01/13] =?UTF-8?q?refactor:=20=EC=A3=BC=EC=84=9D=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0,=20init()=20=ED=95=84=EC=9A=94=EC=97=86?= =?UTF-8?q?=EB=8A=94=20=EC=B4=88=EA=B8=B0=ED=99=94=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Calculator/Models/CalculatorItemQueue.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Calculator/Models/CalculatorItemQueue.swift b/Calculator/Models/CalculatorItemQueue.swift index 23b76427b..616312314 100644 --- a/Calculator/Models/CalculatorItemQueue.swift +++ b/Calculator/Models/CalculatorItemQueue.swift @@ -6,11 +6,10 @@ // private class Node { - //Linked List 중첩타입 var value: T var next: Node? - init(value: T, next: Node? = nil) { + init(value: T, next: Node?) { self.value = value self.next = next } From f63f2d917ff2ac62e7920fa4472aa4045c457d6a Mon Sep 17 00:00:00 2001 From: yawoong2 Date: Thu, 22 Feb 2024 12:32:51 +0900 Subject: [PATCH 02/13] =?UTF-8?q?refactor:=20count=EB=A5=BC=20=EC=97=B0?= =?UTF-8?q?=EC=82=B0=ED=94=84=EB=A1=9C=ED=8D=BC=ED=8B=B0=EB=A1=9C=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Calculator/Models/CalculatorItemQueue.swift | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/Calculator/Models/CalculatorItemQueue.swift b/Calculator/Models/CalculatorItemQueue.swift index 616312314..fc02804eb 100644 --- a/Calculator/Models/CalculatorItemQueue.swift +++ b/Calculator/Models/CalculatorItemQueue.swift @@ -18,15 +18,22 @@ private class Node { struct LinkedList { private var head: Node? private var tail: Node? - private var count: Int = 0 var isEmpty: Bool { head == nil } + private var count: Int { + var result = 0 + var temp = head + while temp != nil { + temp = temp?.next + result += 1 + } + return result + } + mutating func append(_ value: T) { - count += 1 - guard !isEmpty else { head = Node(value: value) tail = head @@ -42,8 +49,6 @@ struct LinkedList { return nil } - count -= 1 - if head.next == nil { self.head = nil return head.value @@ -66,7 +71,6 @@ struct LinkedList { } mutating func clear() { - count = 0 head = nil tail = nil } From c47354db15ae5743894d4c4b6fe83fbb8173f856 Mon Sep 17 00:00:00 2001 From: yawoong2 Date: Thu, 22 Feb 2024 12:45:38 +0900 Subject: [PATCH 03/13] =?UTF-8?q?refactor:=20list=20=EB=B3=80=EC=88=98=20?= =?UTF-8?q?=EC=A0=91=EA=B7=BC=EC=A0=9C=EC=96=B4=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Calculator/Models/CalculatorItemQueue.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Calculator/Models/CalculatorItemQueue.swift b/Calculator/Models/CalculatorItemQueue.swift index fc02804eb..b3df621a4 100644 --- a/Calculator/Models/CalculatorItemQueue.swift +++ b/Calculator/Models/CalculatorItemQueue.swift @@ -77,7 +77,7 @@ struct LinkedList { } struct CalculatorItemQueue { - var list = LinkedList() + private var list = LinkedList() var isEmpty: Bool { list.isEmpty From 7161bb0cde91617fc29c0dbbe62dbb80b5fb99d9 Mon Sep 17 00:00:00 2001 From: yawoong2 Date: Thu, 22 Feb 2024 13:18:14 +0900 Subject: [PATCH 04/13] =?UTF-8?q?refactor:=20=EC=A0=9C=EB=84=88=EB=A6=AD?= =?UTF-8?q?=20=ED=83=80=EC=9E=85=20=EB=AA=85=20=EB=B3=80=EA=B2=BD=20?= =?UTF-8?q?=EB=B0=8F=20peek()=20mutating=20=ED=82=A4=EC=9B=8C=EB=93=9C=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Calculator/Models/CalculatorItemQueue.swift | 26 ++++++++++----------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Calculator/Models/CalculatorItemQueue.swift b/Calculator/Models/CalculatorItemQueue.swift index b3df621a4..c83892ac0 100644 --- a/Calculator/Models/CalculatorItemQueue.swift +++ b/Calculator/Models/CalculatorItemQueue.swift @@ -15,9 +15,9 @@ private class Node { } } -struct LinkedList { - private var head: Node? - private var tail: Node? +struct LinkedList { + private var head: Node? + private var tail: Node? var isEmpty: Bool { head == nil @@ -33,18 +33,18 @@ struct LinkedList { return result } - mutating func append(_ value: T) { + mutating func append(_ value: Element) { guard !isEmpty else { head = Node(value: value) tail = head return } - tail!.next = Node(value: value) - tail = tail!.next + tail?.next = Node(value: value) + tail = tail?.next } - mutating func pop() -> T? { + mutating func pop() -> Element? { guard let head = self.head else { return nil } @@ -58,7 +58,7 @@ struct LinkedList { return head.value } - mutating func peek() -> T? { + func peek() -> Element? { guard let head = self.head else { return nil } @@ -76,8 +76,8 @@ struct LinkedList { } } -struct CalculatorItemQueue { - private var list = LinkedList() +struct CalculatorItemQueue { + private var list = LinkedList() var isEmpty: Bool { list.isEmpty @@ -87,15 +87,15 @@ struct CalculatorItemQueue { list.size() } - mutating func dequeue() -> T? { + mutating func dequeue() -> Element? { return list.pop() } - mutating func enqueue(_ value: T) { + mutating func enqueue(_ value: Element) { list.append(value) } - mutating func peek() -> T? { + mutating func peek() -> Element? { return list.peek() } From b43f862b8a14cfc9d333348531993867b1dd67d8 Mon Sep 17 00:00:00 2001 From: yawoong2 Date: Thu, 22 Feb 2024 13:25:03 +0900 Subject: [PATCH 05/13] =?UTF-8?q?refactor:=20LinkedList=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=EC=9D=84=20=EB=B6=84=EB=A6=AC=ED=95=98=EC=97=AC=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Calculator.xcodeproj/project.pbxproj | 6 ++ Calculator/Models/CalculatorItemQueue.swift | 71 ----------------- Calculator/Models/LinkedList.swift | 78 +++++++++++++++++++ 3 files changed, 84 insertions(+), 71 deletions(-) create mode 100644 Calculator/Models/LinkedList.swift diff --git a/Calculator/Calculator.xcodeproj/project.pbxproj b/Calculator/Calculator.xcodeproj/project.pbxproj index 7e3865ade..a6811d590 100644 --- a/Calculator/Calculator.xcodeproj/project.pbxproj +++ b/Calculator/Calculator.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 06D3E3FE2B870358009ADCB3 /* LinkedList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06D3E3FD2B870358009ADCB3 /* LinkedList.swift */; }; + 06D3E3FF2B870358009ADCB3 /* LinkedList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06D3E3FD2B870358009ADCB3 /* LinkedList.swift */; }; 8B70FB552B7D070900D8229F /* Extensions+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8B70FB542B7D070900D8229F /* Extensions+.swift */; }; 8B88654B2B71003000CE0652 /* CalculatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8B88654A2B71003000CE0652 /* CalculatorTests.swift */; }; 8B8865522B7100FC00CE0652 /* CalculatorItemQueue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8B8865512B7100FC00CE0652 /* CalculatorItemQueue.swift */; }; @@ -32,6 +34,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 06D3E3FD2B870358009ADCB3 /* LinkedList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinkedList.swift; sourceTree = ""; }; 8B70FB542B7D070900D8229F /* Extensions+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Extensions+.swift"; sourceTree = ""; }; 8B8865482B71003000CE0652 /* CalculatorTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CalculatorTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 8B88654A2B71003000CE0652 /* CalculatorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CalculatorTests.swift; sourceTree = ""; }; @@ -113,6 +116,7 @@ 8B8865562B71F3CA00CE0652 /* Models */ = { isa = PBXGroup; children = ( + 06D3E3FD2B870358009ADCB3 /* LinkedList.swift */, 8B8865512B7100FC00CE0652 /* CalculatorItemQueue.swift */, 8B88655A2B7B92F300CE0652 /* Operator.swift */, 8B88655C2B7B931A00CE0652 /* ExpressionParser.swift */, @@ -260,6 +264,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 06D3E3FF2B870358009ADCB3 /* LinkedList.swift in Sources */, 8B88654B2B71003000CE0652 /* CalculatorTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -268,6 +273,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 06D3E3FE2B870358009ADCB3 /* LinkedList.swift in Sources */, 8B8865592B724A4700CE0652 /* CalculateItem.swift in Sources */, 8B88655B2B7B92F300CE0652 /* Operator.swift in Sources */, C713D9462570E5EB001C3AFC /* CalculatorViewController.swift in Sources */, diff --git a/Calculator/Models/CalculatorItemQueue.swift b/Calculator/Models/CalculatorItemQueue.swift index c83892ac0..847911d2a 100644 --- a/Calculator/Models/CalculatorItemQueue.swift +++ b/Calculator/Models/CalculatorItemQueue.swift @@ -5,77 +5,6 @@ // Created by Yejin Hong on 2024/02/05. // -private class Node { - var value: T - var next: Node? - - init(value: T, next: Node?) { - self.value = value - self.next = next - } -} - -struct LinkedList { - private var head: Node? - private var tail: Node? - - var isEmpty: Bool { - head == nil - } - - private var count: Int { - var result = 0 - var temp = head - while temp != nil { - temp = temp?.next - result += 1 - } - return result - } - - mutating func append(_ value: Element) { - guard !isEmpty else { - head = Node(value: value) - tail = head - return - } - - tail?.next = Node(value: value) - tail = tail?.next - } - - mutating func pop() -> Element? { - guard let head = self.head else { - return nil - } - - if head.next == nil { - self.head = nil - return head.value - } - - self.head = head.next - return head.value - } - - func peek() -> Element? { - guard let head = self.head else { - return nil - } - - return head.value - } - - func size() -> Int { - return count - } - - mutating func clear() { - head = nil - tail = nil - } -} - struct CalculatorItemQueue { private var list = LinkedList() diff --git a/Calculator/Models/LinkedList.swift b/Calculator/Models/LinkedList.swift new file mode 100644 index 000000000..16b1d6994 --- /dev/null +++ b/Calculator/Models/LinkedList.swift @@ -0,0 +1,78 @@ +// +// LinkedList.swift +// Calculator +// +// Created by JIWOONG on 2024/02/22. +// + +private class Node { + var value: T + var next: Node? + + init(value: T, next: Node?) { + self.value = value + self.next = next + } +} + +struct LinkedList { + private var head: Node? + private var tail: Node? + + var isEmpty: Bool { + head == nil + } + + private var count: Int { + var result = 0 + var temp = head + while temp != nil { + temp = temp?.next + result += 1 + } + return result + } + + mutating func append(_ value: Element) { + guard !isEmpty else { + head = Node(value: value) + tail = head + return + } + + tail?.next = Node(value: value) + tail = tail?.next + } + + mutating func pop() -> Element? { + guard let head = self.head else { + return nil + } + + if head.next == nil { + self.head = nil + return head.value + } + + self.head = head.next + return head.value + } + + func peek() -> Element? { + guard let head = self.head else { + return nil + } + + return head.value + } + + func size() -> Int { + return count + } + + mutating func clear() { + head = nil + tail = nil + } +} + From a9660a3c398c68dc7f7b8dc98ed4727a73afeb84 Mon Sep 17 00:00:00 2001 From: yawoong2 Date: Thu, 22 Feb 2024 13:37:36 +0900 Subject: [PATCH 06/13] =?UTF-8?q?refactor:=20Extensions=20=EB=82=B4=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=EB=A5=BC=20=ED=8C=8C=EC=9D=BC=20=EB=B6=84?= =?UTF-8?q?=EB=A6=AC=ED=95=98=EC=97=AC=20=EC=A0=80=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Calculator/Calculator.xcodeproj/project.pbxproj | 14 ++++++++++---- Calculator/Extensions/Double+.swift | 8 ++++++++ .../{Extensions+.swift => String+.swift} | 6 ++---- 3 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 Calculator/Extensions/Double+.swift rename Calculator/Extensions/{Extensions+.swift => String+.swift} (63%) diff --git a/Calculator/Calculator.xcodeproj/project.pbxproj b/Calculator/Calculator.xcodeproj/project.pbxproj index a6811d590..4f9be4388 100644 --- a/Calculator/Calculator.xcodeproj/project.pbxproj +++ b/Calculator/Calculator.xcodeproj/project.pbxproj @@ -9,7 +9,9 @@ /* Begin PBXBuildFile section */ 06D3E3FE2B870358009ADCB3 /* LinkedList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06D3E3FD2B870358009ADCB3 /* LinkedList.swift */; }; 06D3E3FF2B870358009ADCB3 /* LinkedList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06D3E3FD2B870358009ADCB3 /* LinkedList.swift */; }; - 8B70FB552B7D070900D8229F /* Extensions+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8B70FB542B7D070900D8229F /* Extensions+.swift */; }; + 06D3E4012B8706C2009ADCB3 /* String+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06D3E4002B8706C2009ADCB3 /* String+.swift */; }; + 06D3E4022B8706C2009ADCB3 /* String+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06D3E4002B8706C2009ADCB3 /* String+.swift */; }; + 8B70FB552B7D070900D8229F /* Double+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8B70FB542B7D070900D8229F /* Double+.swift */; }; 8B88654B2B71003000CE0652 /* CalculatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8B88654A2B71003000CE0652 /* CalculatorTests.swift */; }; 8B8865522B7100FC00CE0652 /* CalculatorItemQueue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8B8865512B7100FC00CE0652 /* CalculatorItemQueue.swift */; }; 8B8865592B724A4700CE0652 /* CalculateItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8B8865582B724A4700CE0652 /* CalculateItem.swift */; }; @@ -35,7 +37,8 @@ /* Begin PBXFileReference section */ 06D3E3FD2B870358009ADCB3 /* LinkedList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinkedList.swift; sourceTree = ""; }; - 8B70FB542B7D070900D8229F /* Extensions+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Extensions+.swift"; sourceTree = ""; }; + 06D3E4002B8706C2009ADCB3 /* String+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+.swift"; sourceTree = ""; }; + 8B70FB542B7D070900D8229F /* Double+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Double+.swift"; sourceTree = ""; }; 8B8865482B71003000CE0652 /* CalculatorTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CalculatorTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 8B88654A2B71003000CE0652 /* CalculatorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CalculatorTests.swift; sourceTree = ""; }; 8B8865512B7100FC00CE0652 /* CalculatorItemQueue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CalculatorItemQueue.swift; sourceTree = ""; }; @@ -73,7 +76,8 @@ 8B090C372B7F50500023E1CB /* Extensions */ = { isa = PBXGroup; children = ( - 8B70FB542B7D070900D8229F /* Extensions+.swift */, + 8B70FB542B7D070900D8229F /* Double+.swift */, + 06D3E4002B8706C2009ADCB3 /* String+.swift */, ); path = Extensions; sourceTree = ""; @@ -265,6 +269,7 @@ buildActionMask = 2147483647; files = ( 06D3E3FF2B870358009ADCB3 /* LinkedList.swift in Sources */, + 06D3E4022B8706C2009ADCB3 /* String+.swift in Sources */, 8B88654B2B71003000CE0652 /* CalculatorTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -275,9 +280,10 @@ files = ( 06D3E3FE2B870358009ADCB3 /* LinkedList.swift in Sources */, 8B8865592B724A4700CE0652 /* CalculateItem.swift in Sources */, + 06D3E4012B8706C2009ADCB3 /* String+.swift in Sources */, 8B88655B2B7B92F300CE0652 /* Operator.swift in Sources */, C713D9462570E5EB001C3AFC /* CalculatorViewController.swift in Sources */, - 8B70FB552B7D070900D8229F /* Extensions+.swift in Sources */, + 8B70FB552B7D070900D8229F /* Double+.swift in Sources */, C713D9422570E5EB001C3AFC /* AppDelegate.swift in Sources */, 8B8865522B7100FC00CE0652 /* CalculatorItemQueue.swift in Sources */, 8B88655D2B7B931A00CE0652 /* ExpressionParser.swift in Sources */, diff --git a/Calculator/Extensions/Double+.swift b/Calculator/Extensions/Double+.swift new file mode 100644 index 000000000..59b7bcab5 --- /dev/null +++ b/Calculator/Extensions/Double+.swift @@ -0,0 +1,8 @@ +// +// Extensions.swift +// Calculator +// +// Created by Yejin Hong on 2024/02/14. +// + +extension Double: CalculateItem { } diff --git a/Calculator/Extensions/Extensions+.swift b/Calculator/Extensions/String+.swift similarity index 63% rename from Calculator/Extensions/Extensions+.swift rename to Calculator/Extensions/String+.swift index 36f1f8933..c3b55747a 100644 --- a/Calculator/Extensions/Extensions+.swift +++ b/Calculator/Extensions/String+.swift @@ -1,12 +1,10 @@ // -// Extensions.swift +// String+.swift // Calculator // -// Created by Yejin Hong on 2024/02/14. +// Created by JIWOONG on 2024/02/22. // -extension Double: CalculateItem { } - extension String { func split(with target: Character) -> [String] { return self.split(separator: target).map { String($0) } From 633445514351e5423641fef3f15d479725694158 Mon Sep 17 00:00:00 2001 From: yawoong2 Date: Thu, 22 Feb 2024 19:46:14 +0900 Subject: [PATCH 07/13] =?UTF-8?q?refactor:=20String=20=EB=82=B4=20split=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=EB=A5=BC=20components=20=EC=9D=B8?= =?UTF-8?q?=EC=8A=A4=ED=84=B4=EC=8A=A4=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=EB=A5=BC=20=EC=82=AC=EC=9A=A9=ED=95=98=EC=97=AC=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Calculator/Extensions/String+.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Calculator/Extensions/String+.swift b/Calculator/Extensions/String+.swift index c3b55747a..98b4d3e38 100644 --- a/Calculator/Extensions/String+.swift +++ b/Calculator/Extensions/String+.swift @@ -7,6 +7,6 @@ extension String { func split(with target: Character) -> [String] { - return self.split(separator: target).map { String($0) } + return self.components(separatedBy: String(target)) } } From 675bcb3c4b055ed2cfd3866f4e3745fd56de1e01 Mon Sep 17 00:00:00 2001 From: yawoong2 Date: Thu, 22 Feb 2024 19:56:03 +0900 Subject: [PATCH 08/13] =?UTF-8?q?refactor:=20Operator=20=EB=82=B4=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=EC=97=90=20return=20=EB=AA=85?= =?UTF-8?q?=EC=8B=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Calculator/Models/Operator.swift | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Calculator/Models/Operator.swift b/Calculator/Models/Operator.swift index c7e7d4694..5da499e02 100644 --- a/Calculator/Models/Operator.swift +++ b/Calculator/Models/Operator.swift @@ -24,17 +24,23 @@ enum Operator: Character, CaseIterable, CalculateItem { } } - private func add(lhs: Double, rhs: Double) -> Double { lhs + rhs } + private func add(lhs: Double, rhs: Double) -> Double { + return lhs + rhs + } - private func substract(lhs: Double, rhs: Double) -> Double { lhs - rhs } + private func substract(lhs: Double, rhs: Double) -> Double { + return lhs - rhs + } private func divide(lhs: Double, rhs: Double) -> Double { if rhs == 0 { - return .infinity + return .nan } return lhs / rhs } - private func multiply(lhs: Double, rhs: Double) -> Double { lhs * rhs } + private func multiply(lhs: Double, rhs: Double) -> Double { + return lhs * rhs + } } From 183bab45e35e7dc1e0d9c7d29ccfd6d789c81799 Mon Sep 17 00:00:00 2001 From: yawoong2 Date: Thu, 22 Feb 2024 21:06:42 +0900 Subject: [PATCH 09/13] =?UTF-8?q?refactor:=20Node=20class=20=EB=82=B4=20in?= =?UTF-8?q?it()=EC=9D=98=20=EB=A7=A4=EA=B0=9C=EB=B3=80=EC=88=98=EB=A5=BC?= =?UTF-8?q?=20value=EB=A7=8C=20=EB=B0=9B=EB=8F=84=EB=A1=9D=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Calculator/Models/LinkedList.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Calculator/Models/LinkedList.swift b/Calculator/Models/LinkedList.swift index 16b1d6994..e092dc7bf 100644 --- a/Calculator/Models/LinkedList.swift +++ b/Calculator/Models/LinkedList.swift @@ -9,9 +9,8 @@ private class Node { var value: T var next: Node? - init(value: T, next: Node?) { + init(value: T) { self.value = value - self.next = next } } From 5424194bc194f2d2954f99a4304a38bd4cb0f71e Mon Sep 17 00:00:00 2001 From: yawoong2 Date: Thu, 22 Feb 2024 21:08:48 +0900 Subject: [PATCH 10/13] =?UTF-8?q?refactor:=20componentsByOperators()?= =?UTF-8?q?=EB=A5=BC=20replacingOccurrences()=20=EC=9D=B8=EC=8A=A4?= =?UTF-8?q?=ED=84=B4=EC=8A=A4=20=EB=A9=94=EC=84=9C=EB=93=9C=EB=A5=BC=20?= =?UTF-8?q?=ED=99=9C=EC=9A=A9=ED=95=98=EC=97=AC=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Calculator/Models/ExpressionParser.swift | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Calculator/Models/ExpressionParser.swift b/Calculator/Models/ExpressionParser.swift index b6b701e88..1e32a4461 100644 --- a/Calculator/Models/ExpressionParser.swift +++ b/Calculator/Models/ExpressionParser.swift @@ -14,8 +14,8 @@ enum ExpressionParser { var operators: CalculatorItemQueue = CalculatorItemQueue() operandElements - .compactMap{ Double($0) } - .forEach{ operands.enqueue($0) } + .compactMap { Double($0) } + .forEach { operands.enqueue($0) } input .compactMap { Operator(rawValue: $0) } @@ -29,7 +29,7 @@ enum ExpressionParser { let operators: [Character] = Operator.allCases.compactMap{ $0.rawValue } operators.forEach { - components = components.split(with: $0).joined(separator: " ") + components = components.replacingOccurrences(of: String($0), with: " ") } return components.split(with: " ") @@ -39,18 +39,18 @@ enum ExpressionParser { struct Formula { var operands: CalculatorItemQueue var operators: CalculatorItemQueue -// 1 + 2 * 3 + mutating func result() -> Double { - var calculateResult = operands.dequeue() ?? 0.0 + var result = operands.dequeue() ?? 0.0 while let operand = operands.dequeue() { guard let `operator` = operators.dequeue() else { return .nan } - calculateResult = `operator`.calculate(lhs: calculateResult, rhs: operand) + result = `operator`.calculate(lhs: result, rhs: operand) } - return calculateResult + return result } } From 47b4b39bcdf5a2d9a48a04a912f9dbd8f3be84a6 Mon Sep 17 00:00:00 2001 From: Gray <139211436+yawoong2@users.noreply.github.com> Date: Fri, 23 Feb 2024 16:33:45 +0900 Subject: [PATCH 11/13] =?UTF-8?q?docs:=20README=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Diana-yjh --- README.md | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 120 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7416002ae..c451f6972 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,123 @@ -## iOS 커리어 스타터 캠프 +## Diana, Gray의 이상한 계산기 🧮 -### 계산기 프로젝트 저장소 +### 목차 +[1. 소개](#1-소개) +[2. 팀원](#2-팀원) +[3. 타임라인](#3-타임라인) +[4. 실행 화면](#4-실행-화면) +[5. 트러블슈팅](#5-트러블슈팅) +[6. 팀 회고](#6-팀-회고) +[7. 참고 자료](#7-참고-자료) + +--- +### 1. 소개 +#### **Diana, Gray의 이상한 계산기 🧮** + 아이폰의 계산기와 동일해보이지만, 사칙연산 계산 순서는 우리가 아는 것과는 조금 다른 계산기입니다.
+ 어떻게 다른지 직접 이상한 계산기를 이용해 보세요! 🙃 + +### 2. 팀원 +| | | +| :---: | :---: | +| Prism ([Github](https://github.com/Diana-yjh/)) | Gray ([Github](https://github.com/yawoong2)) | + +### 3. 타임라인 +| 날짜 | 제목 | +| --- | --- | +| 24.02.05(월) ~ 24.02.07(수) | __피연산자와 연산자를 입력받아 사칙연산을 처리하기 위한 Queue 및 LinkeList구현__
- Node class 생성 및 LinkedList(append, pop, peek, size, clear) 구현
- LinkedList에 구현한 메서드를 활용하여 Queue(enqueue, dequeue) 구현 | +| 24.02.13(화) ~ 24.02.15(목) | __계산기로부터 입력받은 피연산자와 연산자를 Queue에 각각 저장하기 위한 Formula, ExpressionParser 구현__
- Formula : 입력 받은 피연산자와 연산자를 Queue에 삽입
- ExpressionParser : 입력 받은 값들 중 피연산자 값들만 배열로 저장 +| 24.02.19(월) ~ 24.02.22(목) | __입력 받은 계산을 처리하는 모델 병합 및 리팩토링__
- 어느 코드가 적절한지 고려하며, 코드 병합
- 시간복잡도를 고려하여 components(separatedBy:), 및 split(separotor:).map 중 알맞은 메서드 선택
- 시간복잡도를 고려하여 split(with:).joined(), 및 replacingOccurrences(of:,wifh:) 중 알맞은 메서드 선택| +| 24.02.23(금) | README 작성 | + +### 4. 실행 화면 +추가 예정 + +### 5. 트러블슈팅 + +#### ❗️연산을 처리할 때, 초기화 방법 수정 +##### 📌 문제 상황 +```swift +mutating func result() -> Double { + var calculateResult = 0.0 + + while let operand = operands.dequeue() { + guard let `operator` = operators.dequeue() else { + return .nan + } + + calculateResult = `operator`.calculate(lhs: calculateResult, rhs: operand) + } + + return calculateResult +} +``` +처음 calculateResult를 생성한 경우 0.0으로 초기화 해주었는데 이 경우 처음 calculate할 값으로 0.0을 사용하여 operator와 operand의 짝이 맞지 않게 됨. + +##### 🛠️ 해결 방법 +```swift +mutating func result() -> Double { + var calculateResult = operands.dequeue() + + while let operand = operands.dequeue() { + guard let `operator` = operators.dequeue() else { + return .nan + } + + calculateResult = `operator`.calculate(lhs: calculateResult, rhs: operand) + } + + return calculateResult +} +``` +calculateResult 생성 시 초기 값을 operands의 dequeue값 으로 설정하여 문제 해결 + +#### ❗️시간복잡도를 고려한 인스턴스 메서드 선택 +##### 📌 문제 상황 +```swift +// String.swift +extension String { + func split(with target: Character) -> [String] { + return self.split(separator: target).map { String($0) } + } +} +``` +서로의 코드를 비교하며 병합했을 때, 각각 다른 인스턴스 메서드를 사용하여 구현한 부분이 있어 어느 방법을 채택할까 고민을 했습니다. + 위의 방법에서는 split()을 수행한 뒤, map()을 수행하는 로직이라 O(n)의 시간복잡도가 걸리는 메서드를 두 번 실행합니다. + +##### 🛠️ 해결 방법 +```swift +// String.swift +extension String { + func split(with target: Character) -> [String] { + return self.components(separatedBy: String(target)) + } +} +``` + 위의 코드에서는 String으로 형변환한 값을 components(separatedBy:)의 인스턴스 메서드를 수행하기 때문에 시간복잡도가 O(n)으로 완료됩니다. 따라서, components 인스턴스 메서드를 활용한 코드로 수정했습니다. + +### 6. 팀 회고 +#### 😍 우리팀이 잘한 점 +1. 프로젝트를 진행하면서 꾸준히 소통 +2. 코드 작성 전 충분히 공부하는 시간을 가짐 + +#### 💦 우리팀 개선할 점 +1. 일정을 맞추지 못해 프로젝트 완성이 안됨 ㅠ +2. 예외처리에 대한 내용을 디테일하게 검토하지 않음 + +#### 😊 서로에게 좋았던 점 피드백 +1. Diana: 그레이가 일정이 빡빡한데 열심히 준비하고 포기하지 않고 STEP2까지 완료해주어서 병합하기 원활했습니다~ 😌 +2. Gray: 코드를 작성하는데 연습이 될 수 있도록 반복 학습 할 수 있도록 기회와 힌트를 주신 점이 매우 좋았습니다! 덕분에 빠른 시간에 진도를 쫓아갈 수 있었습니다~ 😇 + +#### 💦 자신에게 아쉬웠던 점 +1. Diana: 휴일이 껴있기는 했지만 일정을 맞추지 못해 STEP3를 완성하지 못한 점이 아쉽습니다. +2. Gray: 건강 관리에 신경쓰지 못해 매우 늦게 프로젝트를 시작하게 되었고, 멘탈 관리의 필요성을 느꼈습니다. 코드 작성의 기초단계가 탄탄하지 못하다고 느꼈습니다. + +### 7. 참고 자료 +📍[Apple, Swift documentation, Enumeration]() +📍[Apple, Swift documentation, Double]() +📍[그린, Tistory, Swift split vs component 차이]() +📍[jeonyeohun, Tistory, 스위프트로 구현하는 자료구조 2: 링크드 리스트(Linked List)]() +📍[HeeJeong Kwon, Github, [자료구조] 큐(Queue)란]() +📍[Apple, Swift documentation, replacingOccurrences(of:with:)]() +📍[ZeddiOS, Tistory, String쪼개기]() -- 이 저장소를 자신의 저장소로 fork하여 프로젝트를 진행합니다 From 3bdc31ec64485b993d9d57e722cf6d2db8080f02 Mon Sep 17 00:00:00 2001 From: Gray <139211436+yawoong2@users.noreply.github.com> Date: Fri, 23 Feb 2024 16:48:34 +0900 Subject: [PATCH 12/13] =?UTF-8?q?=08docs:=20README=20=EB=9D=84=EC=96=B4?= =?UTF-8?q?=EC=93=B0=EA=B8=B0=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c451f6972..4b8ab3ba1 100644 --- a/README.md +++ b/README.md @@ -116,8 +116,8 @@ extension String { 📍[Apple, Swift documentation, Double]() 📍[그린, Tistory, Swift split vs component 차이]() 📍[jeonyeohun, Tistory, 스위프트로 구현하는 자료구조 2: 링크드 리스트(Linked List)]() -📍[HeeJeong Kwon, Github, [자료구조] 큐(Queue)란]() -📍[Apple, Swift documentation, replacingOccurrences(of:with:)]() +📍[HeeJeong Kwon, Github, [자료구조] 큐(Queue)란]() +📍[Apple, Swift documentation, replacingOccurrences(of:with:)]() 📍[ZeddiOS, Tistory, String쪼개기]() From 625f576d36fe2e97f7bc6282400a8b3208e32dde Mon Sep 17 00:00:00 2001 From: Diana <57698939+Diana-yjh@users.noreply.github.com> Date: Mon, 26 Feb 2024 14:41:25 +0900 Subject: [PATCH 13/13] =?UTF-8?q?Docs:=20=ED=8C=80=EC=9B=90=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=EC=98=A4=ED=83=80=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4b8ab3ba1..a0eb6ea88 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ ### 2. 팀원 | | | | :---: | :---: | -| Prism ([Github](https://github.com/Diana-yjh/)) | Gray ([Github](https://github.com/yawoong2)) | +| Diana ([Github](https://github.com/Diana-yjh/)) | Gray ([Github](https://github.com/yawoong2)) | ### 3. 타임라인 | 날짜 | 제목 |