Commits

Jay Yu committed 3723472 Merge

merge

  • Participants
  • Parent commits aaa8cbf, f6ce29f

Comments (0)

Files changed (1)

File Calculator/CalculatorViewController.m

     double result = [self.brain performOperation:operation];
     self.history.text = [CalculatorBrain descriptionOfProgram:self.brain.program];
     self.display.text = [NSString stringWithFormat:@"%g", result];
-
-//    [self appendHistory:@"="];
 }
 
 - (IBAction)variablePressed:(UIButton *)sender
 {
+    if (self.userIsInTheMiddleOfEnteringANumber) {
+        [self enterPressed];
+    }
     NSString *variable = sender.currentTitle;
     [self.brain pushVariable:variable];
     self.history.text = [CalculatorBrain descriptionOfProgram:self.brain.program];
 {
     NSString *testNumber = sender.currentTitle;
 
+    if (self.userIsInTheMiddleOfEnteringANumber) {
+        [self enterPressed];
+    }
+
     if ([@"Test 1" isEqualToString:testNumber]) {
         self.testVariableValues = [NSDictionary dictionaryWithObjectsAndKeys:
-                                   [NSNumber numberWithDouble:9.23], @"x", 
-                                   [NSNumber numberWithDouble:-20], @"y", 
-                                   [NSNumber numberWithDouble:0], @"z", nil]; 
+                                   [NSNumber numberWithDouble:9.23], @"x",
+                                   [NSNumber numberWithDouble:-20], @"y",
+                                   [NSNumber numberWithDouble:0], @"z", nil];
     }
 
     if ([@"Test 2" isEqualToString:testNumber]) {
         self.testVariableValues = [NSDictionary dictionaryWithObjectsAndKeys:
-                                   [NSNumber numberWithDouble:-2.11], @"x", 
-                                   [NSNumber numberWithDouble:38.000], @"y", 
-                                   [NSNumber numberWithDouble:0.18], @"z", nil]; 
+                                   [NSNumber numberWithDouble:-2.11], @"x",
+                                   [NSNumber numberWithDouble:38.000], @"y",
+                                   [NSNumber numberWithDouble:0.18], @"z", nil];
     }
 
     if ([@"Test 3" isEqualToString:testNumber]) {
-        self.testVariableValues = nil; 
+        self.testVariableValues = nil;
+    }
+
+    [self updateDisplays];
+}
+
+- (void)updateDisplays
+{
+    [self updateDisplays:self.brain.program usingVariables:self.testVariableValues];
+}
+
+- (void)updateDisplays:(id)program
+        usingVariables:(NSDictionary *)variables
+{
+    if (! program) {
+        program = self.brain.program;
+    }
+
+    if (! variables) {
+        variables = self.testVariableValues;
+    }
+
+    double result = [CalculatorBrain runProgram:program
+                            usingVariableValues:self.testVariableValues];
+    self.history.text = [CalculatorBrain descriptionOfProgram:program];
+    self.display.text = [NSString stringWithFormat:@"%g", result];
+
+    if (! variables) {
         self.variablesDisplay.text = @"Variables not set";
-        return;
+
+    } else {
+        NSMutableArray *variableList = [[NSMutableArray alloc] init];
+        for (id variable in [CalculatorBrain variablesUsedInProgram:program]) {
+            id object = [self.testVariableValues objectForKey:variable];
+            if (! object) {
+                object = @"0";
+            }
+            [variableList addObject:[NSString stringWithFormat:@"%@ = %@", variable, object]];
+        }
+        self.variablesDisplay.text = [variableList componentsJoinedByString:@", "];
     }
-    
-    NSMutableArray *varList = [[NSMutableArray alloc] init];
-    for (id variable in [CalculatorBrain variablesUsedInProgram:self.brain.program]) {
-        [varList addObject:[NSString stringWithFormat:@"%@ = %@", variable, [self.testVariableValues objectForKey:variable]]];        
-    }
-    self.variablesDisplay.text = [varList componentsJoinedByString:@", "];
 }
 
 - (IBAction)undoPressed
         [self backspacePressed];
     }
     if (! self.userIsInTheMiddleOfEnteringANumber) {
-        
+
     }
 }
 
 - (void)viewDidUnload {
     self.history = nil;
     self.variablesDisplay = nil;
-    self.testVariableValues = nil; 
+    self.testVariableValues = nil;
     [self setVariablesDisplay:nil];
     [super viewDidUnload];
 }