網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了iOS實現(xiàn)計算器小功能,供大家參考,具體內(nèi)容如下
本文利用ios實現(xiàn)計算器app,后期將用mvc結(jié)構(gòu)重構(gòu)
import UIKit class CalculViewController: UIViewController { ? ? @IBOutlet weak var display: UILabel! ? ? var userIsInTheMiddleOFTypingANumber:Bool=false ? ? @IBAction func appendDigit(sender: UIButton) { ? ? ? ? let digit=sender.currentTitle! ? ? ? ? if userIsInTheMiddleOFTypingANumber { ? ? ? ? display.text=display.text!+digit ? ? ? ? }else{ ? ? ? ? ? ? display.text=digit ? ? ? ? ? ? userIsInTheMiddleOFTypingANumber=true ? ? ? ? } ? ? } ? ? var operandstack:Array<Double>=Array<Double>() ? ? @IBAction func operate(sender: UIButton) { ? ? ? ? let operation=sender.currentTitle!; ? ? ? ? if userIsInTheMiddleOFTypingANumber { ? ? ? ? ? ? enter() ? ? ? ? } ? ? ? ? switch operation { ? ? ? ? case "×":performeOperation{$0*$1} ? ? ? ? case "÷":performeOperation{$1/$0} ? ? ? ? case "+":performeOperation{$0+$1} ? ? ? ? case "-":performeOperation{$1-$0} ? ? ? ? case "√":performeOperation{sqrt($0)} ? ? ? ? default: ? ? ? ? ? ? break ? ? ? ? } ? ? } // ? ?func multiply(op1:Double,op2:Double) -> Double { // ? ? ? ?return op1*op2; // ? ?} ? ? func performeOperation(operation:(Double,Double)->Double){ ? ? ? ? if operandstack.count>=2 { ? ? ? ? ? ? displayValue=operation(operandstack.removeLast(),operandstack.removeLast()) ? ? ? ? ? ? enter() ? ? ? ? } ? ? } ? ? ?private func performeOperation(operation:Double->Double){ ? ? ? ? if operandstack.count>=1 { ? ? ? ? ? ? displayValue=operation(operandstack.removeLast()) ? ? ? ? ? ? enter() ? ? ? ? } ? ? } ? ? @IBAction func enter() { ? ? ? ? userIsInTheMiddleOFTypingANumber=false ? ? ? ? operandstack.append(displayValue) ? ? ? ? print("operandstack=\(operandstack)") ? ? } ? ? var displayValue:Double{ ? ? ? ? get{ ? ? ? ? ? ? return NSNumberFormatter().numberFromString(display.text!)!.doubleValue ? ? ? ? } ? ? ? ? set{ ? ? ? ? ? ? display.text="\(newValue)" ? ? ? ? ? ? userIsInTheMiddleOFTypingANumber=false ? ? ? ? } ? ? }
知識點如下
計算型屬性的setter與getter
swift利用函數(shù)作為參數(shù)
swift的重載,詳情參見:swift override
效果如下
增加model文件
import Foundation class CalculatorBrain { ? ? private enum Op : CustomStringConvertible{ ? ? ? ? case operand(Double) ? ? ? ? case UnaryOperation(String,Double->Double) ? ? ? ? case BinaryOperation(String,(Double,Double)->Double) ? ? ? ? var description:String{ ? ? ? ? ? ? get{ ? ? ? ? ? ? ? ? switch self { ? ? ? ? ? ? ? ? case .operand(let operand): ? ? ? ? ? ? ? ? ? ? return "\(operand)" ? ? ? ? ? ? ? ? case .BinaryOperation(let symbol,_): ? ? ? ? ? ? ? ? ? ? return symbol ? ? ? ? ? ? ? ? case .UnaryOperation(let symbol, _): ? ? ? ? ? ? ? ? ? ? return symbol ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? private var opstack=[Op]() ? ? private var knowOps=[String:Op]() ? ? init(){ ? ? ? ? func learnOp(op:Op){ ? ? ? ? ? ? knowOps[op.description]=op ? ? ? ? } ? ? ? ? learnOp(Op.BinaryOperation("×"){$0*$1}) ? ? ? ? learnOp(Op.BinaryOperation("÷"){$1/$0}) ? ? ? ? learnOp(Op.BinaryOperation("+"){$0+$1}) ? ? ? ? learnOp(Op.BinaryOperation("-"){$1-$0}) ? ? ? ? learnOp(Op.UnaryOperation("√"){sqrt($0)}) // ? ? ? ?knowOps["×"]=Op.BinaryOperation("×"){$0*$1} // ? ? ? ?knowOps["÷"]=Op.BinaryOperation("÷"){$1/$0} // ? ? ? ?knowOps["+"]=Op.BinaryOperation("+"){$0+$1} // ? ? ? ?knowOps["-"]=Op.BinaryOperation("-"){$1-$0} // ? ? ? ?knowOps["√"]=Op.UnaryOperation("√"){sqrt($0)} ? ? } ? ? private func evaluate(ops:[Op])->(result:Double?,remainOps:[Op]){ ? ? ? ? if !ops.isEmpty { ? ? ? ? ? ? var remainOps=ops; ? ? ? ? ? ? let op=remainOps.removeLast() ? ? ? ? ? ? switch op { ? ? ? ? ? ? case Op.operand(let operand): ? ? ? ? ? ? ? ? return(operand,remainOps) ? ? ? ? ? ? case Op.UnaryOperation(_, let operation): ? ? ? ? ? ? ? ? let operandEvalution=evaluate(remainOps) ? ? ? ? ? ? ? ? if let operand=operandEvalution.result{ ? ? ? ? ? ? ? ? ? ? return(operation(operand),operandEvalution.remainOps) ? ? ? ? ? ? ? ? } ? ? ? ? ? ? case Op.BinaryOperation(_, let operation): ? ? ? ? ? ? ? ? let operandEvlution1=evaluate(remainOps) ? ? ? ? ? ? ? ? if let operand1=operandEvlution1.result { ? ? ? ? ? ? ? ? ? ? let operandEvlution2=evaluate(operandEvlution1.remainOps) ? ? ? ? ? ? ? ? ? ? if let operand2=operandEvlution2.result { ? ? ? ? ? ? ? ? ? ? ? ? return (operation(operand1,operand2),operandEvlution2.remainOps) ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return (nil,ops) ? ? } ? ? func evaluate()->Double?{ ? ? ? ? let (result,remainder)=evaluate(opstack) ? ? ? ? print("\(opstack)=\(result)with\(remainder)left over") ? ? ? ? return result ? ? } ? ? func pushOperand(operand:Double)->Double?{ ? ? ? ? opstack.append(Op.operand(operand)) ? ? ? ? return evaluate() ? ? } ? ? func performOperation(symbol:String)->Double?{ ? ? ? ? if let operation=knowOps[symbol]{ ? ? ? ? ? ? opstack.append(operation) ? ? ? ? } ? ? ? ? return evaluate() ? ? } }
controll修改為
import UIKit class CalculViewController: UIViewController { ? ? @IBOutlet weak var display: UILabel! ? ? var userIsInTheMiddleOFTypingANumber:Bool=false ? ? var brain=CalculatorBrain() ? ? @IBAction func appendDigit(sender: UIButton) { ? ? ? ? let digit=sender.currentTitle! ? ? ? ? if userIsInTheMiddleOFTypingANumber { ? ? ? ? display.text=display.text!+digit ? ? ? ? }else{ ? ? ? ? ? ? display.text=digit ? ? ? ? ? ? userIsInTheMiddleOFTypingANumber=true ? ? ? ? } ? ? } ? ? //var operandstack:Array<Double>=Array<Double>() ? ? @IBAction func operate(sender: UIButton) { ? ? ? ? if userIsInTheMiddleOFTypingANumber { ? ? ? ? ? ? enter() ? ? ? ? } ? ? ? ? if let operation=sender.currentTitle{ ? ? ? ? ? ? if let result=brain.performOperation(operation) { ? ? ? ? ? ? ? ? displayValue=result ? ? ? ? ? ? }else{ ? ? ? ? ? ? ? ? displayValue=0 ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? @IBAction func enter() { ? ? ? ? userIsInTheMiddleOFTypingANumber=false ? ? ? ? if let result=brain.pushOperand(displayValue){ ? ? ? ? ? ? displayValue=result ? ? ? ? }else{ ? ? ? ? ? ? displayValue=0 ? ? ? ? } ? ? } ? ? var displayValue:Double{ ? ? ? ? get{ ? ? ? ? ? ? return NSNumberFormatter().numberFromString(display.text!)!.doubleValue ? ? ? ? } ? ? ? ? set{ ? ? ? ? ? ? display.text="\(newValue)" ? ? ? ? ? ? userIsInTheMiddleOFTypingANumber=false ? ? ? ? } ? ? } }
原文鏈接:https://blog.csdn.net/whuhan2013/article/details/52842191
相關(guān)推薦
- 2022-07-30 Linux常見命令-壓縮和解壓類 一、gzip/gunzip 壓縮 二、zip/unzip 壓縮 三
- 2022-07-15 SQL?Server中的排名函數(shù)與分析函數(shù)詳解_MsSql
- 2023-11-19 Linux虛擬機VMware的Ubuntu使用vi指令的方向鍵和backspace空格鍵亂碼
- 2022-12-05 單步調(diào)試?step?into/step?out/step?over?區(qū)別說明_python
- 2021-12-24 SQL注入詳解及防范方法_數(shù)據(jù)庫其它
- 2022-10-30 Matlab利用遺傳算法GA求解非連續(xù)函數(shù)問題詳解_C 語言
- 2022-12-28 jquery實現(xiàn)點擊瀏覽器返回上一頁按鈕并能直接刷新_jquery
- 2022-07-09 C++深入探究用NULL來初始化空指針是否合適_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支