Swift實現(xiàn)簡易計算器功能
更新時間:2022年01月26日 08:39:17 作者:文恒
這篇文章主要為大家詳細介紹了Swift實現(xiàn)簡易計算器功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
用Swift寫一個簡單計算器的Demo,供大家參考,具體內容如下
實驗環(huán)境:
Xcode v6.4 & OS X Yosemite 10.10
功能描述:
1、實現(xiàn)加減乘除+根號(結果display為Double型)
2、邊界適應:各元素之間的距離固定,且適應手機旋轉(Roate)
(學習過程,根據(jù)Stanford的Swift課程而寫的程序)

代碼實現(xiàn):
//
// ?ViewController.swift
// ?Calculator
//
// ?Created by VincentYau on 4/7/16.
// ?Copyright (c) 2016 VincentYau. All rights reserved.
//
import UIKit
class ViewController: UIViewController
{
? ? @IBOutlet weak var display: UILabel!
? ? var userIsInTheMiddleOfTypingANumber:Bool = false
? ? //用戶是否已經輸入數(shù)字,由于Swift的變量必須負初始值,所以設為false
? ? @IBAction func appendDigit(sender: UIButton){
? ? ? ? let digit = sender.currentTitle!//直接獲取Button的數(shù)字
? ? ? ? //若已輸入過數(shù)字,則直接往display中添加數(shù)字,否則直接現(xiàn)實新點擊數(shù)字,去除原始0的操作
? ? ? ? if userIsInTheMiddleOfTypingANumber{
? ? ? ? ? ? display.text = display.text! + digit
? ? ? ? }else{
? ? ? ? ? ? display.text = digit
? ? ? ? ? ? userIsInTheMiddleOfTypingANumber = true
? ? ? ? }
? ? }
? ? //對數(shù)字進行運算
? ? @IBAction func operate(sender: UIButton) {
? ? ? ? let operation = sender.currentTitle!
? ? ? ? if userIsInTheMiddleOfTypingANumber{
? ? ? ? ? ? enter()
? ? ? ? }
? ? ? ? switch operation{
? ? ? ? /*swift算法極為簡潔,當調用方法performOperation時,其自動對比方法的參數(shù),而無需在
? ? ? ? ?*調用方法時寫明參數(shù)類型,例如,這里的參數(shù)$0 與 $1并沒有指明類型,而Swift會直接將其適應為
? ? ? ? ?*方法performOpetation中的Double型
? ? ? ? */
? ? ? ? case "×": performOperation { $0 * $1 }
? ? ? ? case "÷": performOperation { $1 / $0 }
? ? ? ? case "+": performOperation { $0 + $1 }
? ? ? ? case "?": performOperation { $1 - $0 }
? ? ? ? case "√": performOperation { sqrt($0) }
? ? ? ? default: break
? ? ? ? }
? ? }
? ? //兩個參數(shù)進行運算的方法
? ? func performOperation(operation: (Double,Double) -> Double){
? ? ? ? if operandStack.count >= 2 {
? ? ? ? ? ? displayValue = operation(operandStack.removeLast(),operandStack.removeLast())
? ? ? ? ? ? enter()
? ? ? ? }
? ? }
? ? //一個參數(shù)進行運算的方法,Swift支持方法的重載,但Obj-C不允許,這里繼承了Obj-C的
? ? //類UIViewColler,不能重載方法performOperation,故將其變?yōu)镻rivate方法
? ? private func performOperation(operation: Double -> Double){
? ? ? ? if operandStack.count >= 1 {
? ? ? ? ? ? displayValue = operation(operandStack.removeLast())
? ? ? ? ? ? enter()
? ? ? ? }
? ? }
? ? var operandStack = Array<Double>() ?
? ? //若用戶點擊enter,則將相應數(shù)字添加至數(shù)組Array中 ?
? ? @IBAction func enter() {
? ? ? ? userIsInTheMiddleOfTypingANumber = false
? ? ? ? operandStack.append(displayValue)
? ? ? ? println("operandStack = \(operandStack)")
? ? }
? ? var displayValue: Double {
? ? ? ? get{
? ? ? ? ? ? return NSNumberFormatter().numberFromString(display.text!)!.doubleValue
? ? ? ? }
? ? ? ? set{
? ? ? ? ? ? display.text = "\(newValue)"
? ? ? ? ? ? userIsInTheMiddleOfTypingANumber = false
? ? ? ? }
? ? }
}注意:
這里容易忽略的是,各元素之間的距離還有元素與邊界的距離,設置好后如下:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
舉例講解Swift編程中switch...case語句的用法
這篇文章主要介紹了Swift編程中switch...case語句的用法,其中fallthrough關鍵字在switch語句中的使用是重點,需要的朋友可以參考下2016-04-04
Swift自動調整視圖布局AutoLayout和AutoresizingMask功能詳解
這篇文章主要為大家介紹了Swift自動調整視圖布局AutoLayout和AutoresizingMask功能及使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06

