1. Jay Yu
  2. CS193P Assignment 1

Commits

Jay Yu  committed 5d96ca0

incorporate classmates' feedback from piazza @965.
fixed a bug that allow 2 decimal points to be entered
implemented +/- button - UNTESTED!

  • Participants
  • Parent commits 4c24348
  • Branches default

Comments (0)

Files changed (4)

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

Binary file modified.

File Calculator/CalculatorBrain.m

View file
         result = sqrt([self popOperand]);
     } else if ([@"π" isEqualToString:operation]) {
         result = M_PI;
+    } else if ([@"+/-" isEqualToString:operation]) {
+        result = [self popOperand] * -1;
     }
 
     [self pushOperand:result];
 
 - (void)clearStates
 {
-    [self setOperandStack:[[NSMutableArray alloc] init]];
+    [self.operandStack removeAllObjects];
 }
 
 @end

File Calculator/CalculatorViewController.m

View file
 @interface CalculatorViewController ()
 @property (nonatomic, strong) CalculatorBrain *brain;
 @property (nonatomic) BOOL userIsInTheMiddleOfEnteringANumber;
-@property (nonatomic) BOOL userHasEnteredDot;
 @end
 
 @implementation CalculatorViewController
 @synthesize display = _display;
 @synthesize history = _history;
 @synthesize userIsInTheMiddleOfEnteringANumber = _userIsInTheMiddleOfEnteringANumber;
-@synthesize userHasEnteredDot = _userHasEnteredDot;
 
 - (CalculatorBrain *)brain
 {
 {
     NSString *digit = [sender currentTitle];
     if ([digit isEqualToString:@"."]) {
-        if (self.userHasEnteredDot) {
-            return;
-        }
         if (! self.userIsInTheMiddleOfEnteringANumber) {
-            // shortcut for user: start entering an operand with "." will automatically
-            // enter "0."
+            // shortcut for user: start entering an operand with "." will
+            // automatically enter "0."
             self.display.text = @"0.";
             self.userIsInTheMiddleOfEnteringANumber = YES;
             return;
         }
-        self.userHasEnteredDot = YES;
+
+        NSRange range = [self.display.text rangeOfString:@"."];
+        if (range.location == NSNotFound) {
+            self.display.text = [self.display.text stringByAppendingString:digit];
+        } else {
+            return;
+        }
     }
 
     if (self.userIsInTheMiddleOfEnteringANumber) {
 
 - (IBAction)enterPressed
 {
-    NSString *dt = self.display.text;
-    [self.brain pushOperand:[dt doubleValue]];
-    [self appendHistory:dt];
+    [self.brain pushOperand:[self.display.text doubleValue]];
+    [self appendHistory:self.display.text];
     self.userIsInTheMiddleOfEnteringANumber = NO;
-    self.userHasEnteredDot = NO;
 }
 
 - (IBAction)operationPressed:(UIButton *)sender
     self.display.text = @"0";
     self.history.text = @"";
     self.userIsInTheMiddleOfEnteringANumber = NO;
-    self.userHasEnteredDot = NO;
 }
 
 - (IBAction)backspacePressed
         NSUInteger length = [self.display.text length];
         if (length > 0) {
             self.display.text = [self.display.text substringToIndex:length - 1];
-            // Check if a decimal dot is still there
-            // Based on Hint #1, Assignment 1
-            NSRange range = [self.display.text rangeOfString:@"."];
-            if (range.location == NSNotFound) {
-                self.userHasEnteredDot = NO;
-            } else {
-                self.userHasEnteredDot = YES;
-            }
         }
         if (length == 0) {
             self.userIsInTheMiddleOfEnteringANumber = NO;
     }
 }
 
+- (IBAction)invertSignPressed
+{
+    if (self.userIsInTheMiddleOfEnteringANumber) {
+        double result = [self.display.text doubleValue] * -1;
+        self.display.text = [NSString stringWithFormat:@"%g", result];
+    } else {
+        // implement +/- as an operation
+        // FIXME: reduce duplicate code in here and operationPressed
+        if (self.userIsInTheMiddleOfEnteringANumber) {
+            [self enterPressed];
+        }
+        NSString *operation = @"+/-";
+        double result = [self.brain performOperation:operation];
+        self.display.text = [NSString stringWithFormat:@"%g", result];        
+    }
+}
+
 - (void)viewDidUnload {
     [self setHistory:nil];
     [super viewDidUnload];

File Calculator/en.lproj/MainStoryboard.storyboard

View file
                                     <action selector="backspacePressed" destination="2" eventType="touchUpInside" id="v4k-Mb-mv2"/>
                                 </connections>
                             </button>
+                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="c7h-b2-JmJ">
+                                <rect key="frame" x="20" y="329" width="64" height="40"/>
+                                <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
+                                <fontDescription key="fontDescription" type="boldSystem" pointSize="15"/>
+                                <state key="normal" title="+/-">
+                                    <color key="titleColor" red="0.19607843459999999" green="0.30980393290000002" blue="0.52156865600000002" alpha="1" colorSpace="calibratedRGB"/>
+                                    <color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
+                                </state>
+                                <state key="highlighted">
+                                    <color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
+                                </state>
+                                <connections>
+                                    <action selector="invertSignPressed" destination="2" eventType="touchUpInside" id="ZK3-iW-jHb"/>
+                                </connections>
+                            </button>
                         </subviews>
                         <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
                     </view>
             <point key="canvasLocation" x="175" y="72"/>
         </scene>
     </scenes>
-    <classes>
-        <class className="CalculatorViewController" superclassName="UIViewController">
-            <source key="sourceIdentifier" type="project" relativePath="./Classes/CalculatorViewController.h"/>
-            <relationships>
-                <relationship kind="outlet" name="display" candidateClass="UILabel"/>
-                <relationship kind="outlet" name="history" candidateClass="UILabel"/>
-            </relationships>
-        </class>
-    </classes>
     <simulatedMetricsContainer key="defaultSimulatedMetrics">
         <simulatedStatusBarMetrics key="statusBar"/>
         <simulatedOrientationMetrics key="orientation"/>