美文网首页
Swift斯坦福公开课(1-3)代码中文注释

Swift斯坦福公开课(1-3)代码中文注释

作者: 郭百度 | 来源:发表于2017-10-06 16:53 被阅读107次
    //
    //  ViewController.swift
    //  calculator
    //
    //  Created by 郭百度 on 2017/9/23.
    //  Copyright © 2017年 Luke. All rights reserved.
    //
    
    import UIKit
    
    class ViewController: UIViewController {
        @IBOutlet weak var display: UILabel!
        var userIsInTheMiddleOfTyping = false
        var lessOnePoint = true
        @IBAction func touchDigit(_ sender: UIButton) {
            let digit = sender.currentTitle!
            //课后修正计算器bug添加,当输入为"."时判断,屏幕上数字是否已有小数点,是的话不执行
            let point: Character = "."
            if digit == String(point) && display.text!.contains(".") {
                    lessOnePoint = false
            }
            if lessOnePoint {
                if userIsInTheMiddleOfTyping {
                    let textCurrentlyInDisplay = display.text!
                    display.text = textCurrentlyInDisplay + digit
                    print("I'm \(digit) b")
                } else {
                    display.text = digit
                    userIsInTheMiddleOfTyping = true
                    print("I'm \(digit) c")
                }
            }
            lessOnePoint = true
        }
        
        var displayVaule: Double {
            get {
                return Double(display.text!)!
            }
            set {
                display.text = String(newValue)
            }
        }
    
        private var  brain = CalculatorBrain()
        
        @IBAction func performOperation(_ sender: UIButton) {
            //执行运算按钮中,如果用户正在键入中,那么将display值提交到brain的蓄存器中,用于实现当用户点击开根号等计算时,将屏幕display数进行缓存
            if userIsInTheMiddleOfTyping {
                brain.setOperand(displayVaule)
            }
            //标识用户停止键入
            userIsInTheMiddleOfTyping = false
            //那么让model执行数学运算符判断,将输入值sender标题→mathmaticalSymbol数学符号→brain执行运算函数
            if let mathmaticalSymbol = sender.currentTitle {
                brain.performOperation(mathmaticalSymbol)
            }
            //那么让我们将model中的结果从堆中复制给view中result,在view中将其复制给屏幕(label)
            if let result = brain.result {
                displayVaule = result
            }
        }
    }
    //
    //  CalculatorBrain.swift
    //  calculator
    //
    //  Created by 郭百度 on 2017/9/23.
    //  Copyright © 2017年 Luke. All rights reserved.
    //
    
    import Foundation
    //func changeSign(operand: Double) -> Double {
    //    return -operand
    //}
    //func multiply(op1:Double, op2:Double) -> Double{
    //    return op1 * op2
    //}
    
    
    struct CalculatorBrain {
        //构建私有变量accumulator双精度蓄存器
        private var accumulator: Double?
        
        private enum Operation {
            case constant(Double)
            case unaryOperation((Double) -> Double)
            case binaryOperation((Double,Double) -> Double)
            case equals
        }
        
        //生命私有变量operations(加了s)为字典,为字典赋值
        private var operations: Dictionary<String,Operation> = [
            "π" : Operation.constant(Double.pi),
            "e" : Operation.constant(M_E),//M_E,
            "√" : Operation.unaryOperation(sqrt),//sqrt,
            "cos" : Operation.unaryOperation(cos), //cos
            "±" : Operation.unaryOperation({-$0}), //±
            "+" : Operation.binaryOperation({ $0 + $1} ), // x *
            "-" : Operation.binaryOperation({ $0 - $1 }), // x *
            "×" : Operation.binaryOperation({ $0 * $1 }), // x *
            "÷" : Operation.binaryOperation({ $0 / $1 }), // x *
            "=" : Operation.equals
        ]
    
        //构建多变函数执行运算,输入值忽略标签,变量symbol符号,类型为字符串
        mutating func performOperation(_ symbol: String) {
            //如果字典查询operations[symbol]有值的话,将其赋值给变量operation,没有则跳过整个枚举
            if let operation = operations[symbol] {
                //对赋值后的operation进行解析
                switch operation {
                    //声明字典内提取为“值”value,将字典内常数值直接给蓄存器
                    case .constant(let value):
                        accumulator = value
                    //声明字典内提取为“函数”function,蓄存器中若不为nil,则进行sqrt、cos等枚举的函数运算,否则跳过
                    case .unaryOperation(let function):
                        if accumulator != nil {
                            accumulator = function(accumulator!)
                        }
                    //声明字典内提取为“二元函数运算”同样适用function(不同枚举不冲突),蓄存器中若不为nil,则进行枚举binaryOperation字典中的运算公式,运算函数为function,为区别,此处与官方教程不同使用了function2和functionVaule更易懂
                    case .binaryOperation(let functionVaule):
                        if accumulator != nil {
                            pendingBinaryOperation = PendingBinaryOperation(funciton2: functionVaule, firstOperand: accumulator!)
                            accumulator = nil
                        }
                    case .equals:
                        performPendingBinaryOperation()
                }
            } else {
                //之前一个用x当乘号一个用×当乘号……然后乘法一直没用……坑
                print("没有找到枚举值")
            }
        }
        
        private mutating func performPendingBinaryOperation() {
            if pendingBinaryOperation != nil && accumulator != nil {
                accumulator = pendingBinaryOperation!.perform(with: accumulator!)
                pendingBinaryOperation = nil
            }
        }
        
        private var pendingBinaryOperation: PendingBinaryOperation?
        private struct PendingBinaryOperation {
            //此处声明了二元函数运算中为两个双精度变量输入及一个双精度变量输出
            let funciton2 : (Double,Double) -> Double
            let firstOperand : Double
            
            func perform(with secondOperand:Double) -> Double {
                //此处调用了function为枚举计算公式,在swift中使用$0、$1...按顺序输入变量
                return funciton2(firstOperand,secondOperand)
            }
        }
        
        mutating func setOperand(_ operand:Double) {
            accumulator = operand
        }
        //输出结果
        var result: Double? {
            get {
                return accumulator
            }
        }
    }
    
    

    引用
    Swift 语言 iOS10 开发 斯坦福(Stanford) CS193p 公开课(1)
    Swift 语言 iOS10 开发 斯坦福(Stanford) CS193p 公开课(2)
    Swift 语言 iOS10 开发 斯坦福(Stanford) CS193p 公开课(3)

    相关文章

      网友评论

          本文标题:Swift斯坦福公开课(1-3)代码中文注释

          本文链接:https://www.haomeiwen.com/subject/delryxtx.html