cs193p – Project #2 Assignment #2 Extra Task #3

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

Add a new method, evaluateAndReportErrors(). It should work like evaluate() except that if there is a problem of any kind evaluating the stack (not just unset variables or missing operands, but also divide by zero, square root of a negative number, etc.), instead of returning nil, it will return a String with what the problem is (if there are multiple problems, you can simply return any one of them you wish). Report any such errors in the display of your calculator (instead of just making it blank or showing some weird value). You must still implement evaluate() as specified in the Required Tasks above, but, if you want, you can have evaluate() return nil if there are any errors (not just in the “unset variable” or “not enough operands” case). The push and perform methods should still return Double? (which is kind of a wasted evaluation, but we want to be able to evaluate your Extra Credit separate from the Required Tasks).

The definitions of the unary and binary operations get new parameters to pass optional error-test methods (and all their occurrences need to be adjusted for the new parameter):
Continue reading “cs193p – Project #2 Assignment #2 Extra Task #3”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Project #2 Assignment #2 Extra Task #2

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

Add Undo to your Calculator. In Assignment 1’s Extra Credit, you might have added “backspace”. Here we’re talking about combining both backspace and actual undo into a single button. If the user is in the middle of entering a number, this Undo button should be backspace. When the user is not in the middle of entering a number, it should undo the last thing that was done in the CalculatorBrain.

Just for cosmetics change the name of the backspace button to “CE”:

cs193p - Project #2 Assignment #2 Extra Task #2 - Undo Button
cs193p – Project #2 Assignment #2 Extra Task #2 – Undo Button

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

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Project #2 Assignment #2 Extra Task #1

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

Make your description have as few parentheses as possible for binary operations.

Add a new computed variable to the Op enum, which returns by the default the maximum integer value and for binary operations the value you set when defining the operation:

    private enum Op: Printable
    {
        ...
        case BinaryOperation(String, Int, (Double, Double) -> Double)
        ...
        var precedence: Int {
            get {
                switch self {
                case .BinaryOperation(_, let precedence, _):
                    return precedence
                default:
                    return Int.max
                }
            }
        }
    }

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

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Project #2 Assignment #2 Task #12

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

Your UI should look good on any size iPhone in both portrait and landscape (don’t worry about iPad until next week). This means setting up Autolayout properly, nothing more.


… didn’t change anything there either. The two new memory buttons had already be there, just without functionality.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Project #2 Assignment #2 Task #11

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

When you touch the C button, the M variable should be removed from the variableValues Dictionary in the CalculatorBrain (not set to zero or any other value). This will allow you to test the case of an “unset” variable (because it will make evaluate() return nil and thus your Calculator’s display will be empty if M is ever used without a →M).

… well … we (I) just reset the whole calculator brain by throwing away the old brain and creating a new one. Thus, also the variable values are reset by default. Maybe that was not the intended way to do …

If you have another better solution, please post them in the comments section!

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Project #2 Assignment #2 Task #9

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

Add two new buttons to your Calculator’s keypad: →M and M. These 2 buttons will set and get (respectively) a variable in the CalculatorBrain called M.

  1. →M sets the value of the variable M in the brain to the current value of the display (if any)
  2. →M should not perform an automatic ↲ (though it should reset “user is in the middle of typing a number”)
  3. Touching M should push an M variable (not the value of M) onto the CalculatorBrain
  4. Touching either button should show the evaluation of the brain (i.e. the result of evaluate()) in the display
  5. →M and M are Controller mechanics, not Model mechanics (though they both use the Model mechanic of variables).
  6. This is not a very great “memory” button on our Calculator, but it’s good for testing whether our variable function implemented above is working properly. Examples …
    7 M + √ ⇒ description is √(7+M), display is blank because M is not set
    9 →M ⇒ display now shows 4 (the square root of 16), description is still √(7+M)
    14 + ⇒ display now shows 18, description is now √(7+M)+14

Rename the two remaining blank buttons in storyboard and link them to actions in the view controller:

cs193p - Project #2 Assignment #2 Task #9 - Memory Buttons
cs193p – Project #2 Assignment #2 Task #9 – Memory Buttons

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

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail