iOS簡(jiǎn)單項(xiàng)目 — 計(jì)算器

計(jì)算器所需求功能

 1. 加減乘除
 2. 開平方
 3. 開次方
 4. 能進(jìn)行正負(fù)轉(zhuǎn)換

計(jì)算器所用模式:MVC
計(jì)算器所用語言:Swift
計(jì)算器所參考課程:斯坦福

第一步:構(gòu)造計(jì)算器界面

許多大牛喜歡用純代碼構(gòu)造,我這種小白還是用Xcode強(qiáng)大的stroyboard。顯示器選擇Label,按鈕選擇Botton。
image.png

第二步:連接事件
顯示器Lable連接Outlet

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

數(shù)字按鈕Botton連接第一個(gè)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)
        }
    }

運(yùn)算符號(hào)Botton連接第二個(gè)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連接第三個(gè)Action

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

第三步:寫計(jì)算器的運(yùn)算部分
定義一個(gè)枚舉

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

聲明一個(gè)字典

private var operations: 
Dictionary<String,Operation> = [
        "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中編譯器自動(dòng)給參數(shù)定義了一個(gè)參數(shù)$0,第二個(gè)參數(shù)$1

計(jì)算器需要進(jìn)行二元運(yùn)算

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

計(jì)算器邏輯

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?

第四步:?jiǎn)?dòng)頁


image.png

第五步:應(yīng)用圖標(biāo)
制作不同像素的圖片進(jìn)行添加


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<String,Operation> = [
        "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
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容