Created
July 24, 2012 18:45
-
-
Save dnaumenko/3171765 to your computer and use it in GitHub Desktop.
Recursive desribeOparandOnStack
This file contains 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
+(NSString *)describeOperandOnStack:(NSMutableArray *)stack withRootOperation:(NSString *)rootOperation { | |
NSString *result = @""; | |
id topOfStack = [stack lastObject]; | |
if (topOfStack) [stack removeLastObject]; | |
if ([topOfStack isKindOfClass:[NSNumber class]]) { | |
result = [NSString stringWithFormat:@"%@", topOfStack]; | |
} else if ([topOfStack isKindOfClass:[NSString class]]) { | |
NSString* operation = topOfStack; | |
BOOL noParenthesesNeededForSure = !rootOperation || [self isSingleOperandOperation:rootOperation]; | |
if ([operation isEqualToString:@"+"]) { | |
NSString *format = @"(%@ + %@)"; | |
if (noParenthesesNeededForSure || [@"+" isEqualToString:rootOperation] ) { | |
format = @"%@ + %@"; | |
} | |
result = [NSString stringWithFormat:format, [self describeOperandOnStack:stack withRootOperation:@"+"], [self describeOperandOnStack:stack withRootOperation:@"+"]]; | |
} else if ([operation isEqualToString:@"*"]) { | |
NSString *format = @"(%@ * %@)"; | |
if (noParenthesesNeededForSure || [@"+" isEqualToString:rootOperation] || [@"-" isEqualToString:rootOperation]) { | |
format = @"%@ * %@"; | |
} | |
result = [NSString stringWithFormat:format, [self describeOperandOnStack:stack withRootOperation:@"*"], [self describeOperandOnStack:stack withRootOperation:@"*"]]; | |
} else if ([operation isEqualToString:@"-"]) { | |
NSString *format = @"(%@ - %@)"; | |
if (noParenthesesNeededForSure) { | |
format = @"%@ - %@"; | |
} | |
NSString* subtrahend = [self describeOperandOnStack:stack withRootOperation:@"-"]; | |
result = [NSString stringWithFormat:format, [self describeOperandOnStack:stack withRootOperation:@"-"], subtrahend]; | |
} else if ([operation isEqualToString:@"/"]) { | |
NSString *format = @"(%@ / %@)"; | |
if (noParenthesesNeededForSure || [@"+" isEqualToString:rootOperation] || [@"-" isEqualToString:rootOperation]) { | |
format = @"%@ / %@"; | |
} | |
NSString* divisor = [self describeOperandOnStack:stack withRootOperation:@"/"]; | |
if (divisor) result = [NSString stringWithFormat:format, [self describeOperandOnStack:stack withRootOperation:@"/"], divisor]; | |
} else if ([operation isEqualToString:@"sin"]) { | |
result = [NSString stringWithFormat:@"sin(%@)", [self describeOperandOnStack:stack withRootOperation:@"sin"]]; | |
} else if ([operation isEqualToString:@"cos"]) { | |
result = [NSString stringWithFormat:@"cos(%@)", [self describeOperandOnStack:stack withRootOperation:@"cos"]]; | |
} else if ([operation isEqualToString:@"sqrt"]) { | |
result = [NSString stringWithFormat:@"sqrt(%@)", [self describeOperandOnStack:stack withRootOperation:@"sqrt"]]; | |
} else { | |
result = operation; | |
} | |
} | |
if ([stack count] != 0 && !rootOperation) { | |
return [NSString stringWithFormat:@"%@, %@", [self describeOperandOnStack:stack withRootOperation:nil], result]; | |
} else { | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment