cs193p – Project #2 Assignment #2 Extra Task #2

von Unbekannt [Public domain oder Public domain], via Wikimedia Commons

Please note, this blog entry is from a previous course. You might want to check out the current one.

Add Undo to your Calculator. In Assignment 1’s Extra Credit, you might have added “backspace”. Here we’re talking about combining both backspace and actual undo into a single button. If the user is in the middle of entering a number, this Undo button should be backspace. When the user is not in the middle of entering a number, it should undo the last thing that was done in the CalculatorBrain.

Just for cosmetics change the name of the backspace button to “CE”:

cs193p - Project #2 Assignment #2 Extra Task #2 - Undo Button
cs193p – Project #2 Assignment #2 Extra Task #2 – Undo Button


Add a new public method to the calculator brain to remove the last object form the stack:

    func popOperand() -> Double? {
        if !opStack.isEmpty {
            opStack.removeLast()
        }
        return evaluate()
    }

… and use it, when the undo button gets pressed and the “user is not in the middle of typing a number”:

    @IBAction func backSpace() {
        if userIsInTheMiddleOfTypingANumber {
            ...
        } else {
            if let result = brain.popOperand() {
                displayValue = result
            } else {
                displayValue = nil
            }
        }
    }

I added some additional cosmetic changes, which are not that breathtaking and not worth mentioning …

The complete code for extra task #2 is available on GitHub.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

Leave a Reply

Your email address will not be published.