您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“iOS實現(xiàn)計算器小功能的代碼怎么寫”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
本文利用ios實現(xiàn)計算器app,后期將用mvc結(jié)構(gòu)重構(gòu)
import UIKit class CalculViewController: UIViewController { @IBOutlet weak var display: UILabel! var userIsInTheMiddleOFTypingANumber:Bool=false @IBAction func appendDigit(sender: UIButton) { let digit=sender.currentTitle! if userIsInTheMiddleOFTypingANumber { display.text=display.text!+digit }else{ display.text=digit userIsInTheMiddleOFTypingANumber=true } } var operandstack:Array<Double>=Array<Double>() @IBAction func operate(sender: UIButton) { let operation=sender.currentTitle!; if userIsInTheMiddleOFTypingANumber { enter() } switch operation { case "×":performeOperation{$0*$1} case "÷":performeOperation{$1/$0} case "+":performeOperation{$0+$1} case "-":performeOperation{$1-$0} case "√":performeOperation{sqrt($0)} default: break } } // func multiply(op1:Double,op2:Double) -> Double { // return op1*op2; // } func performeOperation(operation:(Double,Double)->Double){ if operandstack.count>=2 { displayValue=operation(operandstack.removeLast(),operandstack.removeLast()) enter() } } private func performeOperation(operation:Double->Double){ if operandstack.count>=1 { displayValue=operation(operandstack.removeLast()) enter() } } @IBAction func enter() { userIsInTheMiddleOFTypingANumber=false operandstack.append(displayValue) print("operandstack=\(operandstack)") } var displayValue:Double{ get{ return NSNumberFormatter().numberFromString(display.text!)!.doubleValue } set{ display.text="\(newValue)" userIsInTheMiddleOFTypingANumber=false } }
計算型屬性的setter與getter
swift利用函數(shù)作為參數(shù)
swift的重載,詳情參見:swift override
效果如下
增加model文件
import Foundation class CalculatorBrain { private enum Op : CustomStringConvertible{ case operand(Double) case UnaryOperation(String,Double->Double) case BinaryOperation(String,(Double,Double)->Double) var description:String{ get{ switch self { case .operand(let operand): return "\(operand)" case .BinaryOperation(let symbol,_): return symbol case .UnaryOperation(let symbol, _): return symbol } } } } private var opstack=[Op]() private var knowOps=[String:Op]() init(){ func learnOp(op:Op){ knowOps[op.description]=op } learnOp(Op.BinaryOperation("×"){$0*$1}) learnOp(Op.BinaryOperation("÷"){$1/$0}) learnOp(Op.BinaryOperation("+"){$0+$1}) learnOp(Op.BinaryOperation("-"){$1-$0}) learnOp(Op.UnaryOperation("√"){sqrt($0)}) // knowOps["×"]=Op.BinaryOperation("×"){$0*$1} // knowOps["÷"]=Op.BinaryOperation("÷"){$1/$0} // knowOps["+"]=Op.BinaryOperation("+"){$0+$1} // knowOps["-"]=Op.BinaryOperation("-"){$1-$0} // knowOps["√"]=Op.UnaryOperation("√"){sqrt($0)} } private func evaluate(ops:[Op])->(result:Double?,remainOps:[Op]){ if !ops.isEmpty { var remainOps=ops; let op=remainOps.removeLast() switch op { case Op.operand(let operand): return(operand,remainOps) case Op.UnaryOperation(_, let operation): let operandEvalution=evaluate(remainOps) if let operand=operandEvalution.result{ return(operation(operand),operandEvalution.remainOps) } case Op.BinaryOperation(_, let operation): let operandEvlution1=evaluate(remainOps) if let operand1=operandEvlution1.result { let operandEvlution2=evaluate(operandEvlution1.remainOps) if let operand2=operandEvlution2.result { return (operation(operand1,operand2),operandEvlution2.remainOps) } } } } return (nil,ops) } func evaluate()->Double?{ let (result,remainder)=evaluate(opstack) print("\(opstack)=\(result)with\(remainder)left over") return result } func pushOperand(operand:Double)->Double?{ opstack.append(Op.operand(operand)) return evaluate() } func performOperation(symbol:String)->Double?{ if let operation=knowOps[symbol]{ opstack.append(operation) } return evaluate() } }
controll修改為
import UIKit class CalculViewController: UIViewController { @IBOutlet weak var display: UILabel! var userIsInTheMiddleOFTypingANumber:Bool=false var brain=CalculatorBrain() @IBAction func appendDigit(sender: UIButton) { let digit=sender.currentTitle! if userIsInTheMiddleOFTypingANumber { display.text=display.text!+digit }else{ display.text=digit userIsInTheMiddleOFTypingANumber=true } } //var operandstack:Array<Double>=Array<Double>() @IBAction func operate(sender: UIButton) { if userIsInTheMiddleOFTypingANumber { enter() } if let operation=sender.currentTitle{ if let result=brain.performOperation(operation) { displayValue=result }else{ displayValue=0 } } } @IBAction func enter() { userIsInTheMiddleOFTypingANumber=false if let result=brain.pushOperand(displayValue){ displayValue=result }else{ displayValue=0 } } var displayValue:Double{ get{ return NSNumberFormatter().numberFromString(display.text!)!.doubleValue } set{ display.text="\(newValue)" userIsInTheMiddleOFTypingANumber=false } } }
“iOS實現(xiàn)計算器小功能的代碼怎么寫”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。