Skip to content

Commit

Permalink
Completed task 6 and closes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Vanhoef authored and Peter Vanhoef committed Mar 24, 2017
1 parent 8870e01 commit 754a7dd
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions Calculator/Calculator/CalculatorBrain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,29 @@ import Foundation
struct CalculatorBrain {

private var accumulator: Double?
private var accumulatorString: String? // todo tuple

private enum Operation {
case constant(Double)
case unaryOperation((Double) -> Double)
case binaryOperation((Double,Double) -> Double)
case unaryOperation((Double) -> Double, (String) -> String)
case binaryOperation((Double,Double) -> Double, (String,String) -> String)
case equals
}

private var operations: Dictionary<String,Operation> = [
"π" : Operation.constant(Double.pi),
"e" : Operation.constant(M_E),
"" : Operation.unaryOperation(sqrt),
"cos" : Operation.unaryOperation(cos),
"sin" : Operation.unaryOperation(sin),
"tan" : Operation.unaryOperation(tan),
"" : Operation.unaryOperation({ $0 * $0 }),
"x⁻¹" : Operation.unaryOperation({ 1 / $0 }),
"±" : Operation.unaryOperation({ -$0 }),
"×" : Operation.binaryOperation({ $0 * $1 }),
"÷" : Operation.binaryOperation({ $0 / $1 }),
"+" : Operation.binaryOperation({ $0 + $1 }),
"" : Operation.binaryOperation({ $0 - $1 }),
"" : Operation.unaryOperation(sqrt, {"√(\($0))"}),
"cos" : Operation.unaryOperation(cos, {"cos(\($0))"}),
"sin" : Operation.unaryOperation(sin, {"sin(\($0))"}),
"tan" : Operation.unaryOperation(tan, {"tan(\($0))"}),
"" : Operation.unaryOperation({ $0 * $0 }, {"\($0)²"}),
"x⁻¹" : Operation.unaryOperation({ 1 / $0 }, {"\($0)⁻¹"}),
"±" : Operation.unaryOperation({ -$0 }, {"-\($0)"}),
"×" : Operation.binaryOperation({ $0 * $1 }, {"\($0) × \($1)"}),
"÷" : Operation.binaryOperation({ $0 / $1 }, {"\($0) ÷ \($1)"}),
"+" : Operation.binaryOperation({ $0 + $1 }, {"\($0) + \($1)"}),
"" : Operation.binaryOperation({ $0 - $1 }, {"\($0)\($1)"}),
"=" : Operation.equals
]

Expand All @@ -41,15 +42,18 @@ struct CalculatorBrain {
switch operation {
case .constant(let value):
accumulator = value
case .unaryOperation(let function):
accumulatorString = symbol
case .unaryOperation(let function, let descriptionFunction):
if accumulator != nil {
accumulator = function(accumulator!)
accumulatorString = descriptionFunction(accumulatorString!)
}
case .binaryOperation(let function):
case .binaryOperation(let function, let descriptionFunction):
performPendingBinaryOperation()
if accumulator != nil {
pendingBinaryOperation = PendingBinaryOperation(function: function, firstOperand: accumulator!)
pendingBinaryOperation = PendingBinaryOperation(function: function, firstOperand: accumulator!, descriptionFunction: descriptionFunction, descriptionOperand: accumulatorString!)
accumulator = nil
accumulatorString = nil
}
break
case .equals:
Expand All @@ -61,6 +65,7 @@ struct CalculatorBrain {
private mutating func performPendingBinaryOperation() {
if pendingBinaryOperation != nil && accumulator != nil {
accumulator = pendingBinaryOperation!.perform(with: accumulator!)
accumulatorString = pendingBinaryOperation!.buildDescription(with: accumulatorString!)
pendingBinaryOperation = nil
}
}
Expand All @@ -70,14 +75,22 @@ struct CalculatorBrain {
private struct PendingBinaryOperation {
let function: (Double,Double) -> Double
let firstOperand: Double

let descriptionFunction: (String, String) -> String
let descriptionOperand: String

func perform(with secondOperand: Double) -> Double {
return function(firstOperand, secondOperand)
}

func buildDescription(with secondOperand: String) -> String {
return descriptionFunction(descriptionOperand, secondOperand)
}
}

mutating func setOperand(_ operand: Double) {
accumulator = operand
accumulatorString = String(format: "%g", operand)
}

var result: Double? {
Expand All @@ -92,5 +105,13 @@ struct CalculatorBrain {
}
}

var description: String = "Not implemented"
var description: String? {
get {
if pendingBinaryOperation != nil {
return pendingBinaryOperation!.descriptionFunction(pendingBinaryOperation!.descriptionOperand, accumulatorString ?? "")
} else {
return accumulatorString
}
}
}
}

0 comments on commit 754a7dd

Please sign in to comment.