计算器中的函数等于

Function equals in calculator

我是 Swift 使用 Xcode 编程的新手。我创建了一个与 Windows 7(默认计算器)中的计算器非常相似的应用程序。我在使用 equals @IBAction 按钮时遇到问题。在Windows7计算器中,每个数执行运算

喜欢:

2*2 = 4

这就是结果,如果您连续单击按钮“=”,它将执行操作,第二个输入将乘以结果,所以它会像这样。

//WINDOWS 7 CALCULATOR OUTPUT

4 // this is the result
//if user press equals consecutively
4, 8 , 16 , 32 , 64
//and so on, this is how the windows 7 calcu executes the equals button

在我的代码中我遇到了问题。当我按等于时,它会连续乘以结果。

示例:

//MY CALCULATOR OUTPUT IN XCODE

2 * 2 = 4 //result is four

//If equals is click consecutively the result would be this
4 , 16 , 256 , 65536 // and so on

//so basically it will multiply the result of calculator's result.

因此,我在这里的实际目标是将 same/similar 结果作为 windows 7 计算器。我该如何执行这种方法?我是否必须声明一个临时变量来保存第二个数字输入?

到目前为止,这是我的代码:

我创建了一个名为 MyCalculator.swift

的 class

我的计算器

//
//  MyCalculator.swift
//  Calculator
//
//  Created by
//  Anonymous
//

import Foundation

class MyCalculator
{
    var memory: Float = 0

    var percentnum: Float = 0

    var currentNum: Float = 0

    var result: Float = 0

    var operation: String = ""

    var isOperation: Bool = false

    var isRepeatOperation: Bool = false

    var isNum: Bool = false

    func operation (operation:String) -> Float
    {
        if operation == "+" {
            result = result + currentNum
        }
        else if operation == "-" {
            result = result - currentNum
        }
        else if operation == "*" {
            result = result * currentNum
        }
        else if operation == "/" {
            result = result / currentNum
        }
        return result
    }

    func percent(percentNum:Float) ->Float
    {
        let valpercent = percentNum * 0.01
        result = result * valpercent
        return result
    }

    func clearAll() {
        result = 0
        currentNum = 0
        isNum = false

    }

}

这是我的 ViewController.swift 文件

查看控制器

//
//  ViewController.swift
//  Calculator
//
//  Created by 
//  Anonymous
//

import UIKit

class ViewController: UIViewController {

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

    //Start Calculator

    //Declerations      

    //Label Result: This is where the result is displayed
    @IBOutlet weak var lblResult: UILabel!


    //Initialization
    let calc = MyCalculator() //call the class MyCalculator

    //Function for Number Inputs
    @IBAction func btnNumInput(sender: AnyObject)
    {
        let num = sender.currentTitle 
        if calc.isNum == false
        {
            lblResult.text = num
            calc.isNum = true
        }
        else
        {
            lblResult.text = lblResult.text! + num!!
        }
        calc.isRepeatOperation = false
    }

    //Function for Equals
    @IBAction func btnEquals(sender: AnyObject)
    {
        calc.currentNum = Float(lblResult.text!)!
        lblHistory.text = ""
        calc.isNum = false

        if calc.operation == "+"
        {
            calc.result = calc.operation("+")
        }
        else if calc.operation == "-"
        {
            calc.result = calc.operation("-")
        }
        else if calc.operation == "*"
        {
            calc.result = calc.operation("*")
        }
        else if calc.operation == "/"
        {
            calc.result = calc.operation("/")
        }

        if calc.operation == "/" && calc.currentNum == 0
        {
            lblResult.text = "Cannot divide by zero"
            calc.result = 0
        }
        else
        {
            let stringresult = String(format:"%g" , calc.result)
            lblResult.text = "\(stringresult)"
            calc.isOperation = false
        }
    }

是的,您需要一些临时变量来存储乘数,目前您正在做的事情:

func operation (operation:String) -> Float
    {
        ...
        else if operation == "*" {
            // I suppose this is typo? and there is actualy * instead of "-"?
            result = result - currentNum  
        }
        ...
    }

所以你有:

2*2 = 4

然后 resultcurrentNum 的值都是 4。所以它是这样的:

4*4 = 16
// result, currentNum = 16
16*16 = 256

正确的方法应该是这样的:

else if operation == "*" {                    
    result = currentNum * multiplier  
}

我希望这会解决

func btnEquals(sender: AnyObject)
{
 if (!calc.isRepeatOperation) {  //New Code
    calc.currentNum = Float(lblResult.text!)!
 } 
 lblHistory.text = ""
 calc.isNum = false
 //.....
 else {
        let stringresult = String(format:"%g" , calc.result)
        lblResult.text = "\(stringresult)"
        calc.isOperation = false
        calc.isRepeatOperation = true  //New Code
    }

}

原因是 currentValue 被结果标签覆盖了。 重复的equalTo应该和previousValue进行result运算。

2*2=4=8=16

4*2=8=16=32

16/4 = 4 = 1 = 0.25

问题是:您使用 lblResult 显示结果值,并使用 lblResult.text 进行下一次计算。

您的 calc.currentNum 已被您的结果覆盖。 你必须使用一个变量来存储结果,另一个用于输入值,然后问题就会解决:)