私は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>