Archived
1
This repository has been archived on 2022-08-08. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
mathpad/MathPad/MPParsedProduct.m
2014-09-13 23:16:44 +02:00

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