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

//
//  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)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,026评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,655评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,726评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,204评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,558评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,731评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,944评论 2 314
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,698评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,438评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,633评论 2 247
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,125评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,444评论 3 255
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,137评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,103评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,888评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,772评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,669评论 2 271

推荐阅读更多精彩内容

  • 转载自:https://github.com/Tim9Liu9/TimLiu-iOS[https://github...
    香橙柚子阅读 8,125评论 0 35
  • 许久没有联系,你突然传来简讯,一张风景照,你说你在沐浴阳光。我便知道你想我了。此时我只需驾车去找你。我说可以等我吗...
    忘年缄默阅读 203评论 0 0
  • 木神山,在那远古时期,这里盘踞着一个即便是放眼那块大千世界中最庞大的大陆之中,都能够算做是一方霸主的强大势力,而那...
    混沌天书阅读 328评论 0 0
  • 在我们的日常工作中,每天上下班都应该交接班, 每次只有交接到位了,接你班的同事才能顺利的完成接下来的工作! 就拿我...
    Lzr_2017阅读 133评论 0 4
  • 今天是圣诞节,但是依旧有很多人还是需要加班,还是需要学习,需要为自己的生活奋斗。资本主义的蜜糖明显还是有很大成分的...
    Heaven绿阅读 774评论 2 22