cs193p – Assignment #1 Task #1 & #2

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

Get the Calculator working as demonstrated in lectures 1 and 2.

… done during the lectures …

Your calculator already works with floating point numbers (e.g. if you touch 3 ÷ 4 =, it will properly show 0.75), however, there is no way for the user to enter a floating point number directly. Fix this by allowing legal floating point numbers to be entered (e.g. “192.168.0.1” is not a legal floating point number!). You will have to add a new “.” button to your calculator. Don’t worry too much about precision or significant digits in this assignment.

The storyboard contains already the “.” button from the lecture.

Similar to userIsInTheMiddleOfTyping add a new boolean property to indicate that the user is in the middle of entering a floating point number:

private var userIsInTheMiddleOfFloatingPointNummer = false

When the user pressed the “.” button, and it was used before, ignore it. When not in the middle of typing add a zero before the dot – to be able to do that, you need to change digit to be a variable instead of a constant. Otherwise nothing needs to be changed, just set the new boolean from above to true.

@IBAction private func touchDigit(sender: UIButton) {
  var digit = sender.currentTitle!        
  if digit == "." {
    if userIsInTheMiddleOfFloatingPointNummer {
      return
    }
    if !userIsInTheMiddleOfTyping {
      digit = "0."
    }
    userIsInTheMiddleOfFloatingPointNummer = true
  }
  ...
}

To reset the floating indicator, you could just set it to false whenever the typing indicator is reset. Currently that’s actually only once. Because those two are tightly coupled, you can use a property observer:

private var userIsInTheMiddleOfTyping = false {
  didSet {
    if !userIsInTheMiddleOfTyping {
      userIsInTheMiddleOfFloatingPointNummer = false
    }
  }
}

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

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

9 thoughts on “cs193p – Assignment #1 Task #1 & #2”

  1. Glad you’re doing these for iOS9, they will be of great help 🙂

    I’m kind of stuck in required task 7. I’m not sure how to do it without a lot of code (which was one of the hints), so I’m curious about what your solution will be.

  2. Thank you so much for your time and effort on this blog! It’s been a fantastic reference for someone following the Stanford course in iTunes U who hasn’t coded for several years and is now attempting to learn and program in Swift for iOS.

  3. I took a different approach, that uses rangeOfString as outlined in the hints. It also states that this can be done in one line of code (I did not quite succeed), but I did this: if if digit == “.” && display.text!.rangeOfString(“.”) != nil && userIsInTheMiddleOfTypingANumber {
    return
    }

    Right after declaration of the digit variable. It seems to work and now I’m wondering did I miss something crucial. Idea being: If the user is entering a number and the dot already exists in the string, exit the processing function without doing anything. Otherwise proceed as normally.

    1. I went along the same line of thoughts with a small variation..
      I add the following after declaration of the digit variable

      if digit == “.”
      {
      if let _ = displayLabel.text?.rangeOfString(digit)
      {
      // meaning that the optional binding worked – So there was a “.” already so I just return
      return
      } else
      {
      // here the optional binding didnt work so I want to know if I am not in the middle of
      // entering a number I want to make it 0.
      if !userIsInTheMiddleOfEnteringANumber
      {
      digit = “0.”
      }
      }
      // else it continues with the code as before and adds a . if already in the middle of typing

  4. i really like this solution, i made an outlet to the Dot Button, enabled/disabled it based on rangeOfString’s return option

  5. @IBAction private func touchDigit(sender: UIButton) {
    let digit = sender.currentTitle!
    if userIsInTheMiddleOfTyping {
    // if there is already a “.” and you try anotherone – do nothing, otherwise add “.” to the display.text!
    if !(display.text!.rangeOfString(“.”) != nil && digit == “.”) {
    display.text = display.text! + digit
    }
    } else {
    display.text = digit
    // if “.” is the first digit add a 0 at the front
    if digit == “.” { display.text = “0.” }
    userIsInTheMiddleOfTyping = true
    }
    }

Leave a Reply

Your email address will not be published.