iTunes University (スタンフォード) で Objective-C を学んでいます。私はsinボタンをプログラミングしていますが、解析の問題が発生しています:
この行に期待される識別子 "double myDegrees = [self.operandStack];"。
CalculatorBrain.h
#import <Foundation/Foundation.h>
@interface CalculatorBrain : NSObject
-(void)pushOperand:(double)operand;
-(double)performOperation:(NSString *)operation;
@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 ([operation isEqualToString:@"*"])
{
result = [self popOperand] * [self popOperand];
} else if ([operation isEqualToString:@"/"])
{
result = [self popOperand] / [self popOperand];
} else if ([operation isEqualToString:@"-"])
{
result = [self popOperand] - [self popOperand];
} else if ([operation isEqualToString:@"C"])
{
[self.operandStack removeAllObjects];
result = 0;
} else if ([operation isEqualToString:@"sin"])
{
double myDegrees = [self.operandStack];
NSLog(@"degrees are %f", myDegrees);
double myRadian = myDegrees * M_1_PI/180;
NSLog(@"radian is %f", myRadian);
result = myRadian;
}else if ([operation isEqualToString:@"cos"])
{
result = cos([self popOperand]);
}else if ([operation isEqualToString:@"sqrt"])
{
result = sqrt([self popOperand]);
}
[self pushOperand:result];
return result;
}
@end
CalculatorViewController.h
#import <UIKit/UIKit.h>
#import <math.h>
@interface CalculatorViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *display;
@property (weak, nonatomic) IBOutlet UILabel *history;
@end
CalculatorViewController.m
#import "CalculatorViewController.h"
#import "CalculatorBrain.h"
@interface CalculatorViewController()
@property (nonatomic) BOOL userIsInTheMiddleOfEnteringANumber;
@property (nonatomic, strong) CalculatorBrain *brain;
@end
@implementation CalculatorViewController
@synthesize display = _display;
@synthesize history = _history;
@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 = [self.display.text stringByAppendingString:digit];
} else
{
self.display.text = digit;
self.userIsInTheMiddleOfEnteringANumber = YES;
}
self.history.text = self.history.text = [self.history.text stringByAppendingString: digit];
}
- (IBAction)opperationPresssed:(UIButton *)sender
{
if (self.userIsInTheMiddleOfEnteringANumber) [self enterPressed];
double result = [self.brain performOperation:sender.currentTitle];
NSString *resultString = [NSString stringWithFormat:@"%g", result];
self.display.text = resultString;
}
- (IBAction)enterPressed
{
[self.brain pushOperand:[self.display.text doubleValue]];
self.userIsInTheMiddleOfEnteringANumber = NO;
}
@end