55 lines
1.6 KiB
Objective-C
55 lines
1.6 KiB
Objective-C
//
|
|
// 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
|