Permalink
Browse files

Homework 2

  • Loading branch information...
ciudilo committed Dec 26, 2011
1 parent d982a8d commit 90cba39bdcafa88da12342f590c38a226be51840
@@ -12,12 +12,15 @@
@interface CalculatorBrain : NSObject
- (void)pushOperand:(double)operand;
+- (void)pushVariable:(NSString*)variable;
- (double)performOperation:(NSString*)operation;
- (void)clearBrain;
@property (readonly) id program;
+ (NSString *)descriptionOfProgram:(id)program;
+ (double)runProgram:(id)program;
++ (double)runProgram:(id)program usingVariableValues:(NSDictionary *)variableValues;
++ (NSSet *)variablesUsedInProgram:(id)program;
@end
@@ -10,6 +10,8 @@
@interface CalculatorBrain()
@property (nonatomic, strong) NSMutableArray *programStack;
++ (NSString *)descriptionOfTopOfStack:(NSMutableArray *)program;
++ (BOOL)isOperation:(NSString *)operation;
@end
@implementation CalculatorBrain
@@ -26,20 +28,77 @@ - (id)program
return [self.programStack copy];
}
+- (void)pushOperand:(double)operand;
+{
+ [self.programStack addObject:[NSNumber numberWithDouble:operand]];
+}
+
+- (void)pushVariable:(NSString*)variable
+{
+ [self.programStack addObject:variable];
+}
+
+ (NSString *)descriptionOfProgram:(id)program
{
- return @"Implement this in Homework #2";
+ NSMutableArray *stack;
+ if ([program isKindOfClass:[NSArray class]]) {
+ stack = [program mutableCopy];
+ }
+
+ NSString *result = [self descriptionOfTopOfStack:stack];
+ result = [result stringByAppendingString:@" "];
+
+ if ([stack count] > 0)
+ {
+ result = [result stringByAppendingString:[self descriptionOfProgram:stack]];
+ }
+
+ return result;
}
-- (void)pushOperand:(double)operand
++ (BOOL)isOperation:(NSString *)operation
{
- [self.programStack addObject:[NSNumber numberWithDouble:operand]];
+
+
+ return YES;
+}
+
++ (NSString *)descriptionOfTopOfStack:(NSMutableArray *)program
+{
+ NSString *result;
+
+ NSSet *twoOperandOps = [[NSSet alloc] initWithObjects:@"+", @"-", @"*", @"/", nil];
+ NSSet *singleOperandOps = [[NSSet alloc] initWithObjects:@"sqrt", @"cos", @"sin", nil];
+
+ id topOfStack = [program lastObject];
+ if (topOfStack) [program removeLastObject];
+
+ if ( [twoOperandOps containsObject:topOfStack])
+ {
+ NSString *operand = [NSString stringWithFormat:@"%@", topOfStack];
+ NSString *operation = [NSString stringWithFormat:@"%@ %@ %@",
+ [self descriptionOfTopOfStack:program], operand, [self descriptionOfTopOfStack:program]];
+ result = [NSString stringWithFormat:@"%@%@%@", @"(", operation, @")"];
+ }
+ else if ([singleOperandOps containsObject:topOfStack])
+ {
+ NSString *operand = [NSString stringWithFormat:@"%@", topOfStack];
+ result = [operand stringByAppendingFormat:@"%@%@%@",
+ @"(", [self descriptionOfTopOfStack:program], @")"]; }
+ else
+ {
+ NSLog(@"Top of the stack: %@", topOfStack);
+
+ result = [NSString stringWithFormat:@"%@", topOfStack];
+ }
+
+ return result;
}
- (double)performOperation:(NSString *)operation
{
[self.programStack addObject:operation];
- return [[self class] runProgram:self.program];
+ return [CalculatorBrain runProgram:self.program];
}
+ (double)popOperandOffProgramStack:(NSMutableArray *)stack
@@ -59,6 +118,7 @@ + (double)popOperandOffProgramStack:(NSMutableArray *)stack
if ([operation isEqualToString:@"+"])
{
+ NSLog(@"Operation element on the stack.");
result = [self popOperandOffProgramStack:stack] + [self popOperandOffProgramStack:stack];
}
else if ([operation isEqualToString:@"*"])
@@ -68,16 +128,12 @@ + (double)popOperandOffProgramStack:(NSMutableArray *)stack
else if ([operation isEqualToString:@"/"])
{
double firstOperand = [self popOperandOffProgramStack:stack];
- double secondOperan = [self popOperandOffProgramStack:stack];
-
- result = secondOperan / firstOperand;
+ result = [self popOperandOffProgramStack:stack] / firstOperand;
}
else if ([operation isEqualToString:@"-"])
{
double firstOperand = [self popOperandOffProgramStack:stack];
- double secondOperand = [self popOperandOffProgramStack:stack];
-
- result = secondOperand - firstOperand;
+ result = [self popOperandOffProgramStack:stack] - firstOperand;
}
else if ([operation isEqualToString:@"sin"])
{
@@ -97,6 +153,8 @@ + (double)popOperandOffProgramStack:(NSMutableArray *)stack
}
}
+ NSLog(@"Pesult pop top of the stack: %g", result);
+
return result;
}
@@ -109,6 +167,71 @@ + (double)runProgram:(id)program
return [self popOperandOffProgramStack:stack];
}
++ (double)runProgram:(id)program usingVariableValues:(NSDictionary *)variableValues;
+{
+ NSMutableArray *stack;
+ if ([program isKindOfClass:[NSArray class]]) {
+ stack = [program mutableCopy];
+ }
+
+ NSSet *variables = [CalculatorBrain variablesUsedInProgram:stack];
+
+ NSLog(@"Varaibles set size: %d", [variables count]);
+ NSLog(@"Passed in varaibles dict size: %d", [variableValues count]);
+
+ for (int i = 0; i < [stack count]; i++) {
+ NSObject *operand = [stack objectAtIndex:i];
+ if( [operand isKindOfClass:[NSString class]] && [variables containsObject:operand])
+ {
+ NSNumber *varValue = [variableValues objectForKey:operand];
+ if (varValue != Nil)
+ {
+ NSLog(@"Replacing variable %@ with: %@", operand, varValue);
+ [stack replaceObjectAtIndex:i withObject:varValue];
+ }
+ else
+ {
+ [stack replaceObjectAtIndex:i withObject:[NSNumber numberWithDouble:0]];
+ }
+ }
+ }
+
+
+ return [CalculatorBrain runProgram:stack];
+}
+
++ (NSSet *)variablesUsedInProgram:(id)program
+{
+ NSSet *twoOperandOps = [[NSSet alloc] initWithObjects:@"+", @"-", @"*", @"/", nil];
+ NSSet *singleOperandOps = [[NSSet alloc] initWithObjects:@"sqrt", @"cos", @"sin", nil];
+
+ NSMutableArray *stack;
+ if ([program isKindOfClass:[NSArray class]]) {
+ stack = [program mutableCopy];
+ }
+
+ NSMutableArray *variables = [[NSMutableArray alloc] init];
+ for (NSString *string in stack)
+ {
+ if( ![string doubleValue] && ![twoOperandOps member:string] && ![singleOperandOps member:string]
+ && ![@"π" isEqualToString:string]) // if opernad is not a number and not operation
+ {
+ [variables addObject:string];
+ NSLog(@"Added variable to the list: %@", string);
+ }
+ }
+
+ if ([variables count] > 0) {
+ return [[NSSet alloc] initWithArray:variables];
+ }
+ else
+ {
+ return Nil;
+ }
+
+
+}
+
- (void) clearBrain
{
[self.programStack removeAllObjects];
@@ -18,7 +18,11 @@
- (IBAction)operationPressed:(UIButton*)sender;
+- (IBAction)variablePressed:(UIButton*)sender;
+
- (IBAction)enterPressed;
- (IBAction)clearPressed;
+
+- (IBAction)testButtonPressed:(UIButton*)sender;
@end
@@ -12,6 +12,7 @@
@interface CalculatorViewController()
@property (nonatomic) BOOL userInTheMiddleOfTypingANumber;
@property (nonatomic, strong) CalculatorBrain *brain;
+@property (nonatomic, strong) NSDictionary *testVariableValues;
@end
@implementation CalculatorViewController
@@ -20,20 +21,31 @@ @implementation CalculatorViewController
@synthesize inputLog = _inputLog;
@synthesize userInTheMiddleOfTypingANumber = _userInTheMiddleOfTypingANumber;
@synthesize brain = _brain;
+@synthesize testVariableValues = _testVariableValues;
- (CalculatorBrain *)brain
{
if (!_brain) _brain = [[CalculatorBrain alloc] init];
return _brain;
}
+- (NSDictionary *)testVariableValues
+{
+ if (!_testVariableValues) _testVariableValues = [NSDictionary dictionaryWithObjectsAndKeys:
+ [NSNumber numberWithDouble:33], @"x",
+ [NSNumber numberWithDouble:55], @"a",
+ [NSNumber numberWithDouble:77], @"b",
+ nil];
+ return _testVariableValues;
+}
+
- (IBAction)digitPressed:(UIButton *)sender
{
NSString *digit = sender.currentTitle;
if (self.userInTheMiddleOfTypingANumber)
{
- //makes sure that if user presses . digit more then once consecutive presses are ignored
+ //makes sure that if user presses '.' digit more then once, consecutive presses are ignored
if ( [digit isEqualToString:@"."] && [self.display.text rangeOfString:@"."].location == NSNotFound)
{
self.display.text = [self.display.text stringByAppendingString:digit];
@@ -55,29 +67,50 @@ - (IBAction)operationPressed:(UIButton *)sender
{
if (self.userInTheMiddleOfTypingANumber) [self enterPressed];
- self.inputLog.text = [self.inputLog.text stringByAppendingString:@" "];
- self.inputLog.text = [self.inputLog.text stringByAppendingString:sender.currentTitle];
- self.inputLog.text = [self.inputLog.text stringByAppendingString:@" "];
-
double result = [self.brain performOperation:sender.currentTitle];
+
+ self.inputLog.text = [CalculatorBrain descriptionOfProgram:self.brain.program];
+
NSString *resultString = [NSString stringWithFormat:@"%g", result];
self.display.text = resultString;
}
- (IBAction)enterPressed
{
- self.inputLog.text = [self.inputLog.text stringByAppendingString:@" "];
- self.inputLog.text = [self.inputLog.text stringByAppendingString:self.display.text];
-
[self.brain pushOperand:[self.display.text doubleValue]];
+
+ self.inputLog.text = [CalculatorBrain descriptionOfProgram:self.brain.program];
+
self.userInTheMiddleOfTypingANumber = NO;
}
+- (IBAction)variablePressed:(UIButton*)sender
+{
+ if (self.userInTheMiddleOfTypingANumber) [self enterPressed];
+
+ [self.brain pushVariable:[sender currentTitle]];
+
+ self.inputLog.text = [CalculatorBrain descriptionOfProgram:self.brain.program];
+
+ self.display.text = [sender currentTitle];
+}
+
- (IBAction)clearPressed
{
self.inputLog.text = @"";
self.display.text = @"";
[self.brain clearBrain];
}
+- (IBAction)testButtonPressed:(UIButton *)sender
+{
+ if (self.userInTheMiddleOfTypingANumber) [self enterPressed];
+
+ double result = [CalculatorBrain runProgram:self.brain.program usingVariableValues:self.testVariableValues];
+ self.inputLog.text = [CalculatorBrain descriptionOfProgram:self.brain.program];
+
+ NSString *resultString = [NSString stringWithFormat:@"%g", result];
+ self.display.text = resultString;
+}
+
@end
Oops, something went wrong.

0 comments on commit 90cba39

Please sign in to comment.