0

I'm doing a calculator app base on a online tutorial, itunes.apple. com/itunes-u/ipad-iphone-application- development/ id473757255 (tut 2)

I followed every step closely and all was fine, until the finishing step of a method call performOperation. When I build and run, the numbers and enter function work fine. Only the operation method is not working. So I presume that the main trouble is with the operation method.

BrainCalculator.h

@interface CalculatorBrain : NSObject

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


@end

BrainCalculator.m

#import "CalculatorBrain.h"
@interface CalculatorBrain()
@property (nonatomic, strong) NSMutableArray* _operandStack;

@end

@implementation CalculatorBrain
@synthesize _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];
}   


-(double)performOperation:(NSString *)operation
{
    double result = 0;

    if ([operation isEqualToString:@"+"]){
        result=[self popOperand] + [self popOperand];
    }else if ([@"*" isEqualToString:operation]){
        result = [self popOperand] * [self popOperand];
    }else if ([operation isEqualToString:@"-"]){
        double subtrahend = [self popOperand];
        result = [self popOperand] - subtrahend;
    }else if( [operation isEqualToString:@"/"]){
        double divisor = [self popOperand];
        if (divisor) result = [self popOperand] / divisor;
    }
    [self pushOperand:result];
    return result;
}

@end

Initially, it seem to me that the performOperation method was pretty fishy, so I tried fiddling the

 }else if ([@"*" isEqualToString:operation]){

to

}else if ([operation isEqualToString:@"*"]){

hoping it would work, but sadly it didn't.

Just for additional information

viewcontroller.m

#import "CalculatorViewController.h"
#import "CalculatorBrain.h"

@interface CalculatorViewController ()
@property (nonatomic) BOOL userIsInTheMiddleOfEnteringANumber;
@property (nonatomic, strong) CalculatorBrain *brain;
@end

@implementation CalculatorViewController
@synthesize display;
@synthesize userIsInTheMiddleOfEnteringANumber;
@synthesize brain= _brain;  

-(CalculatorBrain*)brain
{
    if(!_brain)_brain = [[CalculatorBrain alloc]init];
    return _brain;
}




- (IBAction)digitPressed:(UIButton *)sender {

    NSString * digit= [ sender currentTitle];
    if (userIsInTheMiddleOfEnteringANumber){
        self.display.text = [self.display.text stringByAppendingString:digit];
    }
    else{
        self.display.text=digit;
        self.userIsInTheMiddleOfEnteringANumber = YES;
    }

} 


- (IBAction)enterPressed {
    [self.brain pushOperand:[self.display.text doubleValue]];
    self.userIsInTheMiddleOfEnteringANumber = NO;  

}

- (IBAction)operationPressed:(UIButton *)sender {
    if (self.userIsInTheMiddleOfEnteringANumber){
        [self enterPressed];
    }
    NSString *operation = [sender currentTitle];
    double result = [self.brain performOperation:operation];
    self.display.text = [NSString stringWithFormat:@"%g", result];
}   





@end

help will be greatly appreciated as I'm practising xcode to prepare myself for my final year major project.

4

0 回答 0