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

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

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

本文實(shí)例為大家分享了swift實(shí)現(xià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
? ? }
}

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

相關(guān)文章

  • swift?cell自定義左滑手勢處理方法

    swift?cell自定義左滑手勢處理方法

    這篇文章主要介紹了swift?cell自定義左滑手勢處理,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-12-12
  • Swift3.0剪切板代碼拷貝及跨應(yīng)用粘貼實(shí)現(xiàn)代碼

    Swift3.0剪切板代碼拷貝及跨應(yīng)用粘貼實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了Swift3.0剪切板代碼拷貝及跨應(yīng)用粘貼的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • 在Mac OS的終端中運(yùn)行Swift應(yīng)用的方法

    在Mac OS的終端中運(yùn)行Swift應(yīng)用的方法

    這篇文章主要介紹了在Mac OS的終端中運(yùn)行Swift應(yīng)用的方法,依靠Xcode的REPL功能來實(shí)現(xiàn),需要的朋友可以參考下
    2015-07-07
  • Swift繼承Inheritance淺析介紹

    Swift繼承Inheritance淺析介紹

    繼承我們可以理解為一個(gè)類獲取了另外一個(gè)類的方法和屬性。當(dāng)一個(gè)類繼承其它類時(shí),繼承類叫子類,被繼承類叫超類(或父類),在Swift中,類可以調(diào)用和訪問超類的方法,屬性和下標(biāo)腳本,并且可以重寫它們。我們也可以為類中繼承來的屬性添加屬性觀察器
    2022-08-08
  • Swift心得筆記之函數(shù)

    Swift心得筆記之函數(shù)

    函數(shù)是執(zhí)行特定任務(wù)的代碼自包含塊。通過給定一個(gè)函數(shù)名稱標(biāo)識(shí)它是什么,并在需要的時(shí)候使用該名稱來調(diào)用函數(shù)以執(zhí)行任務(wù)。今天我們就來探討下swift中的函數(shù)問題。
    2015-04-04
  • Swift中實(shí)現(xiàn)點(diǎn)擊、雙擊、捏、旋轉(zhuǎn)、拖動(dòng)、劃動(dòng)、長按手勢的類和方法介紹

    Swift中實(shí)現(xiàn)點(diǎn)擊、雙擊、捏、旋轉(zhuǎn)、拖動(dòng)、劃動(dòng)、長按手勢的類和方法介紹

    這篇文章主要介紹了Swift中實(shí)現(xiàn)點(diǎn)擊、雙擊、捏、旋轉(zhuǎn)、拖動(dòng)、劃動(dòng)、長按手勢的類和方法介紹,本文分別給出了各種手勢的實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2015-01-01
  • RxSwift學(xué)習(xí)教程之基礎(chǔ)篇

    RxSwift學(xué)習(xí)教程之基礎(chǔ)篇

    RxSwift是Swift函數(shù)響應(yīng)式編程的一個(gè)開源庫,由Github的ReactiveX組織開發(fā),維護(hù)。下面這篇文章主要給大家介紹了關(guān)于RxSwift學(xué)習(xí)之基礎(chǔ)篇的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-09-09
  • Swift的開發(fā)環(huán)境搭建以及基本語法詳解

    Swift的開發(fā)環(huán)境搭建以及基本語法詳解

    這篇文章主要介紹了Swift的開發(fā)環(huán)境搭建以及基本語法詳解,是Swift入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-11-11
  • Swift教程之控制流詳解

    Swift教程之控制流詳解

    這篇文章主要介紹了Swift教程之控制流詳解,本文詳細(xì)講解了Swift中的for循環(huán)、for-in循環(huán)、For-Condition-Increment條件循環(huán)、while循環(huán)、Do-while循環(huán)、if條件語句等控制流語句,需要的朋友可以參考下
    2015-01-01
  • Swift 4中一些實(shí)用的數(shù)組技巧小結(jié)

    Swift 4中一些實(shí)用的數(shù)組技巧小結(jié)

    這篇文章主要給大家分享了關(guān)于Swift 4中一些實(shí)用的數(shù)組技巧,文中通過示例代碼介紹的介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用swift具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-03-03

最新評(píng)論

景德镇市| 都安| 木兰县| 平南县| 大安市| 固镇县| 噶尔县| 会理县| 汶川县| 武夷山市| 兰考县| 曲阜市| 错那县| 许昌县| 万安县| 舒城县| 恭城| 饶河县| 青龙| 常宁市| 文化| 郴州市| 吴旗县| 徐汇区| 思茅市| 南开区| 林甸县| 扶余县| 吐鲁番市| 政和县| 淮滨县| 长沙县| 中方县| 新邵县| 舟山市| 辉县市| 安义县| 介休市| 体育| 兴文县| 屯门区|