Friday, 10 August 2012

CS193p Progress & Notes on the Course

CS193p The Stanford Course on IOS programming

What happened between Assignment 1 Calculator and The Start of Assignment II?

This course involves a steep learning curve. Assignment one helped me see how Java relates to Obj-C. I found Assignment 2 very tough. I think that understanding what happened in the demo and then what  is required is the main difficulty rather than the coding.
This blog is by someone who seems to know very well what they are about. Very helpful. 

The lecture / demo achieved the following.

Previously, the stack contained just numbers. Operations were applied when pressed in the method performOperation. pushOperand & popOperand just did exactly that, they pulled values off the stack or put numbers on the stack.

In the demo, the program was changed so that the stack could contain operands and operators. The method performOperation was changed so that we could deal with a stack that was full of operators and operands.

The code from performOperation was transferred to popOperandOffStack which might more correctly be called popOffStackWhateverHappensToBeAtTheTop because the stack has has been changed to contain Operators and Operands. More on popOperandOffStack later.

performOperation is now changed so that rather than just performing the operation sent, it pushes the operation (A string) onto the stack and calls runProgram with the parameter delivered by the getter program. The parameter program when got, is a copy of the programStack.

runProgram creates a mutable array called stack, it checks that the programStack delivered by program is in fact an Array by using introspection and if it is it stack becomes a mutable copy  of programStack.
Now we call popOperandOffStack with whatever stack has become, i.e. an empty stack or a mutable copy of the program stack.

popOperandOffStack is recursive. This is important fo several reasons, not least of which is that a RPN calculator stack, is a binary tree which can be traversed via preorder, postorder or inorder traversal. Depending on which one of these you do you will get different versions including an infix version which is the more familiar algebraic notation of most calculators.

popOperandOffStack sets its result to zero, sees if there is anything on the stack, if it is a number then it just returns it. If it is a String then it is treated as an operation using the same code as previously except that rather than calling popOperand to get the operand for the operation, the method is now calling itself. If you look here, wikipedia does a good job of explaining how this works.

Note. If the stack contained a load of numbers only, then nothing happens, popOperandOffStack  wil only consume as many numbers off the stack as it has operators to consume them.

What is Assignment II all about then?

Task 1. In Assignment II Task 1 we will be asked to allow the stack to contain any string at all and if that string is not an operator to replace that string with a value that will be in a Dictionary (hopefully), otherwise replace the string in the stack with a zero. In addition, you must create a class method that returns all the variables used in the program or nil if there are none.

Task 2. This is all about this programmability. You have to put in some Testcases which assign values to variables. You also put in some variable buttons. When you type in a 'program' using the variable buttons you might tap x enter y * 3 +. In your test case you might give values to x and y.
At the bottom of the screen you will return the values of those variables. My current screen looks like this.

The x at the very top is a label I am using to implement function keys f & g. The program is underneath and at the bottom is the values part which lists the values of any current variables. I have not implemented the parentheses removal which is part of one of the tasks. The display 'Error 0' is a piece of behaviour that I have introduced to emulate the HP11 as near as possible.

Removing excess Parentheses.
My code works through the stack until it is empty. If a particular sum is finished (because all the operators have gat their required number of operands ) and there is still stuff on the stack, I place a comma and drag the next complete sum out of the stack. Each sum is separated by a comma from the next.
As for removing parentheses. A complete calculation will always have () around it, so I just remove them like this
  1. A * surrounded by two numbers will never need brackets because multiply is the dominant operand.
  2. A complete expression does not need brackets around it.  e.g. (3+2*6) = 3+2*6
  3. A single operand operator does not need its argument bracketed if is a number. e.g. Cos (6) = Cos 6.
The code I have generated to deal with this situation is via a few methods that may be useful to others who are just learning like me.
is this a number takes a string and delivers a result telling if the answer is a number. The other one tells me if both the first and the last character of a string is a bracket.

I realise that the first one will leak memory if it weren't for the ARC in XCode 4 but I am seeking an improvement.

+ (BOOL)isThisANumber:(NSString *)candidate{
    NSNumberFormatter *fmtr = [[NSNumberFormatter alloc] init];
    [fmtr setNumberStyle:NSNumberFormatterDecimalStyle];
    NSNumber *myNumber = [fmtr numberFromString:candidate];
    if ( myNumber== nil) {
        return NO;
        return YES;

+ (BOOL) isThisBracketed:(NSString *)candidate{
    if ([candidate length] <3) {
        return NO;
    if (([candidate hasPrefix:(@"(")]) && ([candidate hasSuffix:(@")")])){
        return YES;
        return NO;

No comments:

Post a Comment