Archived
1

Corrected MPExpression -itemAtIndex:referenceFrame:MPSymbolReferenceFrame

Corrected Deformed Number Format
Added "arcsin", "arccos", "arctan", "lg", "log", "ln" Elementary Functions
This commit is contained in:
Kim Wittenburg
2014-12-11 15:32:06 +01:00
parent 98e4a6dde6
commit c367b1dbe8
7 changed files with 105 additions and 38 deletions

View File

@@ -16,6 +16,7 @@
#import "MPParenthesisFunction.h"
#import "MPFractionFunction.h"
#import "MPToken.h"
#import "MPRangePath.h"
#import "MPMathRules.h"
@@ -648,6 +649,10 @@
[self insertParenthesisFunction:nil];
return;
}
if ([characters isEqualToString:@")"]) {
[self insertClosingParenthesis];
return;
}
if (theEvent.keyCode == 10) {
[self insertPowerFunction];
return;
@@ -692,6 +697,29 @@
self.selection = MPMakeRangePath([[functionPath indexPathByAddingIndex:0] indexPathByAddingIndex:0], self.selection.length);
}
- (void)insertClosingParenthesis
{
if (self.selection.length > 0) {
[self insertParenthesisFunction:self];
} else {
NSIndexPath *currentPath = [[self.selection.location indexPathByRemovingLastIndex] indexPathByRemovingLastIndex];
while (currentPath.length > 0) {
id element = [self.expressionStorage elementAtIndexPath:currentPath];
if ([element isKindOfClass:[MPParenthesisFunction class]]) {
MPExpression *targetExpression = [self.expressionStorage elementAtIndexPath:[currentPath indexPathByRemovingLastIndex]];
NSUInteger selectedIndex = currentPath.lastIndex + 1;
selectedIndex = [targetExpression convertIndex:selectedIndex
fromReferenceFrame:MPElementReferenceFrame
toReferenceFrame:MPSymbolReferenceFrame];
self.selection = MPMakeRangePath([currentPath indexPathByReplacingLastIndexWithIndex:selectedIndex], 0);
break;
} else {
currentPath = [[currentPath indexPathByRemovingLastIndex] indexPathByRemovingLastIndex];
}
}
}
}
- (void)insertPowerFunction
{
MPExpression *selectedElementsExpression = [self.expressionStorage subexpressionWithRangePath:self.selection
@@ -712,19 +740,55 @@
- (void)insertFractionFunction
{
MPExpression *selectedElementsExpression = [self.expressionStorage subexpressionWithRangePath:self.selection
referenceFrame:MPSymbolReferenceFrame];
MPFractionFunction *function = [[MPFractionFunction alloc] init];
function.nominatorExpression = selectedElementsExpression;
[self.expressionStorage replaceItemsInRangePath:self.selection
referenceFrame:MPSymbolReferenceFrame
withElements:@[function]];
MPExpression *targetExpression = [self.expressionStorage elementAtIndexPath:[self.selection.location indexPathByRemovingLastIndex]];
NSUInteger functionElementIndex = [targetExpression convertIndex:self.selection.location.lastIndex
fromReferenceFrame:MPSymbolReferenceFrame
toReferenceFrame:MPElementReferenceFrame];
NSIndexPath *functionPath = [self.selection.location indexPathByReplacingLastIndexWithIndex:functionElementIndex];
self.selection = MPMakeRangePath([[functionPath indexPathByAddingIndex:0] indexPathByAddingIndex:0], 0);
if (self.selection.length == 0) {
MPExpression *targetExpression = [self.expressionStorage elementAtIndexPath:[self.selection.location indexPathByRemovingLastIndex]];
NSInteger index = self.selection.location.lastIndex;
NSRange nominatorSymbols = NSMakeRange(index, 0);
while (--index >= 0) {
id<MPExpressionElement> symbol = [targetExpression symbolAtIndex:index];
if ([symbol isString]) {
NSUInteger tokenIndex = [targetExpression convertIndex:index
fromReferenceFrame:MPSymbolReferenceFrame
toReferenceFrame:MPTokenReferenceFrame];
id<MPToken> token = [targetExpression tokenAtIndex:tokenIndex];
if (token.tokenType == MPNumberToken || token.tokenType == MPVariableToken || token.tokenType == MPElementaryFunctionToken) {
nominatorSymbols.location--;
nominatorSymbols.length++;
} else {
break;
}
} else if (nominatorSymbols.length == 0) {
nominatorSymbols.location--;
nominatorSymbols.length++;
} else {
break;
}
}
MPExpression *nominatorExpression = [targetExpression subexpressionWithRange:nominatorSymbols
referenceFrame:MPSymbolReferenceFrame];
MPFractionFunction *function = [[MPFractionFunction alloc] init];
function.nominatorExpression = nominatorExpression;
[targetExpression replaceItemsInRange:nominatorSymbols referenceFrame:MPSymbolReferenceFrame withElements:@[function]];
NSUInteger functionElementIndex = [targetExpression convertIndex:nominatorSymbols.location
fromReferenceFrame:MPSymbolReferenceFrame
toReferenceFrame:MPElementReferenceFrame];
NSUInteger selectedSubexpression = nominatorSymbols.length == 0 ? 0 : 1;
self.selection = MPMakeRangePath([[[self.selection.location indexPathByReplacingLastIndexWithIndex:functionElementIndex] indexPathByAddingIndex:selectedSubexpression] indexPathByAddingIndex:0], 0);
} else {
MPExpression *selectedElementsExpression = [self.expressionStorage subexpressionWithRangePath:self.selection
referenceFrame:MPSymbolReferenceFrame];
MPFractionFunction *function = [[MPFractionFunction alloc] init];
function.nominatorExpression = selectedElementsExpression;
[self.expressionStorage replaceItemsInRangePath:self.selection
referenceFrame:MPSymbolReferenceFrame
withElements:@[function]];
MPExpression *targetExpression = [self.expressionStorage elementAtIndexPath:[self.selection.location indexPathByRemovingLastIndex]];
NSUInteger functionElementIndex = [targetExpression convertIndex:self.selection.location.lastIndex
fromReferenceFrame:MPSymbolReferenceFrame
toReferenceFrame:MPElementReferenceFrame];
NSIndexPath *functionPath = [self.selection.location indexPathByReplacingLastIndexWithIndex:functionElementIndex];
self.selection = MPMakeRangePath([[functionPath indexPathByAddingIndex:0] indexPathByAddingIndex:0], 0);
}
}
- (void)insertNewline:(id)sender