2012-02-14 11 views
1

私はスタンフォードiOS開発コースで無料でオンラインで投稿しています。私はプログラム可能な変数を作る方法を考え出すことに取り組んできました。これまでのところ、コードの次の行は、入力された前の数値の代わりに@ "x ="になるように変数をプログラミングしていると思います。目的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 1):

#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を書いていますか?あなたのログラインには何が表示されますか? –

+0

ログライン?それらは何ですか? – BrainInaJar1245

+0

あなたのトレース文を意味します。彼らは何を示していますか? –

答えて

0

variableChanged:メソッドでは、最初にif (!self.userIsInTheMiddleOfEnteringANumber)が必要です。論理否定に注意してください。私は変数名の意味から必要性を導き出します。私はその変数が設定されている方法を参照していないので、あなたのアプリケーションのどこかに適切に設定していると思います。

また、変数xValueViewControllerのインスタンス変数に変更します。これは現在、グローバルな静的変数です。 1つ以上のViewControllerオブジェクトが作成されている場合、問題が発生します。

+0

いいえ、私のやり方は入力した番号を切り捨てるので、3.5x = 8、x = 3.5だけしか処理しません。 – BrainInaJar1245