Created
February 16, 2012 21:14
-
-
Save MaxGabriel/1847902 to your computer and use it in GitHub Desktop.
CS193p Fall 2011 Assignment 2--Calculator Variables
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
+ (BOOL)isOperation:(NSString *)argument | |
{ | |
NSSet *operations = [NSSet setWithObjects:@"+",@"-",@"/",@"*",@"sin",@"cos",@"sqrt",@"pi",nil]; | |
if ([operations containsObject:argument]) | |
return YES; | |
else | |
return NO; | |
} | |
//Converts variables in program to their double value | |
//Then runs popOperandOffStack | |
+ (double)runProgram:(id)program | |
usingVariableValues:(NSDictionary *)variableValues | |
{ | |
NSMutableArray *stack; | |
if ([program isKindOfClass:[NSArray class]]) { | |
stack = [program mutableCopy]; | |
} | |
for (int i=0; i<[stack count]; i++) { | |
if ([[stack objectAtIndex:i] isKindOfClass:[NSString class]] && ![CalculatorBrain isOperation:[stack objectAtIndex:i]]) { //if YES then it is a variable. | |
NSString *variable = [stack objectAtIndex:i]; | |
[stack replaceObjectAtIndex:i withObject:[variableValues objectForKey:variable]]; | |
} | |
} | |
return [self popOperandOffProgramStack:stack]; | |
} | |
//Returns set of NSString variables used in program | |
+ (NSMutableSet *)variablesUsedInProgram:(id)program | |
{ | |
NSMutableSet *set = [[NSMutableSet alloc] init]; | |
for (int i=0; i<[program count]; i++) { | |
if ([[program objectAtIndex:i] isKindOfClass:[NSString class]]) { | |
if ([CalculatorBrain isOperation:[program objectAtIndex:i]] != YES) { | |
[set addObject:[program objectAtIndex:i]]; | |
} | |
} | |
} | |
if ([set count] == 0) | |
return nil; | |
else | |
return set; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment