65 lines
1.3 KiB
Objective-C
65 lines
1.3 KiB
Objective-C
//
|
|
// MPParsedSummand.m
|
|
// MathPad
|
|
//
|
|
// Created by Kim Wittenburg on 10.09.14.
|
|
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
|
|
//
|
|
|
|
#import "MPParsedProduct.h"
|
|
|
|
@implementation MPParsedProduct {
|
|
NSMutableArray *_factors;
|
|
}
|
|
|
|
- (instancetype)init
|
|
{
|
|
self = [super init];
|
|
if (self) {
|
|
_factors = [[NSMutableArray alloc] init];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (instancetype)initWithFactor:(id<MPParsedFactor>)factor
|
|
{
|
|
self = [super init];
|
|
if (self) {
|
|
_factors = [[NSMutableArray alloc] initWithObjects:factor, nil];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (NSArray *)factors
|
|
{
|
|
return _factors;
|
|
}
|
|
|
|
- (void)addFactor:(id<MPParsedFactor>)factor
|
|
{
|
|
[_factors addObject:factor];
|
|
}
|
|
|
|
- (NSDecimalNumber *)evaluateWithError:(MPParseError *__autoreleasing *)error
|
|
{
|
|
if (_factors.count == 0) {
|
|
return [NSDecimalNumber zero];
|
|
}
|
|
NSDecimalNumber *value = [NSDecimalNumber one];
|
|
for (id<MPParsedFactor> factor in _factors) {
|
|
NSDecimalNumber *factorValue = [factor evaluateWithError:error];
|
|
if (!factorValue) {
|
|
return nil;
|
|
}
|
|
value = [value decimalNumberByMultiplyingBy:factorValue];
|
|
}
|
|
return value;
|
|
}
|
|
|
|
- (NSString *)description
|
|
{
|
|
return [NSString stringWithFormat:@"MPParsedProduct<%@>", [_factors componentsJoinedByString:@"*"]];
|
|
}
|
|
|
|
@end
|