swift小程序: 計(jì)算器

純代碼版

沒(méi)有多少注釋,請(qǐng)諒解
//第一次修改:"+/-"按鍵在某種情況下無(wú)效BUG


UI圖
//
//  ViewController.swift
//  uiTest
//
//  Created by Input on 16/7/20.
//  Copyright ? 2016年 Input. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    let outputLabel = UILabel()
    let dicIntStr  = [ 11: "AC",  12: "DEL", 13: "%",   14: "/",
                       21: "7",   22: "8",   23: "9",   24: "*",
                       31: "4",   32: "5",   33: "6",   34: "-",
                       41: "1",   42: "2",   43: "3",   44: "+",
                       51: "0",   52: ".",   53: "+/-", 54: "=" ]
    
    var strOne = "0", strTwo = "", ch = "=";
    //接受運(yùn)算符事件函數(shù)
    func testOperation (operation str : String) -> Void {
        var sum :Double = 0.0
        if strTwo.isEmpty {
            strTwo = strOne;
        }
        else if ch != "=" && !strOne.isEmpty {
            switch ch {
            case "+":
                sum = (Double)(strTwo)! + (Double)(strOne)!
            case "-":
                sum = (Double)(strTwo)! - (Double)(strOne)!
            case "*":
                sum = (Double)(strTwo)! * (Double)(strOne)!
            case "/":
                sum = (Double)(strTwo)! / (Double)(strOne)!
            default:
                break
            }
            strTwo = (String)(sum)
        }
        if strTwo == "0.0" { strTwo = "0" }
        ch = str; outputLabel.text = strTwo; strOne = "0";
    }
    //數(shù)字接受事件函數(shù)
    func inputNumber (numberCharcter number : String ) -> Void {
        if ch == "=" {
            strTwo = ""
        }
        if !strOne.isEmpty{
            if strOne[strOne.characters.startIndex] == "0" && !strOne.containsString(".") {
                strOne = ""
            }
        }
        strOne += number; outputLabel.text = strOne;
    }
    func idDButton() {
        if ch == "=" {
            strTwo = ""
        }
        let count = strOne.containsString(".")
        if !count {
            if strOne.isEmpty{
                strOne += "0."
            }
            else{
                strOne += "."
            }
        }
        outputLabel.text = strOne
    }
    
    func id0Button() {
        if ch == "=" {
            strTwo = ""
        }
        if !strOne.isEmpty {
            if strOne[strOne.characters.startIndex] == "0" {
                if strOne.containsString(".") {
                    strOne += "0"
                }
            }
            else {
                strOne += "0"
            }
        }
        else {
            strOne += "0"
        }
        outputLabel.text = strOne;
    }
    
    //清除緩存,重新開(kāi)始計(jì)算
    func idACButton() {
        strOne = "0"; strTwo = ""; ch = "="
        
        outputLabel.text = strOne;
    }
    //取反
    func idXFButton() {
        if !strTwo.isEmpty && ch == "="{
            if strTwo != "0" {
                if strTwo[strTwo.characters.startIndex] == "-"{
                    strTwo.removeAtIndex(strTwo.characters.startIndex)
                }
                else {
                    strTwo.insert("-", atIndex: strTwo.characters.startIndex)
                }
                outputLabel.text = strTwo
            }
        }
        else{
            if !strOne.isEmpty && (strOne != "0" && strOne != "0."){
                if strOne[strOne.characters.startIndex] == "-"{
                    strOne.removeAtIndex(strOne.characters.startIndex)
                }
                else {
                    strOne.insert("-", atIndex: strOne.characters.startIndex)
                }
                outputLabel.text = strOne
            }
        }
    }
    //取百分?jǐn)?shù)
    func idQYButton() {
        //調(diào)用計(jì)算函數(shù):
        if strTwo.isEmpty{
            strTwo = strOne
        }
        else{
            testOperation(operation: "=")
        }
        ch = "/"; strOne = "100"
        testOperation(operation: "=")
    }
    //Del事件
    func idDelButton(){
        if !strOne.isEmpty && strOne != "0" {
            strOne = strOne.substringToIndex(strOne.endIndex.predecessor())
            if strOne.isEmpty { strOne = "0" }
        }
        if strOne == "-" {strOne = "0"}
        outputLabel.text = strOne
    }
    func funtest(tag :UIButton) {
        let T = (Int)(tag.tag)
        
        switch T {
        case 21, 22, 23, 31, 32, 33, 41, 42, 43:
            inputNumber(numberCharcter: "\((4 - T/10 )*3 + T%10)")
        case 14, 24, 34, 44, 54:
            testOperation(operation: dicIntStr[T]!)
        case 51:
            id0Button()
        case 52:
            idDButton()
        case 11:
            idACButton()
        case 13:
            idQYButton()
        case 53:
            idXFButton()
        case 12:
            idDelButton()
        default:
            break
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad(); view.backgroundColor = UIColor.init(red: 0.8, green: 0.7, blue: 0.6, alpha: 1)
        
        let wiDth = UIScreen.mainScreen().bounds.width,heiGht = UIScreen.mainScreen().bounds.height
        let butWidth = (wiDth - 9)/4, butHeight = 0.65*heiGht/5 - 3
        
        let tile = UILabel()
        
        tile.text = "簡(jiǎn)單計(jì)算器"; tile.frame = CGRectMake(0, 20, wiDth, 0.07*heiGht)
        tile.textAlignment = .Center;tile.backgroundColor = UIColor.init(red: 0.9, green: 0.5, blue: 0.3, alpha: 1)
        self.view.addSubview(tile)
        
        outputLabel.font = UIFont.systemFontOfSize(72)
        outputLabel.textAlignment = .Right;
        outputLabel.backgroundColor = UIColor.greenColor()
        outputLabel.adjustsFontSizeToFitWidth = true;
        outputLabel.text = "0"
        outputLabel.frame = CGRectMake(0, 0.1*heiGht, wiDth, 0.25*heiGht)
        self.view.addSubview(outputLabel)
        
        for i in 1 ..< 6 {
            for j in 1 ..< 5 {
                let button = UIButton()
                
                button.frame = CGRectMake((CGFloat)(j - 1)*(butWidth + 3), 0.35*heiGht + ((CGFloat)(i) - 1)*(butHeight + 3) + 3, butWidth, butHeight)
                button.tag = i*10 + j; button.setTitle(dicIntStr[button.tag], forState: .Normal)
                button.setTitleColor(UIColor.blackColor(), forState: .Highlighted)
                button.backgroundColor = UIColor.redColor()
                button.addTarget(self, action: #selector(funtest), forControlEvents: .TouchUpInside)
                
                self.view.addSubview(button)
            }
        }
    }    
}
最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,936評(píng)論 25 709
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,537評(píng)論 19 139
  • ¥開(kāi)啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開(kāi)一個(gè)線程,因...
    小菜c閱讀 7,319評(píng)論 0 17
  • 記得小時(shí)候,中秋節(jié),是滿懷期待的。大人在盼著過(guò)了中秋,就有令人喜悅的秋收了。小孩子在盼著中秋節(jié),有那硬硬邦邦帶著一...
    與你畫(huà)夕陽(yáng)閱讀 435評(píng)論 1 3
  • 早上好!開(kāi)會(huì)啦?? GP17第一次班會(huì) ?會(huì)議時(shí)間:10月31日 周二早上6:00-7:00 會(huì)議地點(diǎn):云之家~GP...
    Leena琴閱讀 188評(píng)論 0 0

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