亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

swift實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器

 更新時(shí)間:2022年01月26日 09:09:34   作者:ningto.com  
這篇文章主要為大家詳細(xì)介紹了swift實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了swift實(shí)現(xiàn)簡(jiǎn)單計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下

代碼

//
// ?ViewController.swift
// ?Calculator
//
// ?Created by tutujiaw on 15/4/25.
// ?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
? ? }
}

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

相關(guān)文章

  • Swift中內(nèi)置的集合類型學(xué)習(xí)筆記

    Swift中內(nèi)置的集合類型學(xué)習(xí)筆記

    Swift中自帶數(shù)組、set、字典三大集合類型,這里將學(xué)習(xí)過(guò)程中的基礎(chǔ)的Swift中內(nèi)置的集合類型學(xué)習(xí)筆記進(jìn)行整理,需要的朋友可以參考下
    2016-06-06
  • 深入解析Swift代理模式

    深入解析Swift代理模式

    委托(代理)是一種設(shè)計(jì)模式,它允許類或結(jié)構(gòu)體將一些需要它們負(fù)責(zé)的功能交由(委托)給其他的類型。下面這篇文章主要介紹了Swift代理模式的相關(guān)資料,文章開(kāi)始先介紹了Objective-C相關(guān)的內(nèi)容,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-03-03
  • Swift如何為網(wǎng)頁(yè)承載頁(yè)面添加更多功能詳解

    Swift如何為網(wǎng)頁(yè)承載頁(yè)面添加更多功能詳解

    這篇文章主要給大家介紹了關(guān)于Swift如何為網(wǎng)頁(yè)承載頁(yè)面添加更多功能的相關(guān)資料,包括添加菊花加載的效果、添加跳轉(zhuǎn)到Safari的功能、添加復(fù)制鏈接的功能以及添加分享網(wǎng)頁(yè)的功能,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-05-05
  • Switch語(yǔ)句的技巧

    Switch語(yǔ)句的技巧

    switch語(yǔ)句對(duì)一個(gè)表達(dá)式求值,將結(jié)果與 case 子語(yǔ)句比較,如果匹配,則從 case 處的語(yǔ)句向下執(zhí)行,本文給大家介紹Switch語(yǔ)句的技巧,需要的朋友參考下吧
    2016-02-02
  • SwiftUI 登錄界面布局實(shí)現(xiàn)示例詳解

    SwiftUI 登錄界面布局實(shí)現(xiàn)示例詳解

    這篇文章主要為大家介紹了SwiftUI 登錄界面布局實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • Swift讀取App的版本信息與PCH文件詳解

    Swift讀取App的版本信息與PCH文件詳解

    這篇文章主要介紹了Swift讀取App的版本信息與PCH文件的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-03-03
  • iOS Swift UICollectionView橫向分頁(yè)滾動(dòng),cell左右排版問(wèn)題詳解

    iOS Swift UICollectionView橫向分頁(yè)滾動(dòng),cell左右排版問(wèn)題詳解

    UICollectionView是iOS中比較常見(jiàn)的一個(gè)控件,這篇文章主要給大家介紹了關(guān)于iOS Swift UICollectionView橫向分頁(yè)滾動(dòng),cell左右排版問(wèn)題的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-12-12
  • 簡(jiǎn)單了解Swift語(yǔ)言中的break和continue語(yǔ)句的用法

    簡(jiǎn)單了解Swift語(yǔ)言中的break和continue語(yǔ)句的用法

    這篇文章主要簡(jiǎn)單介紹了Swift語(yǔ)言中的break和continue語(yǔ)句的用法,與其他語(yǔ)言的一樣用于循環(huán)語(yǔ)句流程控制,需要的朋友可以參考下
    2015-11-11
  • Swift教程之繼承詳解

    Swift教程之繼承詳解

    這篇文章主要介紹了Swift教程之繼承詳解,一個(gè)類可以從另外一個(gè)類中繼承方法,屬性或者其它的一些特性,當(dāng)一個(gè)類繼承于另外一個(gè)類時(shí),這個(gè)繼承的類叫子類,被繼承的類叫父類,需要的朋友可以參考下
    2015-01-01
  • swift4更新中所遇到的一些問(wèn)題總結(jié)

    swift4更新中所遇到的一些問(wèn)題總結(jié)

    這篇文章主要給大家介紹了關(guān)于在swift4更新中所遇到的一些問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-12-12

最新評(píng)論