Swift实现简单计算器

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

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

本文实例为大家分享了Swift实现简单计算器的具体代码,供大家参考,具体内容如下

使用Storyboard

快速而又方便的进行控件的布局,功能操作简单的进行一些运算;

代码实现

//
// ?ViewController.swift
// ?Swift_Calculator
//
// ?Created by 周文春 on 16/3/2.
// ?Copyright ? 2016年 周文春. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

? ? @IBOutlet weak var lableResult: UILabel!

? ? //第一操作
? ? var firstOperand: Double = 0.0
? ? //第二操作
? ? var secondOperand: Double = 0.0
? ? //标记是否输入了小数点
? ? var decimalPointFlag: Bool = false
? ? //是否输入第二操作数
? ? var isSecond: Bool = false
? ? //操作符
? ? var operatorFlag: String = ""

? ? 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.
? ? }


? ? @IBAction func buttonTap(sender: UIButton) {
? ? ? ? //lableResult 中默认是0,如果开始输入数字,则先清除0
? ? ? ? if lableResult.text == "0" || (isSecond && secondOperand == 0.0) {

? ? ? ? ? ? lableResult.text = ""
? ? ? ? }
? ? ? ? //将用户录入的数添加到lableResult中
? ? ? ? lableResult.text = lableResult.text! + sender.titleLabel!.text!

? ? ? ? if isSecond {
// ? ? ? ? ? ?secondOperand = (lableResult.text! as NSString).doubleValue
? ? ? ? ? ? secondOperand = NSString(string: lableResult.text!).doubleValue
? ? ? ? }else {
? ? ? ? ? ? //将lableResult中的字符串转化为双精度数
// ? ? ? ? ? ?firstOperand = (lableResult.text! as NSString).doubleValue
? ? ? ? ? ? firstOperand = NSString(string: lableResult.text!).doubleValue
? ? ? ? }
? ? ? ? print(firstOperand)
? ? }


? ? @IBAction func decimalPointTap() {

? ? ? ? if !decimalPointFlag {
? ? ? ? ? ? lableResult.text = lableResult.text! + "."
? ? ? ? ? ? if isSecond {
? ? ? ? ? ? ? ? secondOperand = (lableResult.text! as NSString).doubleValue
? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? firstOperand = (lableResult.text! as NSString).doubleValue
? ? ? ? ? ? }

? ? ? ? ? ? decimalPointFlag = !decimalPointFlag
? ? ? ? }
? ? }


? ? @IBAction func operatorTap(sender: UIButton) {
? ? ? ? if firstOperand != 0 {
? ? ? ? ? ? isSecond = true
? ? ? ? ? ? decimalPointFlag = false
? ? ? ? ? ? switch sender.titleLabel!.text! {
? ? ? ? ? ? ? ? case "+":
? ? ? ? ? ? ? ? ? ? ? operatorFlag = "+"
? ? ? ? ? ? ? ? case "-":
? ? ? ? ? ? ? ? ? ? ? operatorFlag = "-"
? ? ? ? ? ? ? ? case "*":
? ? ? ? ? ? ? ? ? ? ? operatorFlag = "*"
? ? ? ? ? ? ? ? case "÷":
? ? ? ? ? ? ? ? ? ? ? operatorFlag = "/"
? ? ? ? ? ? default:
? ? ? ? ? ? ? ? ? ? ? operatorFlag = " "
? ? ? ? ? ? }
? ? ? ? }
? ? }


? ? @IBAction func resultTap(sender: UIButton) {
? ? ? ? //确保第二操作数有值
? ? ? ? if isSecond {
? ? ? ? ? ? //除数不能为0
? ? ? ? ? ? if operatorFlag == "/" && secondOperand == 0 {
? ? ? ? ? ? ? ? print("Error: 除数不能为0")
? ? ? ? ? ? ? ? return
? ? ? ? ? ? }

? ? ? ? ? ? var result: Double = 0.0
? ? ? ? ? ? switch operatorFlag {
? ? ? ? ? ? ? ? case "+":
? ? ? ? ? ? ? ? result = firstOperand + secondOperand
? ? ? ? ? ? ? ? case "-":
? ? ? ? ? ? ? ? result = firstOperand - secondOperand
? ? ? ? ? ? ? ? case ?"*":
? ? ? ? ? ? ? ? result = firstOperand * secondOperand
? ? ? ? ? ? ? ? case ?"/":
? ? ? ? ? ? ? ? result = firstOperand / secondOperand
? ? ? ? ? ? default :
? ? ? ? ? ? ? ? result = 0.0
? ? ? ? ? ? }

? ? ? ? ? ? lableResult.text = result.description
? ? ? ? ? ? print("第一操作: \(firstOperand)")
? ? ? ? ? ? print("操作符: \(operatorFlag)")
? ? ? ? ? ? print("第二操作: \(secondOperand)")
? ? ? ? ? ? print("结果: \(result)")

? ? ? ? }

? ? }

? ? @IBAction func clear(sender: UIButton) {

? ? ? ? //lable对象显示0
? ? ? ? lableResult.text = "0"

? ? ? ? //第一操作数清零
? ? ? ? firstOperand = 0.0

? ? ? ? //第二操作数清零
? ? ? ? secondOperand = 0.0

? ? ? ? //小数点标记设置为假
? ? ? ? decimalPointFlag = false

? ? ? ? //第二操作数标记设置为假
? ? ? ? isSecond = false

? ? ? ? //操作清空
? ? ? ? operatorFlag = ""

? ? }
}

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

相关文章

最新评论

?


http://www.vxiaotou.com