cs193p – Project #2 Assignment #2 Task #5

Ralf Pfeifer at the German language Wikipedia [GFDL (http://www.gnu.org/copyleft/fdl.html) or CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/)], via Wikimedia Commons

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()
    }


… which needs to be added to the enum definition:

    private enum Op: Printable
    {
        ...
        case Variable(String)
        
        var description: String {
            get {
                switch self {
                ...
                case .Variable(let symbol):
                    return symbol
                }
            }
        }
    }

The evaluation is part of the next task, thus we return nil for now:

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

These changes do not show in the in the calculator for the moment. To see if they do what we expect them to do create a tiny test case:

    func testPushOperandVariable() {
        var brain = CalculatorBrain()
        XCTAssertNil(brain.pushOperand("x"))
        XCTAssertEqual(brain.program[0] as String, "x")
    }

To implement the dictionary of variable values, just define for now:

    var variableValues = [String: Double]()

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

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

6 thoughts on “cs193p – Project #2 Assignment #2 Task #5”

  1. I get an error on the UT file, it looks like CalculatorBrain is unresolved. Did you get the same? How would you get rid of it?

    1. What do you mean by “UT file”? If a type is unresolved, though you are sure it exists, might happen when there is another error in your code which trips the compiler … Sometimes forcing a rebuild helps …

  2. I’m getting the same error in the test file. I don’t have much experience with testing so maybe I’m doing something wrong.

    The error is:
    Use of unresolved identifier ‘CalculatorBrain’

    on the line:
    var brain = CalculatorBrain()

    1. Have a look if the CalculatorBrain.swift file is in the build path for the test. Welect the file in the project navigator on the left hand side. On the right hand side, the file inspector should show both targets in the “Target Membership” sections selected.

  3. Hello, I am a freshmen in Swift and my english is not so good.Where should I put the code (the tiny test case you mentioned), in the CalculatorBrain.swift or in the CalculatorTests.swift or anywhere else?And I noticed that the variableValues has not been initionalized in the Init . It doesn’t matter at all ?

Leave a Reply

Your email address will not be published.