iOS RPNCalculatorStanfordVidOne无法弄清楚为什么只有一条线路正常工作

时间:2012-07-16 00:06:22

标签: objective-c ios xcode cs193p

我是iOS的全新品牌。 我在线观看斯坦福大学的视频,了解iOS。我花了几个小时慢慢地仔细浏览第二个视频,确保我没有在一行代码上蠢蠢欲动。一切都很完美,直到 - 你不知道 - 最后一分钟的编码。

我和我的妻子花了很多时间试图解决这个问题,但我的代码与教授的最佳匹配。请帮我。我很喜欢通过这些视频学习,但我不能继续前进,直到我有这个功能,并花时间来审查它。

#import "CalculatorViewController.h"
#import "CalculatorBrain.h"

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

@implementation CalculatorViewController

@synthesize display  = _display;
@synthesize userIsInTheMiddleOfEnteringANumber = 
_userIsInTheMiddleOfEnteringANumber;
@synthesize brain = _brain;

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

- (IBAction)digitPressed:(UIButton *)sender 
{
    NSString *digit = sender.currentTitle;
        if (self.userIsInTheMiddleOfEnteringANumber) 
        {
             self.display.text = [self.display.text stringByAppendingString:digit];
        }
    else 
    {
        self.display.text = digit;
        self.userIsInTheMiddleOfEnteringANumber = YES;
    }
}
- (IBAction)enterPressed 
{
    [self.brain pushOperand:[self.display.text doubleValue]];
    self.userIsInTheMiddleOfEnteringANumber = NO;
}

- (IBAction)operationPressed:(id)sender 
{
    if (self.userIsInTheMiddleOfEnteringANumber) [self enterPressed];
    double result = [self.brain performOperation:sender.currentTitle];  // <- HERE IS THE       LINE IN QUESTION

    NSString *resultString = [NSString stringWithFormat:@"%g", result];
    self.display.text = resultString;
}

@end

CalculatorBrain.m


#import "CalculatorBrain.h"

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

@implementation CalculatorBrain

@synthesize operandStack = _operandStack;

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

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

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

- (double)performOperation:(NSString *)operation
{
    double result = 0;

    if ([operation isEqualToString:@"+"])
    {
        result = [self popOperand] + [self popOperand];
    }
    else if ([@"*" isEqualToString:operation])
    {
        result = [self popOperand] * [self popOperand];
    }

    [self pushOperand:result];

    return result;
}

@end

1 个答案:

答案 0 :(得分:1)

我认为问题在于你的if语句没有正确关闭。

- (IBAction)operationPressed:(id)sender 
{
    if (self.userIsInTheMiddleOfEnteringANumber){ [self enterPressed];
    double result = [self.brain performOperation:sender.currentTitle];  

    NSString *resultString = [NSString stringWithFormat:@"%g", result];
    self.display.text = resultString;
}
}

- (IBAction)operationPressed:(id)sender 
    {
        if (self.userIsInTheMiddleOfEnteringANumber)
{
 [self enterPressed];
}
        double result = [self.brain performOperation:sender.currentTitle]; 

        NSString *resultString = [NSString stringWithFormat:@"%g", result];
        self.display.text = resultString;

    }

建议查看教程的pdf文件以获得更具体的代码源。希望有所帮助。