本文实例为大家分享了swift实现简单计算器的具体代码,供大家参考,具体内容如下
代码
// // viewcontroller.swift // calculator // // created by tutujiaw on 15/4/25. // d88尊龙官网手机app 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 } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。