Commits

Jay Yu committed ff068a1

work in progress: descriptionOfProgram

Comments (0)

Files changed (4)

Calculator.xcodeproj/project.xcworkspace/xcuserdata/jay.xcuserdatad/UserInterfaceState.xcuserstate

Binary file modified.

Calculator/CalculatorBrain.h

 - (double)performOperation:(NSString *)operation;
 - (void)clearStates;
 
+// New in Assignment 2
 @property (readonly) id program;
-
 + (double)runProgram:(id)program;
 + (NSString *)descriptionOfProgram:(id)program;
 

Calculator/CalculatorBrain.m

 @end
 
 @implementation CalculatorBrain
-
 @synthesize programStack = _programStack;
 
 - (NSMutableArray *)programStack
 
 }
 
++ (BOOL)isHigherPriority:(NSString *)operator1:(NSString *)operator2
+{
+    NSLog(@"Comparing: %@, %@", operator1, operator2);
+    if (!operator1 || !operator2) {
+        return NO;
+    }
+
+    NSDictionary *lookUpTable = [[NSDictionary alloc] initWithObjectsAndKeys:
+                         [NSNumber numberWithInt:100], @"sqrt",
+                         [NSNumber numberWithInt:100], @"tan",
+                         [NSNumber numberWithInt:100], @"cos",
+                         [NSNumber numberWithInt:100], @"sin",
+                         [NSNumber numberWithInt:90], @"*",
+                         [NSNumber numberWithInt:90], @"/",
+                         [NSNumber numberWithInt:10], @"+",
+                         [NSNumber numberWithInt:10], @"-",
+                         nil];
+
+    if ([[lookUpTable valueForKey:operator1] intValue] > [[lookUpTable valueForKey:operator2] intValue]) {
+        return YES;
+    }
+    return NO;
+}
+
++ (NSString *)lookAheadOperationInStack:(NSMutableArray *)stack
+{
+    id topOfStack = [stack lastObject];
+    while (topOfStack) {
+        [stack removeLastObject];
+        if ([topOfStack isKindOfClass:[NSString class]]) {
+            return topOfStack;
+        }
+        topOfStack = [stack lastObject];
+    }
+    return nil;
+}
+
 + (NSString *)programStringOffStack:(NSMutableArray *)stack
 {
     NSString *result = nil;
+    NSString *nextOperation = nil;
     id topOfStack = [stack lastObject];
     if (topOfStack) {
         [stack removeLastObject];
     if ([topOfStack isKindOfClass:[NSNumber class]]) {
         result = [NSString stringWithFormat:@"%g", [topOfStack doubleValue]];
 
+
     } else if ([topOfStack isKindOfClass:[NSString class]]) {
         NSString *operation = topOfStack;
-        if ([operation isEqualToString:@"+"]) {
-            result = [NSString stringWithFormat:@"(%g + %g)", [self programStringOffStack:stack], [self programStringOffStack:stack]];
-        } else if ([@"*" isEqualToString:operation]) {
-            result = [NSString stringWithFormat:@"(%g * %g)", [self programStringOffStack:stack], [self programStringOffStack:stack]];
-        } else if ([@"-" isEqualToString:operation]) {
-            double subtrahend = [self programStringOffStack:stack];
-            result = [self popOperandOffStack:stack] - subtrahend;
+        nextOperation = [self lookAheadOperationInStack:[stack mutableCopy]];
+        NSLog(@"programStringOffStack: next operation: %@", nextOperation);
+        BOOL useBracket = NO;
+        if (  [self isHigherPriority:operation:nextOperation]) {
+            useBracket = YES;
+        }
+
+        if ([operation isEqualToString:@"+"] || [@"-" isEqualToString:operation] || [@"*" isEqualToString:operation]) {
+            NSString *operandRight = [self programStringOffStack:stack];
+            NSString *format = @"(%@ %@ %@)";
+            if (false && useBracket) {
+                format = @"%@ %@ %@";
+            }
+            result = [NSString stringWithFormat:format, [self programStringOffStack:stack], operation, operandRight];
+
         } else if ([@"/" isEqualToString:operation]) {
-            double divisor = [self popOperandOffStack:stack];
-            if (divisor) {
-                result = [self popOperandOffStack:stack] / divisor;
+            NSString *divisor = [self programStringOffStack:stack];
+            if ([divisor isEqualToString:@"0"] || [divisor isEqualToString:@"-0"]) {
+                result = [NSString stringWithFormat:@"(%@ / %@)", [self programStringOffStack:stack], divisor];
+            } else {
+                result = @"NaN";  // NaN means Not A Number
             }
         } else if ([@"sin" isEqualToString:operation]) {
-            result = sin([self popOperandOffStack:stack]);
+            result = [NSString stringWithFormat:@"sin(%@)", [self programStringOffStack:stack]];
+
         } else if ([@"cos" isEqualToString:operation]) {
-            result = cos([self popOperandOffStack:stack]);
+            result = [NSString stringWithFormat:@"cos√(%@)", [self programStringOffStack:stack]];
+
         } else if ([@"sqrt" isEqualToString:operation]) {
-            double operand = [self popOperandOffStack:stack];
-            if (operand < 0) {
-                result = 0;
-            } else {
-                result = sqrt(operand);
-            }
+            result = [NSString stringWithFormat:@"√(%@)", [self programStringOffStack:stack]];
+
         } else if ([@"π" isEqualToString:operation]) {
-            result = M_PI;
+            result = @"π";
+
         } else if ([@"+/-" isEqualToString:operation]) {
-            result = [self popOperandOffStack:stack];
-            if (result != 0) {
-                result = result * -1;
-            }
+            result = [NSString stringWithFormat:@"±(%@)", [self programStringOffStack:stack]];
+
         }
     }
+
+    NSLog(@"programStringOffStack: %@, next operation: %@", result, nextOperation);
     return result;
 
 }

Calculator/CalculatorViewController.m

     if ([aString isEqualToString:@"="]) {
         // Extra Credit #2: put an "=" at the end of text label
         // Remove extraneous "=" already in history
-        self.history.text = [self.history.text stringByReplacingOccurrencesOfString:@" = "
-                                                                         withString:@" "];
+        self.history.text = [self.history.text stringByReplacingOccurrencesOfString:@" ="
+                                                                         withString:@""];
     }
     self.history.text = [self.history.text stringByAppendingString:aString];
 
 - (IBAction)enterPressed
 {
     [self.brain pushOperand:[self.display.text doubleValue]];
-    [self appendHistory:self.display.text];
+    self.history.text = [CalculatorBrain descriptionOfProgram:self.brain.program];
     self.userIsInTheMiddleOfEnteringANumber = NO;
 }
 
     }
     NSString *operation = sender.currentTitle;
     double result = [self.brain performOperation:operation];
-    [self appendHistory:operation];
+    self.history.text = [CalculatorBrain descriptionOfProgram:self.brain.program];
     self.display.text = [NSString stringWithFormat:@"%g", result];
 
-    [self appendHistory:@"="];
+//    [self appendHistory:@"="];
 }
 
 - (IBAction)allClear