iOS实现计算器小功能的代码怎么写
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,本篇内容介绍了"iOS实现计算器小功能的代码怎么写"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!本文
千家信息网最后更新 2026年11月03日iOS实现计算器小功能的代码怎么写
本篇内容介绍了"iOS实现计算器小功能的代码怎么写"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
本文利用ios实现计算器app,后期将用mvc结构重构
import UIKitclass 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=Array() @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利用函数作为参数
swift的重载,详情参见:swift override
效果如下
增加model文件
import Foundationclass 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 UIKitclass 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=Array() @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 } }} "iOS实现计算器小功能的代码怎么写"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
计算器
知识
代码
功能
内容
更多
实用
学有所成
接下来
函数
参数
困境
实际
属性
情况
效果
文件
文章
案例
知识点
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
科技平台互联网
互联网巨头反垄断金融科技
上海互联网科技有限公司
互联网科技论文
互联网科技ppt模板
互联网女皇 腾讯科技
沃里互联网科技
上游互联网科技
互联网科技公司有哪些
太原新华互联网科技学校怎么样
隆华科技工业互联网
福州新华互联网科技学校 知乎
互联网公司科技股
哈尔滨屹滨互联网科技有限公司
想开互联网科技公司怎么样
天津腾达互联网科技有限公司
互联网安防科技感
35岁以上互联网科技行业
科技互联网行业投资研究
互联网的黑科技实验室
金融科技 互联网金融
万易互联网科技
智网互联网科技
中国互联网科技发展
北京互联网络科技有限公司
天扬科技互联网广告
昭通云南众福互联网科技有限公司
互联网科技包装股份有限公司上市
现代互联网科技公司
河南商务圈互联网科技有限公司