|
| 1 | +// |
| 2 | +// CalculatorEngine.swift |
| 3 | +// iOS Calculator |
| 4 | +// |
| 5 | +// Created by Manas Paranjape on 12/15/18. |
| 6 | +// Copyright © 2018 DesignX6. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | + |
| 11 | + |
| 12 | +class CalculatorEngine:CalcEngineProtocol { |
| 13 | + var currentValue:Double = 0; |
| 14 | + var operation:operatorCases = .add; |
| 15 | + func clear() { |
| 16 | + operation = .add; |
| 17 | + currentValue = 0 |
| 18 | + } |
| 19 | + func fc(original: Double) -> String { |
| 20 | + return String((original - 32)/1.8) |
| 21 | + } |
| 22 | + func cf(original: Double) -> String { |
| 23 | + return String(original * 1.8 + 32) |
| 24 | + } |
| 25 | + func equalAfterthought(operationPassed: operatorCases) { |
| 26 | + operation = operationPassed |
| 27 | + } |
| 28 | + func equalPressed(operand: Double) -> Double { |
| 29 | + currentValue = operandStack.pop() |
| 30 | + print(currentValue, "I am from equalPressed") |
| 31 | + print(operation, "I am from equalPressed") |
| 32 | + switch operation { |
| 33 | + case .add: |
| 34 | + currentValue = add(operand1: currentValue, operand2: operand) |
| 35 | + case .subtract: |
| 36 | + currentValue = subtract(operand1: currentValue, operand2: operand) |
| 37 | + case .divide: |
| 38 | + currentValue = division(operand1: currentValue, operand2: operand) |
| 39 | + case .multiply: |
| 40 | + currentValue = multiply(operand1: currentValue, operand2: operand) |
| 41 | + } |
| 42 | + operandStack.push(currentValue) |
| 43 | + print(currentValue, "I am from equalPressed") |
| 44 | + print(operation, "I am from equalPressed") |
| 45 | + return currentValue |
| 46 | + } |
| 47 | + struct OperandStack { |
| 48 | + var items = [Double]() |
| 49 | + mutating func push(_ item: Double) { |
| 50 | + items.append(item) |
| 51 | + } |
| 52 | + mutating func pop() -> Double { |
| 53 | + return items.removeLast() |
| 54 | + } |
| 55 | + } |
| 56 | + var operandStack = OperandStack() |
| 57 | + func percenter(original: Double) -> Double { |
| 58 | + return (original/100) |
| 59 | + } |
| 60 | + func positiveNegative(original: Double) -> Double { |
| 61 | + let original2 = original * Double(-1) |
| 62 | + return original2 |
| 63 | + } |
| 64 | + func add(operand1: Double, operand2: Double) -> Double { |
| 65 | + return operand2 + operand1 |
| 66 | + } |
| 67 | + func subtract(operand1: Double, operand2: Double) -> Double { |
| 68 | + return operand1 - operand2 |
| 69 | + } |
| 70 | + func multiply(operand1: Double, operand2: Double) -> Double { |
| 71 | + return operand2 * operand1 |
| 72 | + } |
| 73 | + func division(operand1: Double, operand2: Double) -> Double { |
| 74 | + if operand2 == Double(0) { |
| 75 | + print("You cannot divide by zero.") |
| 76 | + } |
| 77 | + return operand1 / operand2 |
| 78 | + } |
| 79 | + func calcEngineInput(operand: Double, operationPassed: operatorCases) { |
| 80 | + if currentValue == Double(0) { |
| 81 | + operation = operationPassed |
| 82 | + operandStack.push(operand) |
| 83 | + currentValue = operand |
| 84 | + print(currentValue, "I am from CalcEngineInput") |
| 85 | + print(operation, "I am from CalcEngineInput") |
| 86 | + } else { |
| 87 | + currentValue = operandStack.pop() |
| 88 | + print(currentValue, "I am from CalcEngineInput") |
| 89 | + switch operation { |
| 90 | + case .add: |
| 91 | + currentValue = add(operand1: currentValue, operand2: operand) |
| 92 | + case .subtract: |
| 93 | + currentValue = subtract(operand1: currentValue, operand2: operand) |
| 94 | + case .multiply: |
| 95 | + currentValue = multiply(operand1: currentValue, operand2: operand) |
| 96 | + case .divide: |
| 97 | + currentValue = division(operand1: currentValue, operand2: operand) |
| 98 | + } |
| 99 | + operandStack.push(currentValue) |
| 100 | + print(currentValue, "I am from CalcEngineInput") |
| 101 | + operation = operationPassed |
| 102 | + print(operation, "I am from CalcEngineInput") |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments