gpt4 book ai didi

objective-c - iOS RPNCalculatorStanfordVidOne 无法弄清楚为什么除了一根线之外的所有线都在工作

转载 作者:行者123 更新时间:2023-11-29 04:32:46 25 4
gpt4 key购买 nike

我对 iOS 是 100% 的新手。我正在在线观看斯坦福大学的视频来学习 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

最佳答案

我认为问题在于您的 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 文件以获得更具体的代码源。希望有帮助。

关于objective-c - iOS RPNCalculatorStanfordVidOne 无法弄清楚为什么除了一根线之外的所有线都在工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11496699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com