cs193p – Project #2 Assignment #2 Task #6

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

The evaluate() function should use a variable’s value (from the variableValues dictionary) whenever a variable is encountered or return nil if it encounters a variable with no corresponding value.

… which actually means just return the dictionary entry from the variable values:

    private func evaluate(ops: [Op]) -> (result: Double?, remainingOps: [Op]) {
                ...
            case .Variable(let symbol):
                return (variableValues[symbol], remainingOps)
            ...
    }

Continue reading “cs193p – Project #2 Assignment #2 Task #6”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Project #2 Assignment #2 Task #5

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

Add the capability to your CalculatorBrain to allow the pushing of variables onto its internal stack. Do so by implementing the following API in your CalculatorBrain …

      func pushOperand(symbol: String) -> Double?
      var variableValues: Dictionary<String,Double>

These must do exactly what you would imagine they would: the first pushes a “variable” onto your brain’s internal stack (e.g. pushOperand(“x”) would push a variable named x) and the second lets users of the CalculatorBrain set the value for any variable they wish (e.g. brain.variableValues[“x”] = 35.0). pushOperand should return the result of evaluate() after having pushed the variable (just like the other pushOperand does).

Implementing the push method looks just like the existing one, the only difference is that we append a variable instead of an operand:

    func pushOperand(symbol: String) -> Double? {
        opStack.append(Op.Variable(symbol))
        return evaluate()
    }

Continue reading “cs193p – Project #2 Assignment #2 Task #5”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Project #2 Assignment #2 Task #4

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

The extra credit item from last week to turn displayValue into a Double? (i.e, an Optional rather than a Double) is now required. displayValue should return nil whenever the contents of the display cannot be interpreted as a Double. Setting displayValue to nil should clear the display.

… done that, too. However, hint #1 asks to consider using optional chaining, which we did not up to now.

Assuming there is always a text string, and that we handle the internationalization problem elsewhere, we can reduce the getter of displayValue to a single line using optional chaining:

return NSNumberFormatter().numberFromString(display.text!)?.doubleValue

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

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Project #2 Assignment #2 Task #3

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

Your UI should always be in sync with your Model (the CalculatorBrain).

… meaning we should reflect changes in the model in the UI … we will do that, too.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Project #2 Assignment #2 Task #1

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

All of the changes to the Calculator made in lecture must be applied to your Assignment 1. Get this fully functioning before proceeding to the rest of the Required Tasks. And, as last week, type the changes in, do not copy/paste from anywhere.

… done …

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail