最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS實現(xiàn)計算器小功能

 更新時間:2022年01月27日 12:14:44   作者:Ricardo.M.Jiang  
這篇文章主要介紹了iOS實現(xiàn)計算器小功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了iOS實現(xiàn)計算器小功能,供大家參考,具體內(nèi)容如下

本文利用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
? ? ? ? }
? ? }


}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS監(jiān)控筆記之啟動crash

    iOS監(jiān)控筆記之啟動crash

    iOS崩潰是讓iOS開發(fā)人員比較頭痛的事情,app崩潰了,說明代碼寫的有問題,下面這篇文章主要給大家介紹了關(guān)于iOS監(jiān)控筆記之啟動crash的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-05-05
  • iOS通過UISwitch控制搖一搖

    iOS通過UISwitch控制搖一搖

    這篇文章主要為大家詳細(xì)介紹了iOS通過UISwitch控制搖一搖,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • 解決Charles抓包https時,無法查看CONNECT請求的問題

    解決Charles抓包https時,無法查看CONNECT請求的問題

    下面小編就為大家分享一篇解決Charles抓包https時,無法查看CONNECT請求的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • IOS開發(fā)之JSON轉(zhuǎn)PLIST實例詳解

    IOS開發(fā)之JSON轉(zhuǎn)PLIST實例詳解

    這篇文章主要介紹了IOS開發(fā)之JSON轉(zhuǎn)PLIST實例詳解的相關(guān)資料,需要的朋友可以參考下
    2016-11-11
  • 淺談WKWebView 在64位設(shè)備上的白屏問題

    淺談WKWebView 在64位設(shè)備上的白屏問題

    下面小編就為大家?guī)硪黄獪\談WKWebView 在64位設(shè)備上的白屏問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • iOS繪制專屬于程序猿的浪漫愛心

    iOS繪制專屬于程序猿的浪漫愛心

    誰說程序猿不懂浪漫,這篇文章主要介紹了iOS繪制專屬于程序猿的浪漫愛心,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-03-03
  • iOS關(guān)閉虛擬鍵盤方法匯總

    iOS關(guān)閉虛擬鍵盤方法匯總

    在iOS應(yīng)用開發(fā)中,有三類視圖對象會打開虛擬鍵盤,進(jìn)行輸入操作,但如何關(guān)閉虛擬鍵盤,卻沒有提供自動化的方法
    2016-04-04
  • IOS CocoaPods詳解之制作篇

    IOS CocoaPods詳解之制作篇

    學(xué)會使用別人的Pods依賴庫以后,你一定對創(chuàng)建自己的依賴庫躍躍欲試,今天就來揭開Pods依賴庫創(chuàng)建過程的神秘面紗
    2016-09-09
  • Xcode中iOS應(yīng)用開發(fā)的一般項目目錄結(jié)構(gòu)和流程簡介

    Xcode中iOS應(yīng)用開發(fā)的一般項目目錄結(jié)構(gòu)和流程簡介

    這篇文章主要介紹了Xcode中iOS應(yīng)用開發(fā)的一般項目目錄結(jié)構(gòu)和流程簡介,包括項目所需的一些平臺路徑如模擬器路徑等的介紹,需要的朋友可以參考下
    2016-02-02
  • IOS開發(fā)之路--C語言構(gòu)造類型

    IOS開發(fā)之路--C語言構(gòu)造類型

    在第一節(jié)中我們就提到C語言的構(gòu)造類型,分為:數(shù)組、結(jié)構(gòu)體、枚舉、共用體,當(dāng)然前面數(shù)組的內(nèi)容已經(jīng)說了很多了,這一節(jié)將會重點說一下其他三種類型。
    2014-08-08

最新評論

西平县| 中超| 潮州市| 孟州市| 饶阳县| 哈尔滨市| 西城区| 清河县| 定兴县| 三明市| 岑巩县| 新疆| 隆德县| 寻乌县| 阳信县| 阿鲁科尔沁旗| 开江县| 渭南市| 讷河市| 舒兰市| 民县| 临海市| 晋江市| 南和县| 区。| 绥阳县| 桐城市| 临颍县| 岳普湖县| 元氏县| 贵南县| 澄迈县| 保亭| 攀枝花市| 武穴市| 建阳市| 阿坝县| 通许县| 德令哈市| 嵊州市| 威信县|