我正在尝试学习iOS,我正在关注斯坦福大学视频,我试图重新创建一个计算器样本。但由于某种原因,=按钮不起作用或没有给出结果。我一步一步地按照练习,但根本不适合我。任何人都可以帮助我,我会非常感激。
//这是我的Calculator.h
#import <UIKit/UIKit.h>
#import "CalculatorBrain.h"
@interface ViewController : UIViewController {
IBOutlet UILabel *display;
CalculatorBrain *brain;
BOOL userIsInTheMiddleOfTypingANumber;
}
-(IBAction) digitPressed:(UIButton *) sender;
-(IBAction) operationPressed: (UIButton *) sender;
@end
<code>
//This is my Calculator.m
<pre>
#import "ViewController.h"
#import "CalculatorBrain.h"
@implementation ViewController
-(CalculatorBrain *) brain
{
if (!brain) brain = [[CalculatorBrain alloc] init];
return brain;
}
-(IBAction) digitPressed:(UIButton *) sender
{
NSString *digit = [[sender titleLabel]text];
if (userIsInTheMiddleOfTypingANumber)
{
[display setText:[[display text] stringByAppendingString:digit]];
}else{
[display setText:digit];
userIsInTheMiddleOfTypingANumber = YES;
}
}
-(IBAction) operationPressed: (UIButton *) sender
{
if (userIsInTheMiddleOfTypingANumber)
{
[[self brain] setOperand:[[display text] doubleValue]];
userIsInTheMiddleOfTypingANumber = NO;
}
NSString *operation = [[sender titleLabel] text];
double result = [[self brain] performOperation:operation];
[display setText:[NSString stringWithFormat:@"%g", result]];
}
@end
<code>
//This is my CalculatorBrain.h
<pre>
#import <Foundation/Foundation.h>
@interface CalculatorBrain : NSObject {
double operand;
NSString *waitingOperation;
double waitingOperand;
}
-(void)setOperand:(double)anOperand;
-(double)performOperation:(NSString *) operation;
@end
<code>
//This is my CalculatorBrain.m
<pre>
#import "CalculatorBrain.h"
@implementation CalculatorBrain
-(void)setOperand:(double)anOperand{
operand = anOperand;
}
-(void) performWaitingOperation{
if ([@"+" isEqual:waitingOperation])
{
operand = waitingOperand + operand;
}
else if ([@"*" isEqual:waitingOperation])
{
operand = waitingOperand - operand;
}
else if ([@"-" isEqual:waitingOperation])
{
operand = waitingOperand * operand;
}
else if([@"/" isEqual:waitingOperation])
{
if (operand)
{
operand = waitingOperand / operand;
}
}
}
-(double)performOperation:(NSString *) operation
{
if ([operation isEqual:@"sqrt"])
{
operand = sqrt(operand);
}
else if([@"*+/-=" isEqual:operation])
{
[self performWaitingOperation];
waitingOperation = operation;
waitingOperand = operand;
}
return operand;
}
@end
<code>
答案 0 :(得分:2)
除了if([@“* + / - =”isEqual:operation])catch之外,还有另外一个问题:
if ([@"*" isEqual:waitingOperation])
{
operand = waitingOperand - operand;
}
else if ([@"-" isEqual:waitingOperation])
{
operand = waitingOperand * operand;
}
逻辑错误。
接下来将performWaitingOperation函数更改为
-(double) performWaitingOperation{
if ([@"+" isEqual:waitingOperation])
{
return operand = waitingOperand + operand;
}
else if ([@"*" isEqual:waitingOperation])
{
return operand = waitingOperand * operand;
}
else if ([@"-" isEqual:waitingOperation])
{
return operand = waitingOperand - operand;
}
else if([@"/" isEqual:waitingOperation])
{
if (operand)
{
return operand = waitingOperand / operand;
}
}else if ([@"=" isEqual:waitingOperation]){
return operand;
}
}
并更改[self performWaitingOperation]; to operand = [self performWaitingOperation];
好的首发:Calculator tutorial
希望这会有所帮助..
答案 1 :(得分:1)
我认为这是这一行。 if([@“ + / - =”isEqual:operation])。除非你的字符串'operation'字面上是字符串“ + / - =”,否则永远不会评估为true。你可能想要,instad,分别与每个thos进行比较。例如if([@“*”isEqual:operation] || [@“+”isEqual:operation] ... etc
答案 2 :(得分:1)
如果您有任何问题,请参阅以下link 它一起为您提供计算器大脑项目。