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

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

When the user hits an operation button, put an = on the end of the UILabel you added in the Required Task above. Thus the user will be able to tell whether the number in the Calculator’s display is the result of a calculation or a number that the user has just entered. Don’t end up with multiple occurrences of = in your UILabel.

When the display value is set – which is basically every time an operation button is pressed – add the “=” to the history label but only if there is something to show:

    var displayValue: Double {
            ...
            let stack = brain.showStack()
            if !stack!.isEmpty {
                history.text = stack! + " ="
            }
        ...
    }


When a new number is entered show the stack without equation sign:

    @IBAction func appendDigit(sender: UIButton) {
        ... else {
            ...
            history.text = brain.showStack()
        }
    }

Finally also reset the history label:

    @IBAction func clear() {
        ...
        history.text = ""
    }

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

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!

10 thoughts on “cs193p – Project #1 Assignment #1 Extra Task #2”

  1. I think that it would be better if instead of —

    var displayValue: Double {
    ...
    let stack = brain.showStack()
    if !stack!.isEmpty {
    history.text = stack! + " ="
    }
    ...
    }

    we use —

    var displayValue: Double {
    ...
    if let stack = brain.showStack() {
    if !stack!.isEmpty {
    history.text = stack! + " ="
    }
    }
    ...
    }

    1. The statement:
      if let stack = brain.showStack() {….
      is very appropriate.
      After that, the tipe of stack is String, not optional String. Therefore the successive postfix exclamation points are not needed.

  2. Hi!
    I do not understand the proposed solution.
    Is supposed to display only the symbol “=” when an operation is performed.
    This is what I’ve done and it seems to work, but I’m newbie and may be too easy:
    @IBAction func operate(sender: UIButton) {
    if userIsInTheMiddleOfTypingANumber {
    enter()
    }
    if let operation = sender.currentTitle {
    if let result = brain.performOperation(operation) {
    displayValue = result
    history.text = brain.showTrack()! + ” =”
    } else {
    displayValue = 0
    }

    }

    }

  3. My solution has only one line of code, and is more appropriate given the task:

    @IBAction func operate(sender: UIButton) {
    if userIsInTheMiddleOfTypingANumber {
    enter()
    }
    if let operation = sender.currentTitle {
    if let result = brain.performOperation(operation) {
    displayValue = result
    } else {
    displayValue = nil
    }
    }
    history.text = history.text! + "=" // All you need
    }

    1. I think you made a small mistake Patrick. history.text! += ” =” should have been inside the “if let result = brain.performOperation(operation) {}” loop as otherwise it will show “=” even when wrong/ inadmissible operation is entered by user.

Leave a Reply

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