//// VIEwController.swift// Calculator//// Created by tutujiaw on 15/4/25.// copyright (c) 2015年 tutujiaw. All rights reserved.//import UIKitclass 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 }}总结
以上是内存溢出为你收集整理的swift做一个简单的计算器全部内容,希望文章能够帮你解决swift做一个简单的计算器所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)