Created
December 6, 2011 19:32
-
-
Save ichoosetoaccept/1439581 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Assignment 2, Problem 2 | |
| + (NSString *)descriptionOfProgram:(id)program | |
| { | |
| NSMutableArray *stack; | |
| if ([program isMemberOfClass:[NSArray class]]) | |
| { | |
| stack = [program mutableCopy]; | |
| } | |
| return [self descriptionOfTopOfStack:stack]; | |
| } | |
| + (NSString *)descriptionOfTopOfStack:(NSMutableArray *)stack | |
| { | |
| // recursive code to describe each object on the program stack goes here | |
| NSString *descriptionOfTopOfStack; | |
| NSString *operation; | |
| id topOfStack = [stack lastObject]; | |
| if (topOfStack) [stack removeLastObject]; | |
| if ([topOfStack isKindOfClass:[NSNumber class]]) | |
| { | |
| descriptionOfTopOfStack = [topOfStack stringValue]; | |
| } | |
| else if ([topOfStack isKindOfClass:[NSString class]]) | |
| { | |
| if ([self isOperation:topOfStack]) | |
| { | |
| operation = [topOfStack stringValue]; | |
| if ([[self isKindOfOperation:topOfStack] isEqualToString:@"two-operand operation"]) | |
| { | |
| // should display all multi-operand operations using “infix” notation if appropriate, else function notation. For example, 3 Enter 5 + should display as 3 + 5. | |
| descriptionOfTopOfStack = [[[self descriptionOfTopOfStack:stack] stringByAppendingString:operation] stringByAppendingString:[self descriptionOfTopOfStack:stack]]; | |
| } | |
| else if ([[self isKindOfOperation:topOfStack] isEqualToString:@"single-operand operation"]) | |
| { | |
| // should display all single-operand operations using “function” notation. For example, 10 sqrt should display as sqrt(10). | |
| descriptionOfTopOfStack = [[[operation stringByAppendingString:@"("] stringByAppendingString:[self descriptionOfTopOfStack:stack]] stringByAppendingString:@")"]; | |
| } | |
| else if ([[self isKindOfOperation:topOfStack] isEqualToString:@"no-operand operation"]) | |
| { | |
| // no-operand operations, like π, should appear unadorned. For example, π. | |
| descriptionOfTopOfStack = [topOfStack stringValue]; | |
| } | |
| } | |
| else // topOfStack must be a variable | |
| { | |
| // Variables (Required Task #1) should also appear unadorned. For example, x. | |
| descriptionOfTopOfStack = [topOfStack stringValue]; | |
| } | |
| } | |
| return descriptionOfTopOfStack; | |
| } | |
| + (BOOL)isOperation:(NSString *)operation | |
| { | |
| if ([operation isEqualToString:@"+"] || | |
| [operation isEqualToString:@"-"] || | |
| [operation isEqualToString:@"*"] || | |
| [operation isEqualToString:@"/"] || | |
| [operation isEqualToString:@"sin"] || | |
| [operation isEqualToString:@"cos"] || | |
| [operation isEqualToString:@"sqrt"] || | |
| [operation isEqualToString:@"π"]) | |
| { | |
| return YES; | |
| } | |
| else return NO; | |
| } | |
| + (NSString *)isKindOfOperation:(NSString *)operation | |
| { | |
| NSString *kindOfOperation = @"no operation"; | |
| // code to determine whether a given string on the stack is... | |
| if ([self isOperation:operation]) | |
| { | |
| kindOfOperation = @"no-operand operation"; | |
| if ([operation isEqualToString:@"+"] || | |
| [operation isEqualToString:@"-"] || | |
| [operation isEqualToString:@"*"] || | |
| [operation isEqualToString:@"/"]) | |
| { | |
| kindOfOperation = @"two-operand operation"; | |
| } | |
| else if ([operation isEqualToString:@"sin"] || | |
| [operation isEqualToString:@"cos"] || | |
| [operation isEqualToString:@"sqrt"]) | |
| { | |
| kindOfOperation = @"single-operand operation"; | |
| } | |
| } | |
| return kindOfOperation; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment