cs193p – Assignment #1 Task #8

Add a C button that clears everything (your display, the new UILabel you added above, any pending binary operations, etc.). Ideally, this should leave your Calculator in the same state it was in when you launched it.

Add the new button (or two) in story board:
cs193p-assignment-1-task-8-winter-2017-storyboard
Continue reading “cs193p – Assignment #1 Task #8”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Assignment #1 Task #7

Implement a UILabel in your UI which shows the sequence of operands and operations that led to (or is leading to if resultIsPending) what is (or “will be” if resultIsPending) showing in the display. If resultIsPending is true, put . . . on the end of the UILabel, else put =. If the userIsInTheMiddleOfTyping, 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 = √ + 2 = would show “√(7 + 9) + 2 =” (6 in the display)
f. 7 + 9 √ would show “7 + √(9) …” (3 in the display)
g. 7 + 9 √ = would show “7 + √(9) =“ (10 in the display)
h. 7 + 9 = + 6 = + 3 = would show “7 + 9 + 6 + 3 =” (25 in the display)
i. 7 + 9 = √ 6 + 3 = would show “6 + 3 =” (9 in the display)
j. 5 + 6 = 7 3 would show “5 + 6 =” (73 in the display)
k. 4 × π = would show “4 × π =“ (12.5663706143592 in the display)

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

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Assignment #1 Task #6

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 (or the result so far if resultIsPending). The character = (the equals sign) should never appear in this description, nor should … (ellipses).

First we need to change the accumulator into a tuple to hold also the description text:

private var accumulator: (Double, String)?

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

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Assignment #1 Task #5

Add a Bool property to your CalculatorBrain called resultIsPending which returns whether there is a binary operation pending.

Create a generated property which checks if there is a pending binary operation:

    var resultIsPending: Bool {
        get {
            return nil != pendingBinaryOperation
        }
    }

The complete code for the assignment #1 task #5 is available on GitHub.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Assignment #1 Task #4

Use color to make your UI look nice. At the very least, your operations buttons must be a different color than your keypad buttons, but otherwise you can use color in whatever way you think looks nice.

Adjust the colors in storyboard:
cs193p-assignment-1-task-4-winter-2017-storyboard
Continue reading “cs193p – Assignment #1 Task #4”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Assignment #1 Task #3

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 iPhone 6’s and 7’s.

Start adding the new operations to the brain model:

    private let 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),
        "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”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Assignment #1 Task #2

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 need to have a “.” button in your calculator. Don’t worry too much about precision or significant digits in this assignment (doing so is Extra Credit).

… the button has already been added during the lecture. Because it has been copied from a digit button it points already to touchDigit. When the user is in the middle of typing, only add the digit if it is not a “.”, or if there is not already one on the display. For new new numbers, add a leading zero if necessary:

    @IBAction func touchDigit(_ sender: UIButton) {
        ...
        if userIsInTheMiddleOfTyping {
            ...
            if "." != digit || !textCurrentlyInDisplay.contains(".") {
                display.text = textCurrentlyInDisplay + digit
            }
        } else {
            display.text = "." == digit ? "0." : digit
            ...
        }        
    }

As requested in the hints we added only a single line, and adjusted another one.

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

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail