在進(jìn)行了第一課Logistics,iOS8 Overview.和第二課More Xcode and Swift,MVC.兩課的學(xué)習(xí)后有了第一次的課堂作業(yè),要求完善計算器DEMO的功能.
基本要求
- 實現(xiàn)自動布局
- 實現(xiàn)小數(shù)點"."的按鍵功能
- 實現(xiàn)"sin","cos","π"的運算
- 創(chuàng)建歷史記錄框
- 添加清除按鈕"C"
額外要求
- 實現(xiàn)退回鍵功能
- 添加等號"="
- 添加正負(fù)轉(zhuǎn)換按鈕"+/-"
- 優(yōu)化界面
2015年11月30日
實現(xiàn)了"sin","cos","π"的運算
- "sin","cos"可直接通過之前"√"運算的方法實現(xiàn)
- "π"的運算方法則需改寫下preformOperation方法來實現(xiàn),具體見下面preformOperation3的方法,和case "π" :performOperation3(M_PI)
@IBAction func operate(sender: UIButton) { //創(chuàng)建運算符操作方法
let operation = sender.currentTitle! //識別運算符
if userIsInTheMiddleOfTypingANumber { //可省略一次enter的點擊操作,如不寫這段代碼則是"6 enter 6 enter X"最后輸出36,有這段則是"6 enter 6 X"
enter()
}
switch operation { //利用switch語句實現(xiàn)運算符的操作
case "×" :performOperation {$0 * $1} //利用閉包簡化代碼
case "÷" :performOperation {$1 / $0} //因為$1為倒數(shù)第二個數(shù),$0為最后一個數(shù),故除法和減法要用$1 /- $0 而乘法和加法則不必考慮順序
case "+" :performOperation {$0 + $1}
case "?" :performOperation {$1 - $0}
case "√" :performOperation2 {sqrt($0)}
case "sin" :performOperation2 {sin($0)}
case "cos" :performOperation2 {cos($0)}
case "π" :performOperation3(M_PI)
default: break
}
}
func performOperation(operation: (Double,Double) -> Double) {
if operandStack.count >= 2 {
displayValue = operation(operandStack.removeLast(), operandStack.removeLast()) //獲取了倒數(shù)第一個和倒數(shù)第二個數(shù),并使其出棧
enter()
}
}
func performOperation2 (operation: Double -> Double) {
if operandStack.count >= 1 {
displayValue = operation(operandStack.removeLast())
enter()
}
}
func performOperation3(operation: Double) {
displayValue = operation
enter()
}
2015年12月2日
實現(xiàn)了"C"清除按鈕的功能,由于其他功能已暫時的學(xué)習(xí)程度不能完美解決,故先繼續(xù)進(jìn)行下一步的學(xué)習(xí),之后再實現(xiàn)未完成的功能
- 先拖一個button,創(chuàng)建Action事件
- 解決思路為:1.清空運算棧. 2.使display:Label復(fù)原 3.設(shè)置用戶是否在輸入為false
@IBAction func clearButton(sender: UIButton) {
operandStack.removeAll()
display.text = "0"
userIsInTheMiddleOfTypingANumber = false
}
2015年12月3日
實現(xiàn)"←"刪除按鈕的功能
- 解決思路:先判斷用戶是否已經(jīng)輸入(display.text?.characters.count > 0 or == 0),如果大于零,則去掉最后一位(display.text = String((display.text!).characters.dropLast())),如果未輸入則默認(rèn)數(shù)字為0(display.text = "0"),最后讓userIsInTheMiddleOfTypingANumber = false
//實現(xiàn)刪除按鈕
@IBAction func backSpace(sender: UIButton) {
if display.text?.characters.count > 0 {
display.text = String((display.text!).characters.dropLast())
}
if display.text?.characters.count == 0 {
display.text = "0"
userIsInTheMiddleOfTypingANumber = false
}
}
實現(xiàn)"+/?"正負(fù)按鈕轉(zhuǎn)換功能
- 解決思路:
1.此功能不僅要在label上顯示,并將其入棧,而且要作為運算方法,實現(xiàn)棧中的正負(fù)號轉(zhuǎn)換.
2.實現(xiàn)label中的現(xiàn)實可通過創(chuàng)建方法實現(xiàn),先判斷是否有"-"負(fù)號,如沒有,則在前面添加,如有則刪除.
if display.text!.hasPrefix("?") {
display.text?.removeAtIndex(display.text!.startIndex)
} else {
display.text = "?" + display.text!
}
3.實現(xiàn)棧中的正負(fù)號改變則需增加運算方法,在func operate()中先判斷是否點擊了"+/?"button,如點擊,實現(xiàn)changeDisplaySign(),再在switch中加入case "+/?" :performOperation2 { -$0 }
代碼如下:
@IBAction func operate(sender: UIButton) { //創(chuàng)建運算符操作方法
let operation = sender.currentTitle! //識別運算符
if userIsInTheMiddleOfTypingANumber { //可省略一次enter的點擊操作,如不寫這段代碼則是"6 enter 6 enter X"最后輸出36,有這段則是"6 enter 6 X"
if operation == "+/?" {
changeDisplatSign()
return
}
enter()
}
switch operation { //利用switch語句實現(xiàn)運算符的操作
case "×" :performOperation {$0 * $1} //利用閉包簡化代碼
case "÷" :performOperation {$1 / $0} //因為$1為倒數(shù)第二個數(shù),$0為最后一個數(shù),故除法和減法要用$1 /- $0 而乘法和加法則不必考慮順序
case "+" :performOperation {$0 + $1}
case "?" :performOperation {$1 - $0}
case "√" :performOperation2 {sqrt($0)}
case "sin" :performOperation2 {sin($0)}
case "cos" :performOperation2 {cos($0)}
case "π" :performOperation3(M_PI)
case "+/?" :performOperation2 { -$0 }
default: break
}
}
func performOperation2 (operation: Double -> Double) {
if operandStack.count >= 1 {
displayValue = operation(operandStack.removeLast())
enter()
}
}
//實現(xiàn)正負(fù)轉(zhuǎn)換按鈕功能
func changeDisplatSign() {
if display.text!.hasPrefix("?") {
display.text?.removeAtIndex(display.text!.startIndex)
} else {
display.text = "?" + display.text!
}
}