gpt4 book ai didi

objective-c - Objective C 计算器编程变量

转载 作者:行者123 更新时间:2023-11-28 17:37:48 24 4
gpt4 key购买 nike

好吧,我一直在研究他们在线免费发布的斯坦福 iOS 开发类(class)。我一直在研究如何制作一个可编程变量。到目前为止,它一直运行良好,除了我认为以下代码行将变量编程为 @"x="而不是之前输入的数字。

View Controller :

#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。但是,我不知道如何解决...有什么想法吗?

最佳答案

在您的 variableChanged: 方法中,您可能需要 if (!self.userIsInTheMiddleOfEnteringANumber) 开头。注意逻辑否定,我从你的变量名的意义上推导出需要。我看不出该变量是如何设置的,所以我相信您已在应用程序的其他地方正确设置了它。

此外,将变量 xValue 更改为 ViewController 上的实例变量。它目前是一个全局静态变量。如果您创建了多个 ViewController 对象,就会遇到问题。

关于objective-c - Objective C 计算器编程变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9279356/

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