計(jì)算器所需求功能
1. 加減乘除
2. 開平方
3. 開次方
4. 能進(jìn)行正負(fù)轉(zhuǎn)換
計(jì)算器所用模式:MVC
計(jì)算器所用語言:Swift
計(jì)算器所參考課程:斯坦福
第一步:構(gòu)造計(jì)算器界面

image.png
第二步:連接事件
顯示器Lable連接Outlet
@IBOutlet weak var display: UILabel!
var userIsInTheMiddleOfTyping = false
數(shù)字按鈕Botton連接第一個(gè)Action
@IBAction func touchDight(_ sender: UIButton) {
let dight = sender.currentTitle!
if userIsInTheMiddleOfTyping {
let textCurrentlyInDisplay = display.text!
display.text = textCurrentlyInDisplay + dight
} else{
display.text = dight
userIsInTheMiddleOfTyping = true
}
}
var displayValue: Double{
get {
return Double(display.text!)!
}
set{
display.text = String(newValue)
}
}
運(yùn)算符號(hào)Botton連接第二個(gè)Action
@IBAction func performOperation(_ sender: UIButton) {
if userIsInTheMiddleOfTyping{
brain.setOperand(displayValue)
userIsInTheMiddleOfTyping = false
}
if let mathematicalSymbol = sender.currentTitle{
brain.performOperation(mathematicalSymbol)
}
if let result = brain.result{
displayValue = result
}
}
AC清屏按鈕Botton連接第三個(gè)Action
@IBAction func AC(_ sender: UIButton) {
display.text = “ “;
}
private var brain = CalculatorBrain()
第三步:寫計(jì)算器的運(yùn)算部分
定義一個(gè)枚舉
private enum Operation{
case constant(Double)
case unaryOperation((Double) -> Double)
case binaryOperation((Double,Double) -> Double)
case equals
}
聲明一個(gè)字典
private var operations:
Dictionary<String,Operation> = [
"V" : Operation.unaryOperation({sqrt($0)}),
"?" : Operation.unaryOperation({pow($0, 1/3)}),
"±" : Operation.unaryOperation({ -$0 }),
"+" : Operation.binaryOperation({ $0 + $1 }),
"-" : Operation.binaryOperation({ $0 - $1 }),
"x" : Operation.binaryOperation({ $0 * $1 }),
"/" : Operation.binaryOperation({ $0 / $1 }),
"=" : Operation.equals
]
- Swift中編譯器自動(dòng)給參數(shù)定義了一個(gè)參數(shù)$0,第二個(gè)參數(shù)$1
計(jì)算器需要進(jìn)行二元運(yùn)算
let function: (Double,Double) -> Double
let firstOperand: Double
func perform(with secondOperand: Double) -> Double{
return function(firstOperand , secondOperand )
}
}
計(jì)算器邏輯
mutating func performOperation(_ symbol: String){
if let operation = operations[symbol] {
switch operation{
case.constant(let Value):
accumulator = Value
case.unaryOperation(let function) :
if accumulator != nil{
accumulator = function(accumulator!)
}
case.binaryOperation(let function):
if accumulator != nil {
pbo = PendingBinaryOperation(function: function ,firstOperand: accumulator!)
accumulator = nil
}
case.equals:
performPendingBinaryOperation()
}
}
}
private mutating func performPendingBinaryOperation(){
if pbo != nil && accumulator != nil {
accumulator = pbo!.perform(with: accumulator!)
pbo = nil
}
}
private var pbo: PendingBinaryOperation?
第四步:?jiǎn)?dòng)頁

image.png
第五步:應(yīng)用圖標(biāo)
制作不同像素的圖片進(jìn)行添加

image.png
代碼部分
ViewController.swift
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var display: UILabel!
var userIsInTheMiddleOfTyping = false
@IBAction func touchDight(_ sender: UIButton) {
let dight = sender.currentTitle!
if userIsInTheMiddleOfTyping {
let textCurrentlyInDisplay = display.text!
display.text = textCurrentlyInDisplay + dight
} else{
display.text = dight
userIsInTheMiddleOfTyping = true
}
}
var displayValue: Double{
get {
return Double(display.text!)!
}
set{
display.text = String(newValue)
}
}
@IBAction func AC(_ sender: UIButton) {
display.text = “ “;
}
private var brain = CalculatorBrain()
@IBAction func performOperation(_ sender: UIButton) {
if userIsInTheMiddleOfTyping{
brain.setOperand(displayValue)
userIsInTheMiddleOfTyping = false
}
if let mathematicalSymbol = sender.currentTitle{
brain.performOperation(mathematicalSymbol)
}
if let result = brain.result{
displayValue = result
}
}
}
CalculatorBrain.swift
import Foundation
struct CalculatorBrain {
private var accumulator: Double?
private enum Operation{
case constant(Double)
case unaryOperation((Double) -> Double)
case binaryOperation((Double,Double) -> Double)
case equals
}
private var operations: Dictionary<String,Operation> = [
"V" : Operation.unaryOperation({sqrt($0)}),
"?" : Operation.unaryOperation({pow($0, 1/3)}),
"±" : Operation.unaryOperation({ -$0 }),
"+" : Operation.binaryOperation({ $0 + $1 }),
"-" : Operation.binaryOperation({ $0 - $1 }),
"x" : Operation.binaryOperation({ $0 * $1 }),
"/" : Operation.binaryOperation({ $0 / $1 }),
"=" : Operation.equals
]
mutating func performOperation(_ symbol: String){
if let operation = operations[symbol] {
switch operation{
case.constant(let Value):
accumulator = Value
case.unaryOperation(let function) :
if accumulator != nil{
accumulator = function(accumulator!)
}
case.binaryOperation(let function):
if accumulator != nil {
pbo = PendingBinaryOperation(function: function ,firstOperand: accumulator!)
accumulator = nil
}
case.equals:
performPendingBinaryOperation()
}
}
}
private mutating func performPendingBinaryOperation(){
if pbo != nil && accumulator != nil {
accumulator = pbo!.perform(with: accumulator!)
pbo = nil
}
}
private var pbo: PendingBinaryOperation?
private struct PendingBinaryOperation{
let function: (Double,Double) -> Double
let firstOperand: Double
func perform(with secondOperand: Double) -> Double{
return function(firstOperand , secondOperand )
}
}
mutating func setOperand(_ operand: Double){
accumulator = operand
}
var result: Double? {
get {
return accumulator
}
}
}