traff
traff

Reputation: 135

Error when add negative numbers in calculator

I have this code:

class MainViewController: UIViewController {
    @IBOutlet weak var summaryLbl: UILabel!
    var actualNumber: Double = 0
    var previousNumber: Double = 0
    var operationMath: Bool = false
    var operation = 0

    @IBAction func numberPressed(_ sender: UIButton) {
        if operationMath == true {
            summaryLbl.text = String(sender.tag)
            actualNumber = Double(summaryLbl.text!)!
            operationMath = false
        } else {
            if summaryLbl.text == "0" {
                summaryLbl.text = ""
            }
            summaryLbl.text = summaryLbl.text! + String(sender.tag)
            actualNumber = Double(summaryLbl.text!)!
        }
    }

    @IBAction func buttons(_ sender: UIButton) {
        if summaryLbl.text != "" && sender.tag != 10 && sender.tag != 17 {
            previousNumber = Double(summaryLbl.text!)!

            if sender.tag == 13 {
                summaryLbl.text = "/"
            } else if sender.tag == 14 {
                summaryLbl.text = "x"
            } else if sender.tag == 15 {
                summaryLbl.text = "-"
            } else if sender.tag == 16 {
                summaryLbl.text = "+"
            } else if sender.tag == 11 {
                var number: Double =  Double(summaryLbl.text!)!
                number.negate()
                let rounded = number.rounded()
                summaryLbl.text = String(rounded).replacingOccurrences(of: ".0", with: "", options: .literal, range: nil)
            }
            operation = sender.tag
            operationMath = true
        } else if sender.tag == 17 {
            var result: Double = 0
            var rounded: Double = 0

            if operation == 13 {
                result = previousNumber / actualNumber
            } else if operation == 14 {
                result = previousNumber * actualNumber
            } else if operation == 15 {
                result = previousNumber - actualNumber
            } else if operation == 16 {
                result = previousNumber + actualNumber
            } else if operation == 12 {
                result = previousNumber.truncatingRemainder(dividingBy: actualNumber)
            }
            rounded = result.rounded()
            if (result == rounded) { 
                summaryLbl.text = String(result).replacingOccurrences(of: ".0", with: "", options: .literal, range: nil)
            } else { 
                summaryLbl.text = String(result)
            }
        } else if sender.tag == 10 {
            summaryLbl.text = "0"
            previousNumber = 0
            actualNumber = 0
            operation = 0
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        summaryLbl.text = "0"
        previousNumber = 0
        actualNumber = 0
        operation = 0
    }
}

This is simple calculator. I have a problem with calculations.

  1. When I click the buttons, for example: 2 + 5 * - then the application turns off with an error. When I enter such a key combination: 2 + 5 = This calculation will be done correctly.  
  2. How do I add commas to numbers?

Does anyone know how to fix the above problems?

Upvotes: 1

Views: 225

Answers (1)

HAK
HAK

Reputation: 2071

A calculator is a Finite State Machine. It can be very complex but in its simplest form it resembles this:

calculator FA

So if we keep things simple and take the above machine as our target, after 2 + 5, our machine expects equals(=) to calculate the result or if an operator is added (like * in our case) it will expect a digit next. giving an operator (minus in our case) will result in an error.

The complexity is limited only by your imagination. You can add support for decimal point numbers, brackets, powers etc. The more sugar you want to add the more complex the FSM will become.

I suggest starting with the simplest one. Maintain your states, the transitions allowed next and error handling in case of wrong transition.

Check this repo on github for Finite State Machine in swift: https://github.com/vishalvshekkar/SwiftFSM

And the corresponding article: https://blog.vishalvshekkar.com/finite-state-machine-in-swift-ba0958bca34f

Upvotes: 1

Related Questions