diff options
| author | mo khan <mo@mokhan.ca> | 2013-05-20 22:14:13 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2013-05-20 22:14:13 -0600 |
| commit | 0835100a0fe864250d105a424270778e9fc065f2 (patch) | |
| tree | 52195eef33d0e32bd7b84395f22aa80bd2a6a761 | |
| parent | 1ca838a706f231ac97a3912f91f087fad2e8d864 (diff) | |
add brain and connect the dots
| -rw-r--r-- | Calculator.xcodeproj/project.pbxproj | 8 | ||||
| -rw-r--r-- | Calculator/Brain.h | 6 | ||||
| -rw-r--r-- | Calculator/Brain.m | 59 | ||||
| -rw-r--r-- | Calculator/ViewController.h | 3 | ||||
| -rw-r--r-- | Calculator/ViewController.m | 24 |
5 files changed, 99 insertions, 1 deletions
diff --git a/Calculator.xcodeproj/project.pbxproj b/Calculator.xcodeproj/project.pbxproj index a088556..25a6558 100644 --- a/Calculator.xcodeproj/project.pbxproj +++ b/Calculator.xcodeproj/project.pbxproj @@ -23,6 +23,7 @@ CD4CBEFE174B20A900B6DA7C /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD4CBED9174B20A900B6DA7C /* Foundation.framework */; }; CD4CBF06174B20A900B6DA7C /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = CD4CBF04174B20A900B6DA7C /* InfoPlist.strings */; }; CD4CBF09174B20AA00B6DA7C /* CalculatorTests.m in Sources */ = {isa = PBXBuildFile; fileRef = CD4CBF08174B20AA00B6DA7C /* CalculatorTests.m */; }; + CDAF0243174B25850088CE1A /* Brain.m in Sources */ = {isa = PBXBuildFile; fileRef = CDAF0242174B25850088CE1A /* Brain.m */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -58,6 +59,8 @@ CD4CBF05174B20A900B6DA7C /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; }; CD4CBF07174B20AA00B6DA7C /* CalculatorTests.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CalculatorTests.h; sourceTree = "<group>"; }; CD4CBF08174B20AA00B6DA7C /* CalculatorTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CalculatorTests.m; sourceTree = "<group>"; }; + CDAF0241174B25850088CE1A /* Brain.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Brain.h; sourceTree = "<group>"; }; + CDAF0242174B25850088CE1A /* Brain.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Brain.m; sourceTree = "<group>"; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -122,6 +125,8 @@ CD4CBEEF174B20A900B6DA7C /* ViewController.h */, CD4CBEF0174B20A900B6DA7C /* ViewController.m */, CD4CBEF2174B20A900B6DA7C /* ViewController.xib */, + CDAF0241174B25850088CE1A /* Brain.h */, + CDAF0242174B25850088CE1A /* Brain.m */, CD4CBEDE174B20A900B6DA7C /* Supporting Files */, ); path = Calculator; @@ -273,6 +278,7 @@ CD4CBEE4174B20A900B6DA7C /* main.m in Sources */, CD4CBEE8174B20A900B6DA7C /* AppDelegate.m in Sources */, CD4CBEF1174B20A900B6DA7C /* ViewController.m in Sources */, + CDAF0243174B25850088CE1A /* Brain.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -453,6 +459,7 @@ CD4CBF0E174B20AA00B6DA7C /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; CD4CBF0F174B20AA00B6DA7C /* Build configuration list for PBXNativeTarget "CalculatorTests" */ = { isa = XCConfigurationList; @@ -461,6 +468,7 @@ CD4CBF11174B20AA00B6DA7C /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/Calculator/Brain.h b/Calculator/Brain.h new file mode 100644 index 0000000..3babb2f --- /dev/null +++ b/Calculator/Brain.h @@ -0,0 +1,6 @@ +#import <Foundation/Foundation.h> + +@interface Brain : NSObject +-(void) addObject:(double)operand; +-(double) performOperation:(NSString *)operation; +@end diff --git a/Calculator/Brain.m b/Calculator/Brain.m new file mode 100644 index 0000000..836b4cc --- /dev/null +++ b/Calculator/Brain.m @@ -0,0 +1,59 @@ +#import "Brain.h" + +@interface Brain() +@property (strong, nonatomic) NSMutableArray * arrOperands; +@end + +@implementation Brain +-(void) addObject:(double)operand +{ + if (!self.arrOperands) + { + self.arrOperands = [[NSMutableArray alloc] init]; + } + + [self.arrOperands addObject:[NSNumber numberWithDouble:operand]]; +} + +-(double) performOperation:(NSString *)operation +{ + double result = 0; + + if (self.arrOperands.count > 1) + { + if([operation isEqualToString:@"+"]) + { + result = [self removeLastObject] + [self removeLastObject]; + } + + if ([operation isEqualToString:@"-"]) + { + result = -[self removeLastObject] + [self removeLastObject]; + } + + if ([operation isEqualToString:@"x"]) + { + result = [self removeLastObject] * [self removeLastObject]; + } + + if ([operation isEqualToString:@"/"] && [[self.arrOperands lastObject] doubleValue] != 0) + { + double tempDivisor = [self removeLastObject]; + result = [self removeLastObject] / tempDivisor; + } + } + + [self addObject:result]; + return result; +} + +-(double) removeLastObject +{ + NSNumber * lastObject = [self.arrOperands lastObject]; + if (self.arrOperands) + { + [self.arrOperands removeLastObject]; + } + return [lastObject doubleValue]; +} +@end diff --git a/Calculator/ViewController.h b/Calculator/ViewController.h index a9007f5..5686cb1 100644 --- a/Calculator/ViewController.h +++ b/Calculator/ViewController.h @@ -1,7 +1,10 @@ #import <UIKit/UIKit.h> +@class Brain; + @interface ViewController : UIViewController @property (strong, nonatomic) IBOutlet UILabel * display; +@property (strong, nonatomic) Brain * myBrain; -(IBAction) numberPressed:(id)sender; -(IBAction) operationPressed:(id)sender; -(IBAction) enterPressed:(id)sender; diff --git a/Calculator/ViewController.m b/Calculator/ViewController.m index c9a81ab..83e7c2b 100644 --- a/Calculator/ViewController.m +++ b/Calculator/ViewController.m @@ -1,7 +1,8 @@ #import "ViewController.h" +#import "Brain.h" @interface ViewController () - +@property BOOL enteringNumber; @end @implementation ViewController @@ -9,6 +10,10 @@ - (void)viewDidLoad { [super viewDidLoad]; + if (!self.myBrain) + { + self.myBrain = [[Brain alloc] init]; + } // Do any additional setup after loading the view, typically from a nib. } @@ -20,13 +25,30 @@ -(IBAction) numberPressed:(UIButton *)sender { + NSString * number = sender.currentTitle; + if (self.enteringNumber) + { + self.display.text = [self.display.text stringByAppendingString:number]; + } + else + { + self.display.text = number; + self.enteringNumber = YES; + } } -(IBAction) operationPressed:(UIButton *)sender { + if(self.enteringNumber) + { + [self enterPressed:(UIButton *)sender]; + } + self.display.text = [NSString stringWithFormat:@"%g", [self.myBrain performOperation:sender.currentTitle]]; } -(IBAction) enterPressed:(id)sender { + [self.myBrain addObject:[self.display.text doubleValue]]; + self.enteringNumber = NO; } @end |
