Source

Calculator / Classes / CalculatorBrain.m

//
//  CalculatorBrain.m
//  Calculator
//
//  Created by Martin Holman on 12/10/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "CalculatorBrain.h"


@implementation CalculatorBrain

- (void)setOperand:(double)aDouble 
{
	operand = aDouble;
}

- (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])
	{
		operand = - operand;
	}
	else if ([@"1/x" isEqual:operation])
	{
		if (operand) 
		{
			operand = 1 / operand;
		}
	}
	else if ([@"sin" isEqual:operation])
	{
		operand = sin(operand);
	}
	else if ([@"cos" isEqual:operation])
	{
		operand = cos(operand);
	}
	else if ([@"Store" isEqual:operation])
	{
		storedOperand = operand;
	}
	else if ([@"Recall" isEqual:operation])
	{
		operand = storedOperand;
	}
	else if ([@"Mem +" isEqual:operation])
	{
		storedOperand = storedOperand + operand;
	}
	else
	{
		[self performWaitingOperation]; 
		waitingOperation = operation; 
		waitingOperand = operand;
	}
	
	return operand;
}

@end