Calculator (CS193P)

…And per that last post, I finally have the code in the way that I want it.  I think I’ll eventually getting around to showing some screenshots of what’s happening with the memory leak, but I think I’ve gotten everything correct thus far.  I’m uploading the working code as an Xcode 4 project, without any sort of build data with it.  You may need to fire up your copy of Xcode and adjust the build settings to not use iOS 4.3 but provided the API doesn’t change too drastically, you should be good.  Grab that zip file, here.
Also, I’m leaving out the XIB files– I think you can grok your own interface design, no need to steal my binaries.  If you have any questions, I’d be happy to try and help you out but really…it’s all stuff explained in the lecture or in the Assignment 2 request sheet.

What I will do is expose a bit of code here that gave me the most frustration.  It was a class method designed to evaluate an algebraic expression by spawning a working copy of itself to sub the vales of the variables in and return a result.  It was a bit of work to get my head around abstractly, and I think the resulting code is quite efficient and nice (and doesn’t leak!).

+ (double)evaulateExpression:(id)anExpression usingVariableValues:(NSDictionary *)variables
{
double dubNum=0;
CalculatorBrain *workerBrain = [[CalculatorBrain alloc] init];
for(id item in anExpression)
{
if ([item isKindOfClass:[NSNumber class]])
{
NSNumber *workingNum =item;
[workerBrain setOperand:workingNum.doubleValue];
}
else if ([item isKindOfClass:[NSString class]])
{
NSString *workingString = item;
if ([workingString hasPrefix:VARIABLE_PREFIX]) {
NSString *subFromDict = [variables objectForKey:workingString];
[workerBrain setOperand:subFromDict.doubleValue];
}
else
{
dubNum = [workerBrain performOperation:item];
}
}
}
[workerBrain release];
return (dubNum);
}

Anyways, I’m proud enough that I’m heading on to Assignment 3 which implements a Graphing mode. Hopefully I’ll get the instruments post, as well as a bit of a video demonstration of the Calculator in action up here.

Builds are never done…

I finally finished my Stanford CS193P Assignment 2 project, which was to create a working calculator that also implemented an algebraic solver.  I technically “finished” the entire thing and was moving onto the next assignment and packing up the existing bundle to dump up here as example code.
…When I found a bug. Specifically a memory leak that with the help of Instruments I was able to determine was a pretty big freakin deal as I was hemorrhaging resources left and right when I started to call that aforementioned algebraic solver mode.  Grr, not good.  So, I’m going to work it out before I back-commit it and call the second assignment done.  Hopefully I can figure that out today…