Redesign of the Evaluation System
This commit is contained in:
54
MathPad/MPParsedOperator.m
Normal file
54
MathPad/MPParsedOperator.m
Normal file
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// MPParsedOperator.m
|
||||
// MathPad
|
||||
//
|
||||
// Created by Kim Wittenburg on 13.09.14.
|
||||
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MPParsedOperator.h"
|
||||
|
||||
@interface MPParsedOperator ()
|
||||
@property (nonatomic) NSRange range;
|
||||
@property (nonatomic) NSUInteger numberOfOperators;
|
||||
@property (nonatomic, strong) NSDecimalNumber *multiplicator;
|
||||
@end
|
||||
|
||||
@implementation MPParsedOperator
|
||||
|
||||
- (instancetype)initWithRange:(NSRange)range
|
||||
inString:(NSString *)string
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_range = range;
|
||||
if (range.location == NSNotFound) {
|
||||
_multiplicator = [NSDecimalNumber one];
|
||||
_numberOfOperators = 0;
|
||||
} else {
|
||||
NSString *stringValue = [string substringWithRange:range];
|
||||
NSString *operators = [[stringValue componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] componentsJoinedByString:@""];
|
||||
NSInteger multiplicator = 1;
|
||||
for (NSUInteger characterIndex = 0; characterIndex < operators.length; characterIndex++) {
|
||||
if ([[operators substringWithRange:NSMakeRange(characterIndex, 1)] isEqualToString:@"-"]) {
|
||||
multiplicator *= -1;
|
||||
}
|
||||
}
|
||||
_multiplicator = [[NSDecimalNumber alloc] initWithInteger:multiplicator];
|
||||
_numberOfOperators = operators.length;
|
||||
}
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (BOOL)exists
|
||||
{
|
||||
return self.range.location != NSNotFound;
|
||||
}
|
||||
|
||||
- (NSDecimalNumber *)evaluateWithError:(MPParseError *__autoreleasing *)error
|
||||
{
|
||||
return self.multiplicator;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user