cs193p – Assignment #1 Task #8

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

Add a C button that clears everything (your display, the new UILabel you added above, etc.). The Calculator should be in the same state as it is at application startup after you touch this new button.

Add a clear button (or two) in the interface builder:

Storyboard calculator adding clear buttons
Storyboard calculator adding clear buttons

Link the button(s) to a new action method which resets the calculator model and the two displays:

@IBAction func clearEverything(sender: UIButton) {
  brain = CalculatorBrain()
  display.text = "0"
  history.text = " "
}

The complete code for task #8 is available on GitHub.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Assignment #1 Task #7

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

Use the two properties above to implement a UILabel in your UI which shows the sequence of operands and operations that led to what is showing in the display. If isPartialResult, put . . . on the end of the UILabel, else put =. If the userIsInTheMiddleOfTypingANumber, you can leave the UILabel showing whatever was there before the user started typing the number. Examples …
a. touching 7 + would show “7 + …” (with 7 still in the display)
b. 7 + 9 would show “7 + …” (9 in the display)
c. 7 + 9 = would show “7 + 9 =” (16 in the display)
d. 7 + 9 = √ would show “√(7 + 9) =” (4 in the display)
e. 7 + 9 √ would show “7 + √(9) …” (3 in the display)
f. 7 + 9 √ = would show “7 + √(9) =“ (10 in the display)
g. 7 + 9 = + 6 + 3 = would show “7 + 9 + 6 + 3 =” (25 in the display)
h. 7 + 9 = √ 6 + 3 = would show “6 + 3 =” (9 in the display)
i. 5 + 6 = 7 3 would show “5 + 6 =” (73 in the display)
j. 7 + = would show “7 + 7 =” (14 in the display)
k. 4 × π = would show “4 × π =“ (12.5663706143592 in the display)
l. 4 + 5 × 3 = would show “4 + 5 × 3 =” (27 in the display)
m. 4 + 5 × 3 = could also show “(4 + 5) × 3 =” if you prefer (27 in the display)

Continue reading “cs193p – Assignment #1 Task #7”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Assignment #1 Task #6

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

Add a Bool property to your CalculatorBrain called isPartialResult which returns whether there is a binary operation pending (if so, return true, if not, false).

This a a short one, just add a computed property depending on the private pending property:

var isPartialResult: Bool {
  get {
    return pending != nil
  }
}

… and adjust the unit tests:

func testDescription() {  
  // a. touching 7 + would show “7 + ...” (with 7 still in the display)
  ...
  XCTAssertTrue(brain.isPartialResult)
  
  // b. 7 + 9 would show “7 + ...” (9 in the display)
  ...
  XCTAssertTrue(brain.isPartialResult)
  
  // c. 7 + 9 = would show “7 + 9 =” (16 in the display)
  ...
  XCTAssertFalse(brain.isPartialResult)

  // d. 7 + 9 = √ would show “√(7 + 9) =” (4 in the display)
  ...
  XCTAssertFalse(brain.isPartialResult)
  
  // e. 7 + 9 √ would show “7 + √(9) ...” (3 in the display)
  ...
  XCTAssertTrue(brain.isPartialResult)
  
  // f. 7 + 9 √ = would show “7 + √(9) =“ (10 in the display)
  ...
  XCTAssertFalse(brain.isPartialResult)
  
  // g. 7 + 9 = + 6 + 3 = would show “7 + 9 + 6 + 3 =” (25 in the display)
  ...
  XCTAssertFalse(brain.isPartialResult)
  
  // h. 7 + 9 = √ 6 + 3 = would show “6 + 3 =” (9 in the display)
  ...
  XCTAssertFalse(brain.isPartialResult)
  
  // i. 5 + 6 = 7 3 would show “5 + 6 =” (73 in the display)
  ...
  XCTAssertFalse(brain.isPartialResult)
  
  // j. 7 + = would show “7 + 7 =” (14 in the display)
  ...
  XCTAssertFalse(brain.isPartialResult)
  
  // k. 4 × π = would show “4 × π =“ (12.5663706143592 in the display)
  ...
  XCTAssertFalse(brain.isPartialResult)
  
  // m. 4 + 5 × 3 = could also show “(4 + 5) × 3 =” if you prefer (27 in the display)
  ...
  XCTAssertFalse(brain.isPartialResult)
}

The complete code for task #6 is available on GitHub.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Assignment #1 Task #5

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

Add a String property to your CalculatorBrain called description which returns a description of the sequence of operands and operations that led to the value returned by result. “=“ should never appear in this description, nor should “…”.

Let’s add a public string property called description, and compute it – like the result property – from another new private string property – the description accumulator. When no operation is pending, we can just return the accumulator otherwise we will have to do a little bit more:

var description: String {
  get {
    if pending == nil {
       return descriptionAccumulator
    } else {
       return ...
    }
  }
}
private var descriptionAccumulator = "0"

Continue reading “cs193p – Assignment #1 Task #5”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Assignment #1 Task #3 & #4

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

Add some more operations buttons to your calculator such that it has at least a dozen operations total (it can have even more if you like). You can choose whatever operations appeal to you. The buttons must arrange themselves nicely in portrait and landscape modes on all iPhones.

Add the new operations to the calculator model:

private var operations: Dictionary<String,Operation> = [
  ...
  "x²" : Operation.UnaryOperation({ pow($0, 2) }),
  "x³" : Operation.UnaryOperation({ pow($0, 3) }),
  "x⁻¹" : Operation.UnaryOperation({ 1 / $0 }),
  "sin" : Operation.UnaryOperation(sin),
  "cos" : Operation.UnaryOperation(cos),
  "tan" : Operation.UnaryOperation(tan),
  "sinh" : Operation.UnaryOperation(sinh),
  "cosh" : Operation.UnaryOperation(cosh),
  "tanh" : Operation.UnaryOperation(tanh),
  "ln" : Operation.UnaryOperation(log),
  "log" : Operation.UnaryOperation(log10),
  "eˣ" : Operation.UnaryOperation(exp),
  "10ˣ" : Operation.UnaryOperation({ pow(10, $0) }),
  "x!" : Operation.UnaryOperation(factorial),
  "xʸ" : Operation.BinaryOperation(pow),
]

Continue reading “cs193p – Assignment #1 Task #3 & #4”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

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

Continue reading “cs193p – Assignment #1 Task #1 & #2”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Smashtag Update Swift 1.2

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

To run the Smashtag code with Swift 1.2. are quite extensive:

  • countElements() is now called count()
  • as needs to be replaced with as!
  • reduce() requires the combine parameter named explicitly
  • same with custom methods, its now quite strict …
  • let is now more restrictive, some parameters have to be changed to var (it might be I went a little bit over board here)
  • NSString is no String any more and needs to be cast explicitly
  • if a parameter is set within a closure of the init method, set a default value outside

The updated code is available on GitHub.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail