Implemented the Sum Function
This commit is contained in:
120
MathPad/MPSumFunction.m
Normal file
120
MathPad/MPSumFunction.m
Normal file
@@ -0,0 +1,120 @@
|
||||
//
|
||||
// MPSumFunction.m
|
||||
// MathPad
|
||||
//
|
||||
// Created by Kim Wittenburg on 22.04.14.
|
||||
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MPSumFunction.h"
|
||||
#import "MPExpression.h"
|
||||
|
||||
@implementation MPSumFunction
|
||||
|
||||
#pragma mark Creation Methods
|
||||
|
||||
- (instancetype)init
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_startExpression = [[MPExpression alloc] init];
|
||||
_startExpression.parent = self;
|
||||
_targetExpression = [[MPExpression alloc] init];
|
||||
_targetExpression.parent = self;
|
||||
_sumExpression = [[MPExpression alloc] init];
|
||||
_sumExpression.parent = self;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
#pragma mark Working With the Expression Tree
|
||||
|
||||
- (NSUInteger)numberOfChildren
|
||||
{
|
||||
return 3;
|
||||
}
|
||||
|
||||
- (MPExpression *)childAtIndex:(NSUInteger)index
|
||||
{
|
||||
switch (index) {
|
||||
case 0:
|
||||
return self.startExpression;
|
||||
case 1:
|
||||
return self.targetExpression;
|
||||
case 2:
|
||||
return self.sumExpression;
|
||||
default:
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
|
||||
- (NSArray *)children
|
||||
{
|
||||
return @[self.startExpression, self.targetExpression, self.sumExpression];
|
||||
}
|
||||
|
||||
- (void)setChild:(MPExpression *)child
|
||||
atIndex:(NSUInteger)index
|
||||
{
|
||||
switch (index) {
|
||||
// TODO: Copy child?
|
||||
case 0:
|
||||
self.startExpression.parent = nil;
|
||||
self.startExpression = child;
|
||||
break;
|
||||
case 1:
|
||||
self.targetExpression.parent = nil;
|
||||
self.targetExpression = child;
|
||||
break;
|
||||
case 2:
|
||||
self.sumExpression.parent = nil;
|
||||
self.sumExpression = child;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
child.parent = self;
|
||||
}
|
||||
|
||||
#pragma mark Evaluating Functions
|
||||
|
||||
- (double)doubleValue
|
||||
{
|
||||
#warning Implementation
|
||||
return 0;
|
||||
}
|
||||
|
||||
#pragma mark Working With Functions
|
||||
|
||||
- (BOOL)isEqualToFunction:(MPFunction *)aFunction
|
||||
{
|
||||
if (![aFunction isKindOfClass:[MPSumFunction class]]) {
|
||||
return NO;
|
||||
}
|
||||
MPSumFunction *sumFunction = (MPSumFunction *)aFunction;
|
||||
if (![self.startExpression isEqualToExpression:sumFunction.startExpression]) {
|
||||
return NO;
|
||||
}
|
||||
if (![self.targetExpression isEqualToExpression:sumFunction.targetExpression]) {
|
||||
return NO;
|
||||
}
|
||||
return [self.sumExpression isEqualToExpression:sumFunction.sumExpression];
|
||||
}
|
||||
|
||||
- (NSString *)description
|
||||
{
|
||||
return [NSString stringWithFormat:@"Sum(From: %@; To: %@; Using: %@)", self.startExpression, self.targetExpression, self.sumExpression];
|
||||
}
|
||||
|
||||
#pragma mark - NSCopying
|
||||
|
||||
- (id)copyWithZone:(NSZone *)zone
|
||||
{
|
||||
MPSumFunction *copy = [[MPSumFunction allocWithZone:zone] init];
|
||||
copy.startExpression = [self.startExpression copy];
|
||||
copy.targetExpression = [self.targetExpression copy];
|
||||
copy.sumExpression = [self.sumExpression copy];
|
||||
return copy;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user