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

계산기II [STEP1] Diana, Gray #581

Open
wants to merge 17 commits into
base: ic_11_diana
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions Calculator/Calculator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
objects = {

/* Begin PBXBuildFile section */
8B70FB552B7D070900D8229F /* Extensions+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8B70FB542B7D070900D8229F /* Extensions+.swift */; };
06D3E3FE2B870358009ADCB3 /* LinkedList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06D3E3FD2B870358009ADCB3 /* LinkedList.swift */; };
06D3E3FF2B870358009ADCB3 /* LinkedList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06D3E3FD2B870358009ADCB3 /* LinkedList.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 */; };
Expand All @@ -32,7 +36,9 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
8B70FB542B7D070900D8229F /* Extensions+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Extensions+.swift"; sourceTree = "<group>"; };
06D3E3FD2B870358009ADCB3 /* LinkedList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinkedList.swift; sourceTree = "<group>"; };
06D3E4002B8706C2009ADCB3 /* String+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+.swift"; sourceTree = "<group>"; };
8B70FB542B7D070900D8229F /* Double+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Double+.swift"; sourceTree = "<group>"; };
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 = "<group>"; };
8B8865512B7100FC00CE0652 /* CalculatorItemQueue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CalculatorItemQueue.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -70,7 +76,8 @@
8B090C372B7F50500023E1CB /* Extensions */ = {
isa = PBXGroup;
children = (
8B70FB542B7D070900D8229F /* Extensions+.swift */,
8B70FB542B7D070900D8229F /* Double+.swift */,
06D3E4002B8706C2009ADCB3 /* String+.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -113,6 +120,7 @@
8B8865562B71F3CA00CE0652 /* Models */ = {
isa = PBXGroup;
children = (
06D3E3FD2B870358009ADCB3 /* LinkedList.swift */,
8B8865512B7100FC00CE0652 /* CalculatorItemQueue.swift */,
8B88655A2B7B92F300CE0652 /* Operator.swift */,
8B88655C2B7B931A00CE0652 /* ExpressionParser.swift */,
Expand Down Expand Up @@ -260,6 +268,8 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
06D3E3FF2B870358009ADCB3 /* LinkedList.swift in Sources */,
06D3E4022B8706C2009ADCB3 /* String+.swift in Sources */,
8B88654B2B71003000CE0652 /* CalculatorTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -268,10 +278,12 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
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 */,
Expand Down
8 changes: 8 additions & 0 deletions Calculator/Extensions/Double+.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// Extensions.swift
// Calculator
//
// Created by Yejin Hong on 2024/02/14.
//

extension Double: CalculateItem { }
14 changes: 0 additions & 14 deletions Calculator/Extensions/Extensions+.swift

This file was deleted.

12 changes: 12 additions & 0 deletions Calculator/Extensions/String+.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// String+.swift
// Calculator
//
// Created by JIWOONG on 2024/02/22.
//

extension String {
func split(with target: Character) -> [String] {
return self.components(separatedBy: String(target))
}
}
78 changes: 5 additions & 73 deletions Calculator/Models/CalculatorItemQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,76 +5,8 @@
// Created by Yejin Hong on 2024/02/05.
//

private class Node<T> {
//Linked List 중첩타입
var value: T
var next: Node?

init(value: T, next: Node? = nil) {
self.value = value
self.next = next
}
}

struct LinkedList<T> {
private var head: Node<T>?
private var tail: Node<T>?
private var count: Int = 0

var isEmpty: Bool {
head == nil
}

mutating func append(_ value: T) {
count += 1

guard !isEmpty else {
head = Node(value: value)
tail = head
return
}

tail!.next = Node(value: value)
tail = tail!.next
}

mutating func pop() -> T? {
guard let head = self.head else {
return nil
}

count -= 1

if head.next == nil {
self.head = nil
return head.value
}

self.head = head.next
return head.value
}

mutating func peek() -> T? {
guard let head = self.head else {
return nil
}

return head.value
}

func size() -> Int {
return count
}

mutating func clear() {
count = 0
head = nil
tail = nil
}
}

struct CalculatorItemQueue<T: CalculateItem> {
var list = LinkedList<T>()
struct CalculatorItemQueue<Element: CalculateItem> {
private var list = LinkedList<Element>()

var isEmpty: Bool {
list.isEmpty
Expand All @@ -84,15 +16,15 @@ struct CalculatorItemQueue<T: CalculateItem> {
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()
}

Expand Down
14 changes: 7 additions & 7 deletions Calculator/Models/ExpressionParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ enum ExpressionParser {
var operators: CalculatorItemQueue<Operator> = CalculatorItemQueue()

operandElements
.compactMap{ Double($0) }
.forEach{ operands.enqueue($0) }
.compactMap { Double($0) }
.forEach { operands.enqueue($0) }

input
.compactMap { Operator(rawValue: $0) }
Expand All @@ -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: " ")
Expand All @@ -39,18 +39,18 @@ enum ExpressionParser {
struct Formula {
var operands: CalculatorItemQueue<Double>
var operators: CalculatorItemQueue<Operator>
// 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
}
}
77 changes: 77 additions & 0 deletions Calculator/Models/LinkedList.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//
// LinkedList.swift
// Calculator
//
// Created by JIWOONG on 2024/02/22.
//

private class Node<T> {
var value: T
var next: Node?

init(value: T) {
self.value = value
}
}

struct LinkedList<Element> {
private var head: Node<Element>?
private var tail: Node<Element>?

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
}
}

14 changes: 10 additions & 4 deletions Calculator/Models/Operator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Loading