#import "Calc.h"
#include "integrate.h"

//digits are 0 through 9
#define DIVIDE 10
#define MULTIPLY 11
#define SUBTRACT 12
#define ADD 13
#define NOOP 20

@implementation Calc

- init
{
	[super init];
	[self clearAll];
	return self;
}

- (void)clearAll
{
	xRegister = 0.0;
	pendingOperation = NOOP;        
}

- (double)performOperationWith:(double)value
{
	switch (pendingOperation){
		case ADD:
			xRegister += value;
			break;
		case SUBTRACT:
			xRegister -= value;
			break;
		case MULTIPLY:
			xRegister *= value;
			break;
		case DIVIDE:
			xRegister /= value;
			break;
		default:
			xRegister = value;
			break;
	}
	pendingOperation = NOOP;
    return xRegister;
}

- (double)setOperation:(int)operator forValue:(double)value
{
	xRegister = [self performOperationWith:value];  // do any pending stuff
	pendingOperation = operator;
    return xRegister;
}

- (void)changePendingOperation:(int)operator
{
	pendingOperation = operator;
}

@end

void *createCalcObject()
{
	Calc *aCalc = [[Calc alloc] init];

	return (void *)aCalc;
}

void clearAll(void *aCalc)
{
	[(id)aCalc clearAll];
}

double performOperationWith(void *aCalc, double value)
{
	return [(id)aCalc performOperationWith:value];
}

double setOperationForValue(void *aCalc, int operator, double value)
{
	return [(id)aCalc setOperation:operator forValue:value];
}

void changePendingOperation(void *aCalc, int operator)
{
	[(id)aCalc changePendingOperation:operator];
}

void *destroyCalcObject(void *aCalc)
{
	return [(id)aCalc free];
}
