美文网首页
iOS 计算器的实现

iOS 计算器的实现

作者: 我会回来的 | 来源:发表于2021-09-18 18:43 被阅读0次

iOS.  计算器的实现

//

//  ViewController.swift

//  CalculatorDemo

//

//  Created by Mac on 2021/9/18.

//

importUIKit

class ViewController: UIViewController {

    // 缓存值

    var cacheValue = 0

    //缓存符号

    var cacheSign = ""

    //是否输入小数点

    var isPointInput = false

    // 运算符tag

    var numberTag = 0

    //是否有旧值

    var isNewValue = false

    @IBOutlet weak var inputValueLable: UILabel!

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view.

    }

    //数字按钮

    @IBActionfuncnumberBtn(_sender:UIButton) {

        letscreenNum =Int(inputValueLable.text! )

               ifisPointInput{  //如果小数点输入,则执行下面语句

                inputValueLable.text!+=sender.titleLabel!.text!

               }

               else{  //如果小数点未输入,则执行下面语句

                inputValueLable.text="\(screenNum!*10+Int(sender.titleLabel!.text!)!)"

               }

    }

    //添加小数点

    @IBActionfuncpointActionBtn(_sender:UIButton) {

        if !isPointInput{

            inputValueLable.text!+="."

            isPointInput=true

        }

    }

    //运算符

    @IBActionfuncoperatorBtn(_sender:UIButton) {

        numberTag= sender.tag

        cacheValue = Int(inputValueLable.text ?? "") ?? 0

        isPointInput = false

        inputValueLable.text = "0"

        isNewValue=true

    }

    //等号

    @IBActionfuncequalActionBtn(_sender:UIButton) {

        varequalResult =0

        letcurrentValue =Int(inputValueLable.text??"")

        switchnumberTag{

        case0:

            equalResult =Int(currentValue ??0)  +Int(cacheValue)

        case1:

            equalResult =Int(cacheValue)  -Int(currentValue ??0)

        case2:

            equalResult =  Int(cacheValue)*Int(currentValue ??0)

        case3:

            equalResult =Int(cacheValue)/Int(currentValue!)

        default:

            break

        }

        inputValueLable.text=String(equalResult)

        print("===========🍺🍺\(equalResult)")

        isPointInput = false

        cacheValue=Int(equalResult)

        isNewValue=true

    }

}

相关文章

网友评论

      本文标题:iOS 计算器的实现

      本文链接:https://www.haomeiwen.com/subject/jbsygltx.html