cs193p – Assignment #2 Task #5

We made the result, description and resultIsPending vars non-private API in Assignment 1. That means we signed up to continue to support them even though we are now adding a new feature (variables) in this assignment which sort of makes them irrelevant. Really what we want to do is deprecate these (you’ll see all sorts of deprecated iOS API in Xcode), but for now we will keep the old result, description and resultIsPending vars around and just implement each of them by calling evaluate with the argument nil (i.e. they will give their answer assuming the value of any variables is zero). However, do not use any of these vars anywhere in your code in this assignment. Use evaluate instead.

We use already the evaluate method for those three properties. Just place something like the following above each of them:

@available(*, deprecated, message: "no longer needed ...")

… and don’t use them any more in the view controller:

    @IBAction func performOperation(_ sender: UIButton) {
        ...
        let evaluated = brain.evaluate()        
        if let result = evaluated.result {
            displayValue = result
        }        
        if "" != evaluated.description {
            descriptionDisplay.text = evaluated.description.beautifyNumbers() + (evaluated.isPending ? "…" : "=")
        } else {
            descriptionDisplay.text = " "
        }
    }

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

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Assignment #2 Task #4

Now that you allow variables to be entered as operands, add a method to evaluate the CalculatorBrain (i.e. calculate its result) by substituting values for those variables found in a supplied Dictionary …

    func evaluate(using variables: Dictionary<String,Double>? = nil)
          -> (result: Double?, isPending: Bool, description: String)

Note that this takes an Optional Dictionary (with Strings as keys and Doubles as values) as its argument and that that argument defaults to nil if not supplied when this method is called. Also note that it returns a tuple (the first element of which is an Optional Double). This method is not mutating and you are not allowed to make it so. If a variable that has been set as an operand is not found in the Dictionary, assume its value is zero.

Continue reading “cs193p – Assignment #2 Task #4”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Assignment #2 Task #3

Add the capability to your CalculatorBrain to allow the input of variables. Do so by
implementing the following API in your CalculatorBrain …

func setOperand(variable named: String)

This must do exactly what you would imagine it would: it inputs a “variable” as the operand (e.g. setOperand(variable: “x”) would input a variable named x). Setting the operand to x and then performing the operation cos would mean cos(x) is in your CalculatorBrain.

As you might have seen form the previous tasks – the tasks of this assignment are not that easily separable. Let’s start by adding two new types to operation structure. One for operands and one for variables:

    private enum Operation {
        ...
        case operand(Double)
        case variable(String)
    }

Continue reading “cs193p – Assignment #2 Task #3”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Assignment #2 Task #1

Do not change, remove or add any public API (i.e. non-private funcs and vars) from your CalculatorBrain from Assignment 1 except undo() and as specified in Required Task 3 and 4. Also, continue to use the Dictionary as CalculatorBrain’s primary internal data structure for performing operations.

… will do …

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

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