欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS實(shí)現(xiàn)計(jì)算器小功能

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

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

本文利用ios實(shí)現(xiàn)計(jì)算器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
? ? ? ? }
? ? }

知識(shí)點(diǎn)如下

計(jì)算型屬性的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
? ? ? ? }
? ? }


}

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

相關(guān)文章

  • Swift 2.1 為 UIView 添加點(diǎn)擊事件和點(diǎn)擊效果

    Swift 2.1 為 UIView 添加點(diǎn)擊事件和點(diǎn)擊效果

    本文主要介紹 Swift UIView,這里給大家提供代碼示例作為參考為UIView 添加點(diǎn)擊事件和點(diǎn)擊效果,希望能幫助IOS開發(fā)的同學(xué)
    2016-07-07
  • iOS多語(yǔ)言本地化流程的優(yōu)化方案

    iOS多語(yǔ)言本地化流程的優(yōu)化方案

    這篇文章主要給大家介紹了關(guān)于iOS多語(yǔ)言本地化流程的優(yōu)化的相關(guān)資料,多語(yǔ)言本地化是我們大家在開發(fā)中經(jīng)常會(huì)遇到的一個(gè)功能,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起看看吧。
    2018-01-01
  • IOS 基本文件操作實(shí)例詳解

    IOS 基本文件操作實(shí)例詳解

    這篇文章主要介紹了IOS 基本文件操作實(shí)例詳解 的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • iOS Label隨字自動(dòng)變大效果

    iOS Label隨字自動(dòng)變大效果

    這篇文章主要為大家詳細(xì)介紹了iOS Label隨字自動(dòng)變大效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • iOS實(shí)現(xiàn)UITableView數(shù)據(jù)為空時(shí)的提示頁(yè)面

    iOS實(shí)現(xiàn)UITableView數(shù)據(jù)為空時(shí)的提示頁(yè)面

    最近工作中遇到一個(gè)需求,當(dāng)UITableView數(shù)據(jù)為空的時(shí)候,給出一個(gè)簡(jiǎn)單的提示頁(yè)面,通過從網(wǎng)上查找解決的方法,發(fā)現(xiàn)了兩種實(shí)現(xiàn)的方法,現(xiàn)在分享給大家,有需要的朋友們可以參考借鑒,下面感興趣的朋友們來一起學(xué)習(xí)學(xué)習(xí)吧。
    2016-11-11
  • IOS開發(fā)QQ空間/朋友圈類界面的搭建

    IOS開發(fā)QQ空間/朋友圈類界面的搭建

    本篇文章主要介紹了iOS開發(fā)之類似朋友圈的社交界面實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-11-11
  • iOS實(shí)現(xiàn)百度地圖拖拽后更新位置以及反編碼

    iOS實(shí)現(xiàn)百度地圖拖拽后更新位置以及反編碼

    百度地圖已經(jīng)開放了地圖API,大家可以很方便的調(diào)用地圖中的相應(yīng)數(shù)據(jù),并完成各項(xiàng)個(gè)性化的展示,下面這篇文章主要給大家介紹了關(guān)于iOS如何實(shí)現(xiàn)百度地圖拖拽后更新位置以及反編碼的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-12-12
  • 2016最新CocoaPods安裝和錯(cuò)誤解決方案

    2016最新CocoaPods安裝和錯(cuò)誤解決方案

    CocoaPods是一個(gè)負(fù)責(zé)管理iOS項(xiàng)目中第三方開源庫(kù)的工具,開發(fā)iOS項(xiàng)目不可避免地要使用第三方開源庫(kù)。接下來通過本文給大家介紹2016最新CocoaPods安裝和錯(cuò)誤解決方案,需要的的朋友參考下吧
    2016-11-11
  • ARM匯編逆向iOS 實(shí)戰(zhàn)

    ARM匯編逆向iOS 實(shí)戰(zhàn)

    本文給大家通過一些簡(jiǎn)單的實(shí)戰(zhàn)來講解使用ARM匯編進(jìn)行IOS逆向工程的方法,十分的實(shí)用,有需要的小伙伴可以參考下
    2015-11-11
  • IOS 開發(fā)之ios視頻截屏的實(shí)現(xiàn)代碼

    IOS 開發(fā)之ios視頻截屏的實(shí)現(xiàn)代碼

    這篇文章主要介紹了IOS 開發(fā)之ios視頻截屏的實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下
    2017-07-07

最新評(píng)論