Objective
此實作參考 [1],而試著將 Objective-C 程式 Port 到 Swift 3.0 程式
Lab
1. 新增 Single View Application 專案
2. UI 設計
Code
1. File Calculation.swift
//
// Calculation.swift
// myCaculatorDemo
//
// Created by Elvis Meng on 2017/5/4.
// Copyright © 2017年 Elvis Meng. All rights reserved.
//
import Foundation
class Calculation : NSObject {
var operandA : Float?
var operandB : Float?
var op : Character?
var isFirstOperand : Bool
var result : Float?
override init() {
operandA = 0.0
operandB = 0.0
op = nil
isFirstOperand = true
result = 0.0
}
func calculateResult(operandA : Float, operandB : Float, op : Character) -> Float {
switch op {
case "+" :
result = operandA + operandB
case "-" :
result = operandA - operandB
case "*" :
result = operandA * operandB
case "/" :
result = operandA / operandB
default :
print("error")
}
return result!
}
}
2. File : ViewController.swift
//
// ViewController.swift
// myCaculatorDemo
//
// Created by Elvis Meng on 2017/4/25.
// Copyright © 2017年 Elvis Meng. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var displayResult: UILabel!
var cal:Calculation = Calculation()
var isFirstDigit = true
var hasTapEqual = false
var operand:Float = 0.0
var result:Float = 0.0
var digitCount = 0
var digit = 0
func processCalc(op:Character){
if (cal.isFirstOperand){
cal.operandA = operand
cal.isFirstOperand = false
}
else if (hasTapEqual){
cal.operandA = result
cal.operandB = operand
hasTapEqual = false
}
else {
cal.operandB = operand
result = cal.calculateResult(operandA: cal.operandA!, operandB: cal.operandB!, op: op)
displayProcess(num: result)
cal.operandA = result
}
cal.op = op
operand = 0.0
digitCount = 0
}
@IBAction func tapDigit(_ sender: UIButton) {
digit = sender.tag
if hasTapEqual == true {
displayResult.text = "0.0"
cal.operandA = 0
cal.operandB = 0
cal.isFirstOperand = true
result = 0
operand = 0
isFirstDigit = true
hasTapEqual = false
}
if (isFirstDigit && digit == 0){
isFirstDigit = true
} else {
if (digitCount >= 15) {
return
}
isFirstDigit = false
operand = operand * 10 + Float(digit)
displayProcess(num: operand)
}
digitCount += 1
}
@IBAction func tapPlus(_ sender: UIButton) {
processCalc(op: "+")
}
@IBAction func tapMinus(_ sender: UIButton) {
processCalc(op: "-")
}
@IBAction func tapMultiply(_ sender: UIButton) {
processCalc(op: "*")
}
@IBAction func tapDivide(_ sender: UIButton) {
processCalc(op: "/")
}
@IBAction func tapEqual(_ sender: UIButton) {
if (cal.isFirstOperand == false){
cal.operandB = operand
result = cal.calculateResult(operandA: cal.operandA!, operandB: cal.operandB!, op: cal.op!)
self.displayProcess(num: result)
cal.operandA = result
hasTapEqual = true
}
}
@IBAction func tapAC(_ sender: UIButton) {
displayResult.text = "0.0"
cal.operandA = 0
cal.operandB = 0
cal.isFirstOperand = true
result = 0
operand = 0
isFirstDigit = true
hasTapEqual = false
}
func displayProcess(num:Float){
displayResult.text = String(num)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let cal:Calculation = Calculation()
cal.isFirstOperand = true
isFirstDigit = true
hasTapEqual = false
displayResult.text = String(0.0)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Test
Reference
1. Chapter 24, 範例程式五:簡易計算器實作, P24-27, “學會Objective-C 的24堂課“, 蔡明志
/end
沒有留言:
張貼留言