swift实现简单的计算器
本文实例为大家分享了swift实现简单计算器的具体代码,供大家参考,具体内容如下
文章图片
代码
////ViewController.swift//Calculator////Created by tutujiaw on 15/4/25.//Copyright (c) 2015年 tutujiaw. All rights reserved.// import UIKit class ViewController: UIViewController { @IBOutlet weak var display: UILabel!var sumInMemory: Double = 0.0var sumSoFar: Double = 0.0var factorSoFar: Double = 0.0var pendingAdditiveOperator = ""var pendingMultiplicativeOperator = ""var waitingForOperand = truevar displayValue: Double {set {let intValue = https://www.it610.com/article/Int(newValue)if (Double(intValue) == newValue) {display.text ="\(intValue)"} else {display.text = "\(newValue)"}}get {return (display.text! as NSString).doubleValue}}override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view, typically from a nib.} override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()// Dispose of any resources that can be recreated.} func calculate(rightOperand: Double, pendingOperator: String) -> Bool {var result = falseswitch pendingOperator {case "+":sumSoFar += rightOperandresult = truecase "-":sumSoFar -= rightOperandresult = truecase "*":factorSoFar *= rightOperandresult = truecase "/":if rightOperand != 0.0 {factorSoFar /= rightOperandresult = true}default:break}return result}func abortOperation() {clearAll()display.text = "####"}@IBAction func digitClicked(sender: UIButton) {let digitValue = https://www.it610.com/article/sender.currentTitle?.toInt()if display.text!.toInt() == 0 && digitValue == 0 {return}if waitingForOperand {display.text =""waitingForOperand = false}display.text = display.text! + sender.currentTitle!} @IBAction func changeSignClicked() {displayValue *= -1}@IBAction func backspaceClicked() {if waitingForOperand {return}var strValue = https://www.it610.com/article/display.text!display.text = dropLast(strValue)if display.text!.isEmpty {displayValue = 0.0waitingForOperand = true}}@IBAction func clear() {if waitingForOperand {return}displayValue = 0waitingForOperand = true}@IBAction func clearAll() {sumSoFar = 0.0factorSoFar = 0.0pendingAdditiveOperator =""pendingMultiplicativeOperator = ""displayValue = https://www.it610.com/article/0.0waitingForOperand = true}@IBAction func clearMemory() {sumInMemory = 0.0}@IBAction func readMemory() {displayValue = sumInMemorywaitingForOperand = true}@IBAction func setMemory() {equalClicked()sumInMemory = displayValue}@IBAction func addToMemory() {equalClicked()sumInMemory += displayValue}@IBAction func multiplicativeOperatorClicked(sender: UIButton) {var clickedOperator = sender.currentTitle!var operand = displayValueif !pendingMultiplicativeOperator.isEmpty {if !calculate(operand, pendingOperator: pendingMultiplicativeOperator) {abortOperation()return}displayValue = factorSoFar} else {factorSoFar = operand}pendingMultiplicativeOperator = clickedOperatorwaitingForOperand = true}@IBAction func additiveOperatorClicked(sender: UIButton) {let clickedOperator = sender.currentTitle!var operand = displayValueif !pendingMultiplicativeOperator.isEmpty {if !calculate(operand, pendingOperator: pendingMultiplicativeOperator) {abortOperation()return}displayValue = factorSoFarfactorSoFar = 0.0pendingMultiplicativeOperator =""}if !pendingAdditiveOperator.isEmpty {if !calculate(operand, pendingOperator: pendingAdditiveOperator) {abortOperation()return}displayValue = https://www.it610.com/article/sumSoFar} else {sumSoFar = operand}pendingAdditiveOperator = clickedOperatorwaitingForOperand = true}@IBAction func unaryOperatorClicked(sender: UIButton) {let clickedOperator = sender.currentTitle!var result: Double = 0if clickedOperator =="Sqrt" {if displayValue < 0 {abortOperation()return}result = sqrt(displayValue)} else if clickedOperator == "x^2" {result = pow(displayValue, 2)} else if clickedOperator == "1/x" {if displayValue =https://www.it610.com/article/= 0 {abortOperation()return}result = 1.0 / displayValue}displayValue = resultwaitingForOperand = true}@IBAction func equalClicked() {var operand = displayValueif !pendingMultiplicativeOperator.isEmpty {if !calculate(operand, pendingOperator: pendingMultiplicativeOperator) {abortOperation()return}operand = factorSoFarfactorSoFar = 0.0pendingMultiplicativeOperator =""}if !pendingAdditiveOperator.isEmpty {if !calculate(operand, pendingOperator: pendingAdditiveOperator) {abortOperation()return}pendingAdditiveOperator = ""} else {sumSoFar = operand}displayValue = https://www.it610.com/article/sumSoFarsumSoFar = 0.0waitingForOperand = true}}
【swift实现简单的计算器】以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
推荐阅读
- JS实现玩转风车
- 超简单易懂的vue组件传值
- 按照|按照 Promise/A+ 规范逐行注释并实现 Promise
- Mybatis-Plus|Mybatis-Plus 如何实现一对多关系 举例 用户与角色
- 用一个文件,实现迷你|用一个文件,实现迷你 Web 框架
- 在 2040 年前,实现净零碳排放
- go|Go继承的方法重写,继承抽象类实现
- java|java-继承,super关键字,this关键字,抽象方法和抽象类,红包案例分析实现
- pytorch|pytorch实现手写数字识别 | MNIST数据集(全连接神经网络)
- CXF代码配置实现HTTPS