Implemented Expression Evaluation/Parsing with Proper Error Handling
This commit is contained in:
51
MathPad/MPParsedProduct.m
Normal file
51
MathPad/MPParsedProduct.m
Normal file
@@ -0,0 +1,51 @@
|
||||
//
|
||||
// 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;
|
||||
}
|
||||
|
||||
- (NSArray *)factors
|
||||
{
|
||||
return _factors;
|
||||
}
|
||||
|
||||
- (void)addFactor:(MPParsedFactor *)factor
|
||||
{
|
||||
[_factors addObject:factor];
|
||||
}
|
||||
|
||||
- (NSDecimalNumber *)value
|
||||
{
|
||||
if (_factors.count == 0) {
|
||||
return [NSDecimalNumber zero];
|
||||
}
|
||||
NSDecimalNumber *value = [NSDecimalNumber one];
|
||||
for (MPParsedFactor *factor in _factors) {
|
||||
value = [value decimalNumberByMultiplyingBy:factor.value];
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
- (NSString *)description
|
||||
{
|
||||
return [NSString stringWithFormat:@"MPParsedProduct<%@>", [_factors componentsJoinedByString:@"*"]];
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user