Search code examples
iosswiftcalculatorequals-operator

Function equals in calculator


I'm new to Swift programming using Xcode. I created an application that is much similar to the calculator in Windows 7 (default calculator). I'm having a problem with the equals @IBAction button. In Windows 7 calculator, each number that executes the operation

like:

2*2 = 4

That will be the result and if you click the button " = " consecutively it will execute the operation and the second input would multiply the result so it would be something like this.

//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

In my code I'm having the problem of it. When I press equals it will multiply the result consecutively.

Example:

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

So, what I'm actually aiming here is to have the same/similar result as the windows 7 calculator. How do I perform this kind of method? Do I have to declare a temporary variable that will hold the second number input?

This is my code so far:

I created a class called MyCalculator.swift

MY CALCULATOR

//
//  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

    }

}

and this is my ViewController.swift file

VIEW CONTROLLER

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

Solution

  • I hope this will solve

    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
        }
    

    }

    The reason is the currentValue is overwritten from the result label. Repeated equalTo should perform the result operation with the previousValue.

    2*2 = 4 = 8 =16

    4*2 = 8 = 16 = 32

    16/4 = 4 = 1 = 0.25