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

純代碼版

沒有多少注釋,請(qǐng)諒解
//第一次修改:"+/-"按鍵在某種情況下無效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;
    }
    
    //清除緩存席镀,重新開始計(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 = "簡單計(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)系作者
  • 序言:七十年代末戏仓,一起剝皮案震驚了整個(gè)濱河市蜓耻,隨后出現(xiàn)的幾起案子腌紧,更是在濱河造成了極大的恐慌重虑,老刑警劉巖玷或,帶你破解...
    沈念sama閱讀 216,470評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件小染,死亡現(xiàn)場(chǎng)離奇詭異次兆,居然都是意外死亡援岩,警方通過查閱死者的電腦和手機(jī)歼狼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來享怀,“玉大人羽峰,你說我怎么就攤上這事√泶桑” “怎么了梅屉?”我有些...
    開封第一講書人閱讀 162,577評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長仰坦。 經(jīng)常有香客問我履植,道長,這世上最難降的妖魔是什么悄晃? 我笑而不...
    開封第一講書人閱讀 58,176評(píng)論 1 292
  • 正文 為了忘掉前任玫霎,我火速辦了婚禮,結(jié)果婚禮上妈橄,老公的妹妹穿的比我還像新娘庶近。我一直安慰自己晌梨,他們只是感情好当辐,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,189評(píng)論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著澜薄,像睡著了一般沙热。 火紅的嫁衣襯著肌膚如雪叉钥。 梳的紋絲不亂的頭發(fā)上罢缸,一...
    開封第一講書人閱讀 51,155評(píng)論 1 299
  • 那天,我揣著相機(jī)與錄音投队,去河邊找鬼枫疆。 笑死,一個(gè)胖子當(dāng)著我的面吹牛敷鸦,可吹牛的內(nèi)容都是我干的息楔。 我是一名探鬼主播,決...
    沈念sama閱讀 40,041評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼扒披,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼值依!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起碟案,我...
    開封第一講書人閱讀 38,903評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤愿险,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后蟆淀,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體拯啦,經(jīng)...
    沈念sama閱讀 45,319評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,539評(píng)論 2 332
  • 正文 我和宋清朗相戀三年熔任,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了褒链。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,703評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡疑苔,死狀恐怖甫匹,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情惦费,我是刑警寧澤兵迅,帶...
    沈念sama閱讀 35,417評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站薪贫,受9級(jí)特大地震影響恍箭,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜瞧省,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,013評(píng)論 3 325
  • 文/蒙蒙 一扯夭、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧鞍匾,春花似錦交洗、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春置森,著一層夾襖步出監(jiān)牢的瞬間斗埂,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評(píng)論 1 269
  • 我被黑心中介騙來泰國打工暇藏, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蜜笤,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,711評(píng)論 2 368
  • 正文 我出身青樓盐碱,卻偏偏與公主長得像,于是被迫代替她去往敵國和親沪伙。 傳聞我的和親對(duì)象是個(gè)殘疾皇子瓮顽,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,601評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容

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