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

swift實現簡單的計算器

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

本文實例為大家分享了swift實現簡單計算器的具體代碼,供大家參考,具體內容如下

代碼

//
// ?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
? ? }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Swift中內置的集合類型學習筆記

    Swift中內置的集合類型學習筆記

    Swift中自帶數組、set、字典三大集合類型,這里將學習過程中的基礎的Swift中內置的集合類型學習筆記進行整理,需要的朋友可以參考下
    2016-06-06
  • 深入解析Swift代理模式

    深入解析Swift代理模式

    委托(代理)是一種設計模式,它允許類或結構體將一些需要它們負責的功能交由(委托)給其他的類型。下面這篇文章主要介紹了Swift代理模式的相關資料,文章開始先介紹了Objective-C相關的內容,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-03-03
  • Swift如何為網頁承載頁面添加更多功能詳解

    Swift如何為網頁承載頁面添加更多功能詳解

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

    Switch語句的技巧

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

    SwiftUI 登錄界面布局實現示例詳解

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

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

    這篇文章主要介紹了Swift讀取App的版本信息與PCH文件的相關資料,文中通過圖文介紹的非常詳細,對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。
    2017-03-03
  • iOS Swift UICollectionView橫向分頁滾動,cell左右排版問題詳解

    iOS Swift UICollectionView橫向分頁滾動,cell左右排版問題詳解

    UICollectionView是iOS中比較常見的一個控件,這篇文章主要給大家介紹了關于iOS Swift UICollectionView橫向分頁滾動,cell左右排版問題的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨小編來一起學習學習吧。
    2017-12-12
  • 簡單了解Swift語言中的break和continue語句的用法

    簡單了解Swift語言中的break和continue語句的用法

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

    Swift教程之繼承詳解

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

    swift4更新中所遇到的一些問題總結

    這篇文章主要給大家介紹了關于在swift4更新中所遇到的一些問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。
    2017-12-12

最新評論