Created
May 4, 2012 12:47
-
-
Save magicshui/2594613 to your computer and use it in GitHub Desktop.
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
#import <Cocoa/Cocoa.h> | |
#import "CaculatorBrain.h" | |
@interface CaculatorBrainAppDelegate : NSObject <NSApplicationDelegate> | |
// the outlet of the label | |
@property (weak) IBOutlet NSTextFieldCell *display; | |
@property (assign) IBOutlet NSWindow *window; | |
@property (nonatomic) BOOL userIsInMiddleOfEntering; | |
@property (nonatomic,strong) CaculatorBrain * barin; | |
@end |
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
#import "CaculatorBrainAppDelegate.h" | |
#import "CaculatorBrain.h" | |
@implementation CaculatorBrainAppDelegate | |
// this the label target outlet | |
@synthesize display = _display; | |
// this the windows target outlet | |
@synthesize window = _window; | |
@synthesize barin=_barin; | |
@synthesize userIsInMiddleOfEntering=_userIsInMiddleOfEntering; | |
- (IBAction)enterPressed:(NSButton*)sender { | |
[self.barin pushOperand:[self.display.title doubleValue]]; | |
self.userIsInMiddleOfEntering=NO; | |
} | |
// action of the action | |
- (IBAction)operationPressed:(NSButton*)sender | |
{ | |
double result=[self.barin performOperation:sender.title]; | |
NSString* resultString =[NSString stringWithFormat:@"%@",result]; | |
self.display.title=resultString; | |
} | |
// action for the button | |
- (IBAction)digitalPressed:(NSButton*)sender | |
{ | |
NSString* digit=[sender title]; | |
if(self.userIsInMiddleOfEntering){ | |
[self.display setTitle:[self.display.title stringByAppendingString:[sender title]]]; | |
} | |
else{ | |
self.display.title=digit; | |
self.userIsInMiddleOfEntering=YES; | |
} | |
NSLog(@"digit: %@ has pressed!",digit); | |
} | |
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification | |
{ | |
// Insert code here to initialize your application | |
} | |
@end |
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
#import <Foundation/Foundation.h> | |
@interface CaculatorBrain : NSObject | |
- (void) pushOperand:(double)operand; | |
- (double) performOperation:(NSString*) operation; | |
- (double) popPerand; | |
@property (readonly) id program; | |
+ (double) ruanProgram:(id) program; | |
+ (NSString*) descriptionOfTheProgram:(id) program; | |
@end |
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
#import "CaculatorBrain.h" | |
@interface CaculatorBrain() | |
@property (nonatomic)NSMutableArray* operandStack; | |
@end | |
@implementation CaculatorBrain | |
@synthesize operandStack=_operandStack; | |
@synthesize program=_program; | |
-(NSMutableArray*) operandStack{ | |
if(_operandStack==nil){ | |
_operandStack=[[NSMutableArray alloc] init]; | |
} | |
return _operandStack; | |
} | |
- (void) pushOperand:(double)operand | |
{ | |
NSNumber* operandObject=[NSNumber numberWithDouble:operand]; | |
NSLog(@"add to stack: %@",operand); | |
[self.operandStack addObject:operandObject]; | |
} | |
- (double) performOperation:(NSString*) operation | |
{ | |
[self.operandStack addObject:operation]; | |
return [CaculatorBrain ruanProgram:self.program]; | |
} | |
-(id) program | |
{ | |
return [self.operandStack copy]; | |
} | |
+(NSString*) descriptionOfTheProgram:(id)program{ | |
return @"implement of the program"; | |
} | |
+(double)runProgram:(id)program{ | |
NSMutableArray* stack; | |
if([program isKindOfClass:[NSMutableArray class]]){ | |
stack=[program mutableCopy]; | |
} | |
return [self popOperandOfStack:stack]; | |
} | |
+(double) popOperandOfStack:(NSMutableArray*)stack{ | |
double result=0; | |
id topStack=[stack lastObject]; | |
if (topStack){ | |
[stack removeObject:topStack]; | |
} | |
if ([topStack isKindOfClass:[NSNumber class]]) | |
{ | |
result=[topStack doubleValue]; | |
} | |
if([topStack isKindOfClass:[NSString class]]) | |
{ | |
NSString* operation = topStack; | |
if([operation isEqualToString:@"+"]){ | |
result=[self popOperandOfStack:stack]+[self popOperandOfStack:stack]; | |
} | |
else if([@"-" isEqualToString:operation]) { | |
result=[self popOperandOfStack:stack]-[self popOperandOfStack:stack]; | |
} | |
else if([@"*" isEqualToString:operation]){ | |
result=[self popOperandOfStack:stack]*[self popOperandOfStack:stack]; | |
} | |
else if([@"/" isEqualToString:operation]){ | |
result=[self popOperandOfStack:stack]/[self popOperandOfStack:stack]; | |
} | |
} | |
return result; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment