ios-calculator

ios-calculator

A simple iOS app

ios calculator app

//
//  CalculatorBrain.swift
//  Calculator
//
//  Created by Ajay Ramesh on 5/21/17.
//  Copyright © 2017 Ajay Ramesh. All rights reserved.
//

import Foundation

func test(t:Double) ->Void{
    print("\(t)");
}
struct CalculatorBrain {
    private var accumalator : Double?
    
    private enum Operation {
        case constant(Double)
        case unaryOperation((Double) -> Double)
        case binaryOperation((Double, Double) -> Double)
        case equals
        case dotTODO((Double)->Void)
    }
    
    private var operations: Dictionary<String, Operation> = [
        "π" : Operation.constant(Double.pi),
        "e" : Operation.constant(M_E),
        "√" : Operation.unaryOperation(sqrt),
        "cos" : Operation.unaryOperation(cos),
        "±" : Operation.unaryOperation({-$0}),
        "×" : Operation.binaryOperation({ $0 * $1} ),
        "÷" : Operation.binaryOperation({ $0 / $1}),
        "+" : Operation.binaryOperation({ $0 + $1} ),
        "-" : Operation.binaryOperation({ $0 - $1}),
        "=" : Operation.equals,
        "." : Operation.dotTODO(test)
    ]
    
    
    mutating func performOperation(_ symbol: String )  {
        if let operation = operations[symbol] {
            switch operation {
            case .constant(let value):
                accumalator = value
            case .unaryOperation(let function):
                if accumalator != nil {
                    accumalator = function(accumalator!)
                }
            case .binaryOperation(let function):
                if accumalator != nil {
                    pendingBinaryOperation = PendingBinaryOperation(function: function, firstOperand: accumalator!)
                    accumalator = nil
                }
            case .equals:
                performPendingBinaryOperation()
            
            case .dotTODO(let function):
                if(accumalator != nil ){
                    function(accumalator!)
                }
            }
        }
    }
    
    mutating private func performPendingBinaryOperation(){
        if pendingBinaryOperation != nil && accumalator != nil {
            accumalator = pendingBinaryOperation!.perform(with: accumalator!)
            pendingBinaryOperation = nil
        }
        
    }
    
    private var pendingBinaryOperation: PendingBinaryOperation?
    
    private struct PendingBinaryOperation {
        let function: (Double, Double) -> Double
        let firstOperand: Double
        
        func perform(with secondOperand:Double) -> Double {
            return function(firstOperand, secondOperand)
        }
    }
    
    mutating func setOperand(_ operand: Double)  {
        accumalator = operand
    }
    
    var result: Double? {
        get {
            return accumalator
        }
    }
}

Main Controller source code

//
//  ViewController.swift
//  Calculator
//
//  Created by Ajay Ramesh on 4/4/17.
//  Copyright © 2017 Ajay Ramesh. All rights reserved.
//
import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var display: UILabel!
    
    var userIsInMiddleOfTyping = false
    
    @IBAction func touchDigit(_ sender: UIButton) {
        let digit = sender.currentTitle!
        if userIsInMiddleOfTyping{
            let textCurrentlyInDisplay = display!.text!
            display.text = textCurrentlyInDisplay + digit
        } else {
            display.text! = digit
            userIsInMiddleOfTyping = true
        }
    }
    
    var displayValue : Double{
        get{
            return Double(display.text!)!
        }
        set {
            display.text = String(newValue)
        }
    }
    
    private var brain = CalculatorBrain()
    
    @IBAction func performOperation(_ sender: UIButton) {
        if(userIsInMiddleOfTyping){
            brain.setOperand(displayValue)
            userIsInMiddleOfTyping = false
        }
        
        if let mathematicalSymbol = sender.currentTitle {
            brain.performOperation(mathematicalSymbol)
        }
        if let result = brain.result {
            displayValue = result
        }
    }
    
}

© 2021. All rights reserved.