iOS简单项目 — 计算器

计算器所需求功能

 1. 加减乘除
 2. 开平方
 3. 开次方
 4. 能进行正负转换

计算器所用模式:MVC
计算器所用语言:Swift
计算器所参考课程:斯坦福

第一步:构造计算器界面

许多大牛喜欢用纯代码构造,我这种小白还是用Xcode强大的stroyboard。显示器选择Label,按钮选择Botton。
image.png

第二步:连接事件
显示器Lable连接Outlet

@IBOutlet weak var display: UILabel!
var userIsInTheMiddleOfTyping = false 

数字按钮Botton连接第一个Action

@IBAction func touchDight(_ sender: UIButton) {
       let dight = sender.currentTitle!
        if userIsInTheMiddleOfTyping {
            let textCurrentlyInDisplay = display.text!
            display.text = textCurrentlyInDisplay + dight
        } else{
            display.text = dight
            userIsInTheMiddleOfTyping = true
        }
    }
    var displayValue: Double{
        get {
            return Double(display.text!)!
        }
        set{
            display.text = String(newValue)
        }
    }

运算符号Botton连接第二个Action

@IBAction func performOperation(_ sender: UIButton) {
        if userIsInTheMiddleOfTyping{
            brain.setOperand(displayValue)
            userIsInTheMiddleOfTyping = false
            
        }
        
        if let mathematicalSymbol = sender.currentTitle{
            brain.performOperation(mathematicalSymbol)
        }
        if  let result = brain.result{
            displayValue = result
        }
        
    }

AC清屏按钮Botton连接第三个Action

@IBAction func AC(_ sender: UIButton) {
        display.text = “ “;
    }
    private var brain = CalculatorBrain()

第三步:写计算器的运算部分
定义一个枚举

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

声明一个字典

private var operations: 
Dictionary = [
        "V" : Operation.unaryOperation({sqrt($0)}),
        "∛" : Operation.unaryOperation({pow($0, 1/3)}),
        "±" : Operation.unaryOperation({ -$0 }),
        "+" : Operation.binaryOperation({ $0 + $1   }),
        "-" : Operation.binaryOperation({ $0 - $1   }),
        "x" : Operation.binaryOperation({ $0 * $1   }),
        "/" : Operation.binaryOperation({ $0 / $1   }),
        "=" : Operation.equals
    ]
  • Swift中编译器自动给参数定义了一个参数$0,第二个参数$1

计算器需要进行二元运算

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

计算器逻辑

mutating func performOperation(_ symbol: String){
        if let operation = operations[symbol] {
            switch operation{
            case.constant(let Value):
                accumulator = Value
            case.unaryOperation(let function) :
                if accumulator != nil{
                    accumulator = function(accumulator!)
                }
            case.binaryOperation(let function):
                if accumulator != nil {
                    pbo = PendingBinaryOperation(function: function ,firstOperand: accumulator!)
                    accumulator = nil
                }
            case.equals:
                performPendingBinaryOperation()
            }
        }
    }
    private mutating func performPendingBinaryOperation(){
        if pbo != nil && accumulator != nil {
            accumulator = pbo!.perform(with: accumulator!)
            pbo = nil
        }
    }
    private var pbo: PendingBinaryOperation?

第四步:启动页


image.png

第五步:应用图标
制作不同像素的图片进行添加


image.png

代码部分

ViewController.swift

import UIKit

class ViewController: UIViewController {
   
    @IBOutlet weak var display: UILabel!
    var userIsInTheMiddleOfTyping = false
    
    @IBAction func touchDight(_ sender: UIButton) {
       let dight = sender.currentTitle!
        if userIsInTheMiddleOfTyping {
            let textCurrentlyInDisplay = display.text!
            display.text = textCurrentlyInDisplay + dight
        } else{
            display.text = dight
            userIsInTheMiddleOfTyping = true
        }
    }
    var displayValue: Double{
        get {
            return Double(display.text!)!
        }
        set{
            display.text = String(newValue)
        }
    }

    @IBAction func AC(_ sender: UIButton) {
        display.text = “ “;
    }
    private var brain = CalculatorBrain()
    
    @IBAction func performOperation(_ sender: UIButton) {
        if userIsInTheMiddleOfTyping{
            brain.setOperand(displayValue)
            userIsInTheMiddleOfTyping = false
            
        }
        
        if let mathematicalSymbol = sender.currentTitle{
            brain.performOperation(mathematicalSymbol)
        }
        if  let result = brain.result{
            displayValue = result
        }
        
    }
}

CalculatorBrain.swift

import Foundation

struct CalculatorBrain {
    
    private var accumulator: Double?
    
    private enum Operation{
        case constant(Double)
        case unaryOperation((Double) -> Double)
        case binaryOperation((Double,Double) -> Double)
        case equals
    }
    
    private var operations: Dictionary = [
        "V" : Operation.unaryOperation({sqrt($0)}),
        "∛" : Operation.unaryOperation({pow($0, 1/3)}),
        "±" : Operation.unaryOperation({ -$0 }),
        "+" : Operation.binaryOperation({ $0 + $1   }),
        "-" : Operation.binaryOperation({ $0 - $1   }),
        "x" : Operation.binaryOperation({ $0 * $1   }),
        "/" : Operation.binaryOperation({ $0 / $1   }),
        "=" : Operation.equals
    ]
    mutating func performOperation(_ symbol: String){
        if let operation = operations[symbol] {
            switch operation{
            case.constant(let Value):
                accumulator = Value
            case.unaryOperation(let function) :
                if accumulator != nil{
                    accumulator = function(accumulator!)
                }
            case.binaryOperation(let function):
                if accumulator != nil {
                    pbo = PendingBinaryOperation(function: function ,firstOperand: accumulator!)
                    accumulator = nil
                }
            case.equals:
                performPendingBinaryOperation()
            }
        }
    }
    private mutating func performPendingBinaryOperation(){
        if pbo != nil && accumulator != nil {
            accumulator = pbo!.perform(with: accumulator!)
            pbo = nil
        }
    }
    private var pbo: 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){
        accumulator = operand
    }
    
    var result: Double? {
        get {
            return accumulator
        }
    }
}

你可能感兴趣的:(iOS简单项目 — 计算器)