日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達者為師

網(wǎng)站首頁 編程語言 正文

Swift實現(xiàn)簡易計算器功能_Swift

作者:文恒 ? 更新時間: 2022-04-07 編程語言

用Swift寫一個簡單計算器的Demo,供大家參考,具體內(nèi)容如下

實驗環(huán)境:

Xcode v6.4 ? & OS X Yosemite 10.10

功能描述:

1、實現(xiàn)加減乘除+根號(結(jié)果display為Double型)
2、邊界適應(yīng):各元素之間的距離固定,且適應(yīng)手機旋轉(zhuǎn)(Roate)
(學(xué)習(xí)過程,根據(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
? ? //用戶是否已經(jīng)輸入數(shù)字,由于Swift的變量必須負初始值,所以設(shè)為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算法極為簡潔,當(dāng)調(diào)用方法performOperation時,其自動對比方法的參數(shù),而無需在
? ? ? ? ?*調(diào)用方法時寫明參數(shù)類型,例如,這里的參數(shù)$0 與 $1并沒有指明類型,而Swift會直接將其適應(yīng)為
? ? ? ? ?*方法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,則將相應(yīng)數(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
? ? ? ? }
? ? }
}

注意:

這里容易忽略的是,各元素之間的距離還有元素與邊界的距離,設(shè)置好后如下:

原文鏈接:https://blog.csdn.net/u014491743/article/details/51100312

欄目分類
最近更新