2012-02-14 58 views
1

好的,我一直在研究他們發佈的免費在線的斯坦福大學iOS開發課程。我一直在研究如何編寫可編程變量。到目前爲止,它一直在正常工作,除了我認爲以下代碼行將變量編程爲@「x =」而不是前面輸入的數字。Objective C計算器編程變量

視圖控制器:

#import "ViewController.h" 
#import "CalculatorBrain.h" 

@interface ViewController() 
@property (nonatomic) BOOL userIsInTheMiddleOfEnteringANumber; 
@property (nonatomic) BOOL userPressedSomethingElse; 
@property (nonatomic, strong) CalculatorBrain *brain; 
@end 

@implementation ViewController 
@synthesize display; 
@synthesize inputHistory; 
@synthesize userPressedSomethingElse; 
@synthesize userIsInTheMiddleOfEnteringANumber; 
@synthesize brain = _brain; 

- (CalculatorBrain *)brain 
{ 
    if (!_brain) _brain = [[CalculatorBrain alloc] init]; 
    return _brain; 
} 

NSString *xValue = @"0"; 

- (IBAction)enterPressed 
// A specific action if enter is pressed 
{ 
    [self.brain pushOperand:[self.display.text doubleValue]]; 
    self.userIsInTheMiddleOfEnteringANumber = NO; 
    if (self.userPressedSomethingElse) 
    { 
     self.inputHistory.text = [self.inputHistory.text stringByAppendingString:@" "]; 
    } 
    self.userPressedSomethingElse = NO; 
} 

- (IBAction)variableChanged:(id)sender 
{ 
    if (self.userIsInTheMiddleOfEnteringANumber) 
    { 
     [self enterPressed]; 
    } 
    NSString *operation = [sender currentTitle]; 
    xValue = [self.brain programVariable:operation]; 
    self.inputHistory.text = [self.inputHistory.text stringByAppendingString:@"X="]; 
    self.inputHistory.text = [self.inputHistory.text stringByAppendingString:xValue]; 
} 

計算器腦(在.M一個):

#import "CalculatorBrain.h" 

@interface CalculatorBrain() 
@property (nonatomic,strong) NSMutableArray *operandStack; 
@end 

@implementation CalculatorBrain 

@synthesize operandStack = _operandStack; 
- (NSMutableArray *) operandStack 
{ 
    if (!_operandStack) 
    { 
     _operandStack = [[NSMutableArray alloc] init]; 
    } 
    return _operandStack; 
} 

- (void) pushOperand:(double)operand 
{ 
    NSNumber *operandObject = [NSNumber numberWithDouble:operand]; 
    [self.operandStack addObject:operandObject]; 
} 

- (double)popOperand 
{ 
    NSNumber *operandObject = [self.operandStack lastObject]; 
    if (operandObject) [self.operandStack removeLastObject]; 
    return [operandObject doubleValue]; 
} 

- (NSString *) programVariable: (NSString *) operation 
{ 
    double result = [self popOperand]; 
    NSString *resultString = [NSString stringWithFormat:@"%.2d",result]; 
    return resultString; 
} 

的.H計算器大腦:

#import <Foundation/Foundation.h> 

@interface CalculatorBrain : NSObject 

- (void) pushOperand: (double) operand; 
- (double) performOperation: (NSString *) operation; 
- (NSString *) programVariable: (NSString *) operation; 

@end 

該推按鈕說「x =」,並且由於我添加了一些跟蹤語句,我已經發現它已被設置爲xValue。但是,我不知道如何解決它...任何想法?

+0

你寫了@ @synthesize brain = _brain;'你爲什麼還寫self.brain?你的日誌顯示了什麼? – 2012-02-14 15:38:02

+0

日誌行?那些是什麼? – BrainInaJar1245 2012-02-16 15:30:26

+0

我的意思是你的追蹤報表。他們展示了什麼? – 2012-02-16 15:46:39

回答

0

在您的variableChanged:方法中,您可能需要在開始時使用if (!self.userIsInTheMiddleOfEnteringANumber)。注意邏輯否定,我從變量名的意義上推導出需求。我看不出該變量是如何設置的,所以我相信你在應用程序的其他地方正確設置了它。

另外,將變量xValue更改爲ViewController上的實例變量。它目前是一個全局靜態變量。如果創建了多個ViewController對象,則會出現問題。

+0

不,因爲我這樣做的方式會截斷他們輸入的任何數字,所以它不會將其處理爲3.5x = 8,只有x = 3.5。 – BrainInaJar1245 2012-02-15 14:58:22