swift 100行代码实现的计算器

swift 100行代码实现的计算器,第1张

概述swift 100行代码实现的计算器

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

////  VIEwController.swift//  Calculator////  Created by purkylin on 14-6-19.//  copyright (c) 2014年 Purkylin. All rights reserved.//import UIKitextension String{    // subscript operator overrIDe    subscript(index:Int) -> Character?    {        var cur = 0        for c in self {            if cur == index {                return c            }        }        // return nil        let ret:Character?        return ret    }}class VIEwController: UIVIEwController {    var operand1: Int = 0;  // left operand    var operand2: Int = 0;  // right operand    var operator: Character = "#";  // operator:+-*/=    @IBOutlet var resultLabel : UILabel = nil   // output result        overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()        // Do any additional setup after loading the vIEw,typically from a nib.    }    overrIDe func dIDReceiveMemoryWarning() {        super.dIDReceiveMemoryWarning()        // dispose of any resources that can be recreated.    }        @IBAction func onClick(sender : UIbutton) {        println("Click" + sender.TitleForState(UIControlState.normal));        var label = sender.TitleForState(UIControlState.normal);        var c:Character = label[0]!        switch c{        case "+","-","*","/":            operator = c        case "=":            var result = 0            switch operator {            case "+":                result = operand1 + operand2            case "-":                result = operand1 - operand2            case "*":                result = operand1 * operand2            case "/":                result = operand1 / operand2            default:                break            }            resultLabel.text = "\(result)"            // clear status            operator = "#"            operand1 = result            operand2 = 0            break        default:            if operator=="#" {                let tmp = label.toInt()!                operand1 = operand1*10 + tmp                resultLabel.text = "\(operand1)"            }            else {                let tmp = label.toInt()!                operand2 = operand2*10 + tmp                resultLabel.text = "\(operand2)"            }        }    }    // 其实这个ACTION可以不单独提出来,都放到ONCliCK函数里处理    @IBAction func clearClick(sender : UIbutton) {        operand1 = 0        operand2 = 0        operator = "#"        resultLabel.text = "0"    }}

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的swift 100行代码实现的计算器全部内容,希望文章能够帮你解决swift 100行代码实现的计算器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1107933.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存