swift做一个简单的计算器


//
//  ViewController.swift
//  Calculator
//
//  Created by tutujiaw on 15/4/25.
//  copyright (c) 2015年 tutujiaw. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var display: UILabel!
    var sumInMemory: Double = 0.0
    var sumSoFar: Double = 0.0
    var factorSoFar: Double = 0.0
    var pendingAdditiveOperator = ""
    var pendingMultiplicativeOperator = ""
    var waitingForOperand = true
    
    var displayValue: Double {
        set {
            let intValue = Int(newValue)
            if (Double(intValue) == newValue) {
                display.text = "\(intValue)"
            } else {
                display.text = "\(newValue)"
            }
        }
        get {
            return (display.text! as Nsstring).doubleValue
        }
    }
    
    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.
    }

    func calculate(rightOperand: Double,pendingOperator: String) -> Bool {
        var result = false
        switch pendingOperator {
            case "+":
            sumSoFar += rightOperand
            result = true
            case "-":
            sumSoFar -= rightOperand
            result = true
            case "*":
            factorSoFar *= rightOperand
            result = true
            case "/":
            if rightOperand != 0.0 {
                factorSoFar /= rightOperand
                result = true
            }
        default:
            break
        }
        return result
     }
    
    func abortOperation() {
        clearall()
        display.text = "####"
    }
    
    @IBAction func digitClicked(sender: UIButton) {
        let digitValue = sender.currentTitle?.toInt()
        if display.text!.toInt() == 0 && digitValue == 0 {
            return
        }
        
        if waitingForOperand {
            display.text = ""
            waitingForOperand = false
        }
        display.text = display.text! + sender.currentTitle!
    }

    @IBAction func changeSignClicked() {
        displayValue *= -1
    }
    
    @IBAction func backspaceClicked() {
        if waitingForOperand {
            return
        }
        
        var strValue = display.text!
        display.text = dropLast(strValue)
        if display.text!.isEmpty {
            displayValue = 0.0
            waitingForOperand = true
        }
    }
    
    @IBAction func clear() {
        if waitingForOperand {
            return
        }
        
        displayValue = 0
        waitingForOperand = true
    }
    
    @IBAction func clearall() {
        sumSoFar = 0.0
        factorSoFar = 0.0
        pendingAdditiveOperator = ""
        pendingMultiplicativeOperator = ""
        displayValue = 0.0
        waitingForOperand = true
    }
    
    @IBAction func clearMemory() {
        sumInMemory = 0.0
    }
    
    @IBAction func readMemory() {
        displayValue = sumInMemory
        waitingForOperand = true
    }
    
    @IBAction func setMemory() {
        equalClicked()
        sumInMemory = displayValue
    }
    
    @IBAction func addToMemory() {
        equalClicked()
        sumInMemory += displayValue
    }
    
    @IBAction func multiplicativeOperatorClicked(sender: UIButton) {
        var clickedOperator = sender.currentTitle!
        var operand = displayValue
        if !pendingMultiplicativeOperator.isEmpty {
            if !calculate(operand,pendingOperator: pendingMultiplicativeOperator) {
                abortOperation()
                return
            }
            displayValue = factorSoFar
        } else {
            factorSoFar = operand
        }
        
        pendingMultiplicativeOperator = clickedOperator
        waitingForOperand = true
    }
    
    @IBAction func additiveOperatorClicked(sender: UIButton) {
        let clickedOperator = sender.currentTitle!
        var operand = displayValue
        if !pendingMultiplicativeOperator.isEmpty {
            if !calculate(operand,pendingOperator: pendingMultiplicativeOperator) {
                abortOperation()
                return
            }
            displayValue = factorSoFar
            factorSoFar = 0.0
            pendingMultiplicativeOperator = ""
        }
        
        if !pendingAdditiveOperator.isEmpty {
            if !calculate(operand,pendingOperator: pendingAdditiveOperator) {
                abortOperation()
                return
            }
            displayValue = sumSoFar
        } else {
            sumSoFar = operand
        }
        
        pendingAdditiveOperator = clickedOperator
        waitingForOperand = true
    }
    
    @IBAction func unaryOperatorClicked(sender: UIButton) {
        let clickedOperator = sender.currentTitle!
        var result: Double = 0
        
        if clickedOperator == "Sqrt" {
            if displayValue < 0 {
                abortOperation()
                return
            }
            result = sqrt(displayValue)
        } else if clickedOperator == "x^2" {
            result = pow(displayValue,2)
        } else if clickedOperator == "1/x" {
            if displayValue == 0 {
                abortOperation()
                return
            }
            result = 1.0 / displayValue
        }
        displayValue = result
        waitingForOperand = true
    }
    
    @IBAction func equalClicked() {
        var operand = displayValue
        if !pendingMultiplicativeOperator.isEmpty {
            if !calculate(operand,pendingOperator: pendingMultiplicativeOperator) {
                abortOperation()
                return
            }
            operand = factorSoFar
            factorSoFar = 0.0
            pendingMultiplicativeOperator = ""
        }
        
        if !pendingAdditiveOperator.isEmpty {
            if !calculate(operand,pendingOperator: pendingAdditiveOperator) {
                abortOperation()
                return
            }
            pendingAdditiveOperator = ""
        } else {
            sumSoFar = operand
        }
        
        displayValue = sumSoFar
        sumSoFar = 0.0
        waitingForOperand = true
    }
}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...