cs193p – Project #1 Assignment #1 Extra Task #3

By Sa-se (, ,) [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 a ᐩ/- operation which changes the sign of the number in the display. Be careful with this one. If the user is in the middle of entering a number, you probably want to change the sign of that number and allow typing to continue, not force an enter like other operations do. On the other hand, if the user is not in the middle of typing a number, then this operation would work just like any other unary operation (e.g. cos).

Change the label of one of the “invisible” buttons to the +/- sign:

cs193p - Project #1 Assignment #1 Extra Task #3 - Add +:- Button
cs193p – Project #1 Assignment #1 Extra Task #3 – Add +:- Button

… and link it to the operate action of the view-controller class.

To be able to act as operation add another unary operation to the the calculator-brain class:

    init() {
        ...
        learnOp(Op.UnaryOperation("±", { -$0 }))
        ...
    }

When the user is still entering a number, handle the +/- operation in the view controller. If the first character is a minus sign remove it, otherwise add it:

    @IBAction func operate(sender: UIButton) {
        if let operation = sender.currentTitle {
            if userIsInTheMiddleOfTypingANumber {
                if operation == "±" {
                    let displayText = display.text!
                    if (displayText.rangeOfString("-") != nil) {
                        display.text = dropFirst(displayText)
                    } else {
                        display.text = "-" + displayText
                    }
                    return
                }
                enter()
            }
            ...
    }

The new feature influences the previous functionality.

To prevent leading zeros, we need to check also for “negative” zeros:

    @IBAction func appendDigit(sender: UIButton) {
            ...
            if (digit == "0") && ((display.text == "0") || (display.text == "-0")) { return }
            if (digit != ".") && ((display.text == "0") || (display.text == "-0")) {
                if (display.text == "0") {
                    display.text = digit
                } else {
                    display.text = "-" + digit
                }
            } else ...
    }

And because a single minus sign would look strange when hitting backspace adjust that function, too:

    @IBAction func backSpace() {
            ...
                display.text = dropLast(displayText)
                if (countElements(displayText) == 2) && (display.text?.rangeOfString("-") != nil) {
                    display.text = "-0"
                }
            ...
    }

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

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!

4 thoughts on “cs193p – Project #1 Assignment #1 Extra Task #3”

  1. Why it has to go through UnaryOperation?
    Why not?
    @IBAction func signChange() {
    if displayValue != 0{
    if displayValue > 0{
    display.text = “-” + display.text!
    }else{
    display.text = dropFirst(display.text!)
    }
    }
    }

  2. The sign button should be handled by the operate method. The only code you really need is in the init() because the display is automatically updated when it’s evaluated.

Leave a Reply

Your email address will not be published. Required fields are marked *