swift实现简单的计算器

 更新时间:2022年01月26日 09:09:34   作者:ningto.com  
这篇文章主要为大家详细介绍了swift实现简单的计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

本文实例为大家分享了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.0
? ? var sumSoFar: Double = 0.0
? ? var factorSoFar: Double = 0.0
? ? var pendingAdditiveOperator = ""
? ? var pendingMultiplicativeOperator = ""
? ? var waitingForOperand = true
? ??
? ? var displayValue: Double {
? ? ? ? set {
? ? ? ? ? ? let intValue = 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 = false
? ? ? ? switch pendingOperator {
? ? ? ? ? ? case "+":
? ? ? ? ? ? sumSoFar += rightOperand
? ? ? ? ? ? result = true
? ? ? ? ? ? case "-":
? ? ? ? ? ? sumSoFar -= rightOperand
? ? ? ? ? ? result = true
? ? ? ? ? ? case "*":
? ? ? ? ? ? factorSoFar *= rightOperand
? ? ? ? ? ? result = true
? ? ? ? ? ? case "/":
? ? ? ? ? ? if rightOperand != 0.0 {
? ? ? ? ? ? ? ? factorSoFar /= rightOperand
? ? ? ? ? ? ? ? result = true
? ? ? ? ? ? }
? ? ? ? default:
? ? ? ? ? ? break
? ? ? ? }
? ? ? ? return result
? ? ?}
? ??
? ? func abortOperation() {
? ? ? ? clearAll()
? ? ? ? display.text = "####"
? ? }
? ??
? ? @IBAction func digitClicked(sender: UIButton) {
? ? ? ? let digitValue = 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 = display.text!
? ? ? ? display.text = dropLast(strValue)
? ? ? ? if display.text!.isEmpty {
? ? ? ? ? ? displayValue = 0.0
? ? ? ? ? ? waitingForOperand = true
? ? ? ? }
? ? }
? ??
? ? @IBAction func clear() {
? ? ? ? if waitingForOperand {
? ? ? ? ? ? return
? ? ? ? }
? ? ? ??
? ? ? ? displayValue = 0
? ? ? ? waitingForOperand = true
? ? }
? ??
? ? @IBAction func clearAll() {
? ? ? ? sumSoFar = 0.0
? ? ? ? factorSoFar = 0.0
? ? ? ? pendingAdditiveOperator = ""
? ? ? ? pendingMultiplicativeOperator = ""
? ? ? ? displayValue = 0.0
? ? ? ? waitingForOperand = true
? ? }
? ??
? ? @IBAction func clearMemory() {
? ? ? ? sumInMemory = 0.0
? ? }
? ??
? ? @IBAction func readMemory() {
? ? ? ? displayValue = sumInMemory
? ? ? ? waitingForOperand = true
? ? }
? ??
? ? @IBAction func setMemory() {
? ? ? ? equalClicked()
? ? ? ? sumInMemory = displayValue
? ? }
? ??
? ? @IBAction func addToMemory() {
? ? ? ? equalClicked()
? ? ? ? sumInMemory += displayValue
? ? }
? ??
? ? @IBAction func multiplicativeOperatorClicked(sender: UIButton) {
? ? ? ? var clickedOperator = sender.currentTitle!
? ? ? ? var operand = displayValue
? ? ? ? if !pendingMultiplicativeOperator.isEmpty {
? ? ? ? ? ? if !calculate(operand, pendingOperator: pendingMultiplicativeOperator) {
? ? ? ? ? ? ? ? abortOperation()
? ? ? ? ? ? ? ? return
? ? ? ? ? ? }
? ? ? ? ? ? displayValue = factorSoFar
? ? ? ? } else {
? ? ? ? ? ? factorSoFar = operand
? ? ? ? }
? ? ? ??
? ? ? ? pendingMultiplicativeOperator = clickedOperator
? ? ? ? waitingForOperand = true
? ? }
? ??
? ? @IBAction func additiveOperatorClicked(sender: UIButton) {
? ? ? ? let clickedOperator = sender.currentTitle!
? ? ? ? var operand = displayValue
? ? ? ? if !pendingMultiplicativeOperator.isEmpty {
? ? ? ? ? ? if !calculate(operand, pendingOperator: pendingMultiplicativeOperator) {
? ? ? ? ? ? ? ? abortOperation()
? ? ? ? ? ? ? ? return
? ? ? ? ? ? }
? ? ? ? ? ? displayValue = factorSoFar
? ? ? ? ? ? factorSoFar = 0.0
? ? ? ? ? ? pendingMultiplicativeOperator = ""
? ? ? ? }
? ? ? ??
? ? ? ? if !pendingAdditiveOperator.isEmpty {
? ? ? ? ? ? if !calculate(operand, pendingOperator: pendingAdditiveOperator) {
? ? ? ? ? ? ? ? abortOperation()
? ? ? ? ? ? ? ? return
? ? ? ? ? ? }
? ? ? ? ? ? displayValue = sumSoFar
? ? ? ? } else {
? ? ? ? ? ? sumSoFar = operand
? ? ? ? }
? ? ? ??
? ? ? ? pendingAdditiveOperator = clickedOperator
? ? ? ? waitingForOperand = true
? ? }
? ??
? ? @IBAction func unaryOperatorClicked(sender: UIButton) {
? ? ? ? let clickedOperator = sender.currentTitle!
? ? ? ? var result: Double = 0
? ? ? ??
? ? ? ? if 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 == 0 {
? ? ? ? ? ? ? ? abortOperation()
? ? ? ? ? ? ? ? return
? ? ? ? ? ? }
? ? ? ? ? ? result = 1.0 / displayValue
? ? ? ? }
? ? ? ? displayValue = result
? ? ? ? waitingForOperand = true
? ? }
? ??
? ? @IBAction func equalClicked() {
? ? ? ? var operand = displayValue
? ? ? ? if !pendingMultiplicativeOperator.isEmpty {
? ? ? ? ? ? if !calculate(operand, pendingOperator: pendingMultiplicativeOperator) {
? ? ? ? ? ? ? ? abortOperation()
? ? ? ? ? ? ? ? return
? ? ? ? ? ? }
? ? ? ? ? ? operand = factorSoFar
? ? ? ? ? ? factorSoFar = 0.0
? ? ? ? ? ? pendingMultiplicativeOperator = ""
? ? ? ? }
? ? ? ??
? ? ? ? if !pendingAdditiveOperator.isEmpty {
? ? ? ? ? ? if !calculate(operand, pendingOperator: pendingAdditiveOperator) {
? ? ? ? ? ? ? ? abortOperation()
? ? ? ? ? ? ? ? return
? ? ? ? ? ? }
? ? ? ? ? ? pendingAdditiveOperator = ""
? ? ? ? } else {
? ? ? ? ? ? sumSoFar = operand
? ? ? ? }
? ? ? ??
? ? ? ? displayValue = sumSoFar
? ? ? ? sumSoFar = 0.0
? ? ? ? waitingForOperand = true
? ? }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。

相关文章

  • Swift中内置的集合类型学习笔记

    Swift中内置的集合类型学习笔记

    Swift中自带数组、set、字典三大集合类型,这里将学习过程中的基础的Swift中内置的集合类型学习笔记进行整理,需要的朋友可以参考下
    2016-06-06
  • 深入解析Swift代理模式

    深入解析Swift代理模式

    委托(代理)是一种设计模式,它允许类或结构体将一些需要它们负责的功能交由(委托)给其他的类型。下面这篇文章主要介绍了Swift代理模式的相关资料,文章开始先介绍了Objective-C相关的内容,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-03-03
  • Swift如何为网页承载页面添加更多功能详解

    Swift如何为网页承载页面添加更多功能详解

    这篇文章主要给大家介绍了关于Swift如何为网页承载页面添加更多功能的相关资料,包括添加菊花加载的效果、添加跳转到Safari的功能、添加复制链接的功能以及添加分享网页的功能,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2018-05-05
  • Switch语句的技巧

    Switch语句的技巧

    switch语句对一个表达式求值,将结果与 case 子语句比较,如果匹配,则从 case 处的语句向下执行,本文给大家介绍Switch语句的技巧,需要的朋友参考下吧
    2016-02-02
  • SwiftUI 登录界面布局实现示例详解

    SwiftUI 登录界面布局实现示例详解

    这篇文章主要为大家介绍了SwiftUI 登录界面布局实现示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • Swift读取App的版本信息与PCH文件详解

    Swift读取App的版本信息与PCH文件详解

    这篇文章主要介绍了Swift读取App的版本信息与PCH文件的相关资料,文中通过图文介绍的非常详细,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
    2017-03-03
  • iOS Swift UICollectionView横向分页滚动,cell左右排版问题详解

    iOS Swift UICollectionView横向分页滚动,cell左右排版问题详解

    UICollectionView是iOS中比较常见的一个控件,这篇文章主要给大家介绍了关于iOS Swift UICollectionView横向分页滚动,cell左右排版问题的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随小编来一起学习学习吧。
    2017-12-12
  • 简单了解Swift语言中的break和continue语句的用法

    简单了解Swift语言中的break和continue语句的用法

    这篇文章主要简单介绍了Swift语言中的break和continue语句的用法,与其他语言的一样用于循环语句流程控制,需要的朋友可以参考下
    2015-11-11
  • Swift教程之继承详解

    Swift教程之继承详解

    这篇文章主要介绍了Swift教程之继承详解,一个类可以从另外一个类中继承方法,属性或者其它的一些特性,当一个类继承于另外一个类时,这个继承的类叫子类,被继承的类叫父类,需要的朋友可以参考下
    2015-01-01
  • swift4更新中所遇到的一些问题总结

    swift4更新中所遇到的一些问题总结

    这篇文章主要给大家介绍了关于在swift4更新中所遇到的一些问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-12-12

最新评论

?


http://www.vxiaotou.com