cs193p – Assignment #1 Extra Task #3

Make one of your operation buttons be “generate a random double-precision floating point number between 0 and 1”. This operation button is not a constant (since it changes each time you invoke it). Nor is it a unary operation (since it does not operate on anything). Probably the easiest way to generate a random number in iOS is the global Swift function arc4random() which generates a random number between 0 and the largest possible 32-bit integer (UInt32.max). You’ll have to get to double precision floating point number from there, of course.

Change one of the buttons in story board:
cs193p-assignment-1-extra-task-3-winter-2017-storyboard
Continue reading “cs193p – Assignment #1 Extra Task #3”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Assignment #1 Extra Task #2

Figure out from the documentation how to use the iOS struct NumberFormatter to format your display so that it only shows 6 digits after the decimal point (instead of showing all digits that can be represented in a Double). This will eliminate (or at least reduce) the need for Autoshrink in your display. While you’re at it, make it so that numbers that are integers don’t have an unnecessary “.0” attached to them (e.g. show “4” rather than “4.0” as the result of the square root of sixteen). You can do all this for your description in the CalculatorBrain as well.

First of all, the last task had a tiny bug. when entering e.g. “0,” will show a leading zero:

    @IBAction func backSpace(_ sender: UIButton) {
            ...
            if text.isEmpty || "0" == text {
                ...
    }

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

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Assignment #1 Extra Task #1

Implement a “backspace” button for the user to touch if they hit the wrong digit button. This is not intended to be “undo,” so if the user hits the wrong operation button, he or she is out of luck! It is up to you to decide how to handle the case where the user backspaces away the entire number they are in the middle of typing. You will probably find the Strings and Characters section of the Swift Reference Guide to be helpful here.

Add the backspace button to the storyboard:
cs193p-assignment-1-extra-task-1-winter-2017-storyboard
Continue reading “cs193p – Assignment #1 Extra Task #1”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

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