0

我正在通过 iTunes 大学(斯坦福)学习 Objective-C。我正在对 sin 按钮进行编程,但遇到了 Parse 问题:

此行上的预期标识符“double myDegrees = [self.operandStack];”。

CalculatorBrain.h

#import <Foundation/Foundation.h>


@interface CalculatorBrain : NSObject

-(void)pushOperand:(double)operand;
-(double)performOperation:(NSString *)operation;

@end

计算器大脑.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

计算器视图控制器.h

#import <UIKit/UIKit.h>
#import <math.h>

@interface CalculatorViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *display;
@property (weak, nonatomic) IBOutlet UILabel *history;

@end

计算器视图控制器.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
4

1 回答 1

1
double myDegrees = [[self.operandStack objectAtIndex:0] doubleValue];
于 2012-10-20T16:47:25.740 回答