postfix functions calculations fixes

This commit is contained in:
serso 2011-10-23 20:10:48 +04:00
parent b6742a7c15
commit edc5cb1431
5 changed files with 168 additions and 119 deletions

View File

@ -10,6 +10,7 @@ import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.CharacterAtPositionFinder;
import org.solovyev.android.calculator.StartsWithFinder;
import org.solovyev.android.calculator.model.CalculatorEngine;
import org.solovyev.android.calculator.model.ParseException;
import org.solovyev.common.utils.CollectionsUtils;
import org.solovyev.common.utils.Finder;
@ -36,17 +37,7 @@ public enum MathType {
}
},
grouping_separator(250, false, false, "'", " ") {
@Override
public int processToJscl(@NotNull StringBuilder result, int i, @NotNull String match) {
return i;
}
@Override
public int processFromJscl(@NotNull StringBuilder result, int i, @NotNull String match) {
return i;
}
},
grouping_separator(250, false, false, "'", " "),
power_10(300, true, false, "E") {
@Override
@ -68,6 +59,33 @@ public enum MathType {
return result;
}
/* @Override
public int processToJscl(@NotNull StringBuilder result, int i, @NotNull String match) throws ParseException {
if (result.length() > 0) {
int startOfPrefixFunction = getPostfixFunctionStart(result, result.length() - 1);
if (result.length() > startOfPrefixFunction) {
startOfPrefixFunction = Math.max(0, startOfPrefixFunction);
final String substring = result.substring(startOfPrefixFunction);
result.setCharAt(startOfPrefixFunction, '(');
for ( int j = 0; j < substring.length(); j++ ){
if (result.length() > startOfPrefixFunction + 1 + j) {
result.setCharAt(startOfPrefixFunction + 1 + j, substring.charAt(j));
} else {
result.append(substring.charAt(j));
}
}
} else {
result.append('(');
}
super.processToJscl(result, i, match);
result.append(")");
} else {
throw new ParseException("Could not find start of prefix function!");
}
return returnI(i, match);
}*/
},
unary_operation(500, false, false, "-", "=", "!"),
binary_operation(600, false, false, "-", "+", "*", "×", "", "/", "^") {
@ -190,6 +208,56 @@ public enum MathType {
this.tokens = Collections.unmodifiableList(tokens);
}
/* public static int getPostfixFunctionStart(@NotNull CharSequence s, int position) throws ParseException {
assert s.length() > position;
int numberOfOpenGroups = 0;
int result = position;
for (; result >= 0; result--) {
final MathType mathType = getType(s.toString(), result).getMathType();
if (CollectionsUtils.contains(mathType, digit, dot, grouping_separator, power_10)) {
// continue
} else if (mathType == close_group_symbol) {
numberOfOpenGroups++;
} else if (mathType == open_group_symbol) {
if (numberOfOpenGroups > 0) {
numberOfOpenGroups--;
} else {
break;
}
} else {
if (stop(s, numberOfOpenGroups, result)) break;
}
}
if (numberOfOpenGroups != 0){
throw new ParseException("Could not find start of prefix function!");
}
return result;
}
public static boolean stop(CharSequence s, int numberOfOpenGroups, int i) {
if (numberOfOpenGroups == 0) {
if (i > 0) {
final EndsWithFinder endsWithFinder = new EndsWithFinder(s);
endsWithFinder.setI(i + 1);
if (!CollectionsUtils.contains(function.getTokens(), FilterType.included, endsWithFinder)) {
MathType type = getType(s.toString(), i).getMathType();
if (type != constant) {
return true;
}
}
} else {
return true;
}
}
return false;
}*/
@NotNull
public List<String> getTokens() {
return tokens;
@ -207,9 +275,13 @@ public enum MathType {
return needMultiplicationSignBefore && mathTypeBefore.isNeedMultiplicationSignAfter();
}
public int processToJscl(@NotNull StringBuilder result, int i, @NotNull String match) {
public int processToJscl(@NotNull StringBuilder result, int i, @NotNull String match) throws ParseException {
final String substitute = getSubstituteToJscl(match);
result.append(substitute == null ? match : substitute);
return returnI(i, match);
}
protected int returnI(int i, @NotNull String match) {
if (match.length() > 1) {
return i + match.length() - 1;
} else {
@ -220,11 +292,7 @@ public enum MathType {
public int processFromJscl(@NotNull StringBuilder result, int i, @NotNull String match) {
final String substitute = getSubstituteFromJscl(match);
result.append(substitute == null ? match : substitute);
if (match.length() > 1) {
return i + match.length() - 1;
} else {
return i;
}
return returnI(i, match);
}
@Nullable
@ -318,7 +386,7 @@ public enum MathType {
this.match = match;
}
public int processToJscl(@NotNull StringBuilder result, int i) {
public int processToJscl(@NotNull StringBuilder result, int i) throws ParseException {
return mathType.processToJscl(result, i, match);
}
@ -344,4 +412,25 @@ public enum MathType {
private static boolean contains(@NotNull List<Character> list, @NotNull final CharacterAtPositionFinder atPositionFinder) {
return get(list, atPositionFinder) != null;
}
private static class EndsWithFinder implements Finder<String> {
private int i;
@NotNull
private final CharSequence targetString;
private EndsWithFinder(@NotNull CharSequence targetString) {
this.targetString = targetString;
}
@Override
public boolean isFound(@Nullable String s) {
return targetString.subSequence(0, i).toString().endsWith(s);
}
public void setI(int i) {
this.i = i;
}
}
}

View File

@ -7,13 +7,10 @@
package org.solovyev.android.calculator.model;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.StartsWithFinder;
import org.solovyev.android.calculator.jscl.JsclOperation;
import org.solovyev.android.calculator.math.MathType;
import org.solovyev.common.utils.CollectionsUtils;
import org.solovyev.common.utils.FilterType;
import org.solovyev.common.utils.Finder;
import java.util.ArrayList;
import java.util.List;
@ -48,6 +45,10 @@ class ToJsclTextProcessor implements TextProcessor<PreparedExpression> {
if (mathTypeBefore == MathType.function && CollectionsUtils.get(MathType.openGroupSymbols, startsWithFinder) != null) {
throw new ParseException("Empty function: " + mathTypeResult.getMatch());
} else if (mathTypeBefore == MathType.postfix_function && mathTypeResult.getMathType() == MathType.binary_operation) {
if ( mathTypeResult.getMatch().equals("^") ) {
throw new ParseException("Power operation after postfix function is currently unsupported!");
}
}
i = mathTypeResult.processToJscl(result, i);
@ -99,80 +100,6 @@ class ToJsclTextProcessor implements TextProcessor<PreparedExpression> {
return new PreparedExpression(result.toString(), undefinedVars);
}
private void replaceVariables(StringBuilder sb, String s, int i, @NotNull StartsWithFinder startsWithFinder) {
for (Var var : CalculatorEngine.instance.getVarsRegister().getVars()) {
if (!var.isSystem()) {
if (s.startsWith(var.getName(), i)) {
if (CollectionsUtils.get(MathType.function.getTokens(), startsWithFinder) == null) {
}
}
}
}
}
public int getPostfixFunctionStart(@NotNull String s, int position) {
assert s.length() > position;
int numberOfOpenGroups = 0;
int result = position;
for (; result >= 0; result--) {
final MathType mathType = MathType.getType(s, result).getMathType();
if (CollectionsUtils.contains(mathType, MathType.digit, MathType.dot, MathType.grouping_separator)) {
// continue
} else if (mathType == MathType.close_group_symbol) {
numberOfOpenGroups++;
} else if (mathType == MathType.open_group_symbol) {
numberOfOpenGroups--;
} else {
if (stop(s, numberOfOpenGroups, result)) break;
}
}
return result;
}
private boolean stop(String s, int numberOfOpenGroups, int i) {
if (numberOfOpenGroups == 0) {
if (i > 0) {
final EndsWithFinder endsWithFinder = new EndsWithFinder(s);
endsWithFinder.setI(i + 1);
if (!CollectionsUtils.contains(MathType.function.getTokens(), FilterType.included, endsWithFinder)) {
MathType type = MathType.getType(s, i).getMathType();
if (type != MathType.constant) {
return true;
}
}
} else {
return true;
}
}
return false;
}
private static class EndsWithFinder implements Finder<String> {
private int i;
@NotNull
private final String targetString;
private EndsWithFinder(@NotNull String targetString) {
this.targetString = targetString;
}
@Override
public boolean isFound(@Nullable String s) {
return targetString.substring(0, i).endsWith(s);
}
public void setI(int i) {
this.i = i;
}
}
public static String wrap(@NotNull JsclOperation operation, @NotNull String s) {
return operation.name() + "(\"" + s + "\");";
}

View File

@ -42,4 +42,27 @@ public class MathTypeTest {
} catch (IllegalArgumentException e) {
}
}
/* @Test
public void testPostfixFunctionsProcessing() throws Exception {
org.junit.Assert.assertEquals(-1, MathType.getPostfixFunctionStart("5!", 1));
org.junit.Assert.assertEquals(0, MathType.getPostfixFunctionStart("!", 1));
org.junit.Assert.assertEquals(-1, MathType.getPostfixFunctionStart("5.4434234!", 9));
org.junit.Assert.assertEquals(1, MathType.getPostfixFunctionStart("2+5!", 3));
org.junit.Assert.assertEquals(4, MathType.getPostfixFunctionStart("2.23+5.4434234!", 14));
org.junit.Assert.assertEquals(14, MathType.getPostfixFunctionStart("2.23+5.4434234*5!", 16));
org.junit.Assert.assertEquals(14, MathType.getPostfixFunctionStart("2.23+5.4434234*5.1!", 18));
org.junit.Assert.assertEquals(4, MathType.getPostfixFunctionStart("2.23+(5.4434234*5.1)!", 20));
org.junit.Assert.assertEquals(4, MathType.getPostfixFunctionStart("2.23+(5.4434234*(5.1+1))!", 24));
org.junit.Assert.assertEquals(4, MathType.getPostfixFunctionStart("2.23+(5.4434234*sin(5.1+1))!", 27));
org.junit.Assert.assertEquals(0, MathType.getPostfixFunctionStart("sin(5)!", 6));
org.junit.Assert.assertEquals(-1, MathType.getPostfixFunctionStart(")!", ")!".indexOf("!")));
org.junit.Assert.assertEquals(0, MathType.getPostfixFunctionStart("sin(5sin(5sin(5)))!", 18));
org.junit.Assert.assertEquals(2, MathType.getPostfixFunctionStart("2+sin(5sin(5sin(5)))!", 20));
org.junit.Assert.assertEquals(5, MathType.getPostfixFunctionStart("2.23+sin(5.4434234*sin(5.1+1))!", 30));
org.junit.Assert.assertEquals(5, MathType.getPostfixFunctionStart("2.23+sin(5.4434234*sin(5.1E2+e))!", "2.23+sin(5.4434234*sin(5.1E2+e))!".indexOf("!")));
org.junit.Assert.assertEquals(5, MathType.getPostfixFunctionStart("2.23+sin(5.4434234*sin(5.1E2+5 555 555))!", "2.23+sin(5.4434234*sin(5.1E2+5 555 555))!".indexOf("!")));
org.junit.Assert.assertEquals(5, MathType.getPostfixFunctionStart("2.23+sin(5.4434234^sin(5.1E2!+5'555'555))!", "2.23+sin(5.4434234^sin(5.1E2!+5'555'555))!".lastIndexOf("!")));
}*/
}

View File

@ -107,6 +107,19 @@ public class CalculatorEngineTest {
Assert.assertEquals("11et", cm.evaluate(JsclOperation.numeric, "t11e"));
Assert.assertEquals("11×Infinityt", cm.evaluate(JsclOperation.numeric, "t11∞"));
Assert.assertEquals("-t+t^3", cm.evaluate(JsclOperation.numeric, "t(t-1)(t+1)"));
Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "30°"));
Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "(10+20)°"));
Assert.assertEquals("1.047", cm.evaluate(JsclOperation.numeric, "(10+20)°*2"));
try {
Assert.assertEquals("0.278", cm.evaluate(JsclOperation.numeric, "30°^2"));
junit.framework.Assert.fail();
} catch (ParseException e) {
if ( !e.getMessage().equals("Power operation after postfix function is currently unsupported!") ) {
junit.framework.Assert.fail();
}
}
}
@Test
@ -152,7 +165,7 @@ public class CalculatorEngineTest {
}
@Test
/* @Test
public void testDegrees() throws Exception {
final CalculatorEngine cm = CalculatorEngine.instance;
@ -163,5 +176,5 @@ public class CalculatorEngineTest {
Assert.assertEquals("0.5", cm.evaluate(JsclOperation.numeric, "sin(30°)"));
Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "asin(sin(30°))"));
}
}*/
}

View File

@ -88,38 +88,35 @@ public class ToJsclTextProcessorTest {
}
}
@Test
public void testPostfixFunctionsProcessing() throws Exception {
final ToJsclTextProcessor preprocessor = new ToJsclTextProcessor();
Assert.assertEquals(-1, preprocessor.getPostfixFunctionStart("5!", 0));
Assert.assertEquals(0, preprocessor.getPostfixFunctionStart("!", 0));
Assert.assertEquals(-1, preprocessor.getPostfixFunctionStart("5.4434234!", 8));
Assert.assertEquals(1, preprocessor.getPostfixFunctionStart("2+5!", 2));
Assert.assertEquals(4, preprocessor.getPostfixFunctionStart("2.23+5.4434234!", 13));
Assert.assertEquals(14, preprocessor.getPostfixFunctionStart("2.23+5.4434234*5!", 15));
Assert.assertEquals(14, preprocessor.getPostfixFunctionStart("2.23+5.4434234*5.1!", 17));
Assert.assertEquals(4, preprocessor.getPostfixFunctionStart("2.23+(5.4434234*5.1)!", 19));
Assert.assertEquals(4, preprocessor.getPostfixFunctionStart("2.23+(5.4434234*(5.1+1))!", 23));
Assert.assertEquals(4, preprocessor.getPostfixFunctionStart("2.23+(5.4434234*sin(5.1+1))!", 26));
Assert.assertEquals(0, preprocessor.getPostfixFunctionStart("sin(5)!", 5));
Assert.assertEquals(0, preprocessor.getPostfixFunctionStart("sin(5sin(5sin(5)))!", 17));
Assert.assertEquals(2, preprocessor.getPostfixFunctionStart("2+sin(5sin(5sin(5)))!", 19));
Assert.assertEquals(5, preprocessor.getPostfixFunctionStart("2.23+sin(5.4434234*sin(5.1+1))!", 29));
}
@Test
public void testDegrees() throws Exception {
final ToJsclTextProcessor preprocessor = new ToJsclTextProcessor();
Assert.assertEquals( "", preprocessor.process("").toString());
try {
Assert.assertEquals( "3.141592653589793/180", preprocessor.process("°").toString());
} catch (ParseException e) {
if ( !e.getMessage().startsWith("Could not find start of prefix") ){
junit.framework.Assert.fail();
}
}
Assert.assertEquals( "1*3.141592653589793/180", preprocessor.process("").toString());
Assert.assertEquals( "20.0*3.141592653589793/180", preprocessor.process("20.0°").toString());
Assert.assertEquals( "sin(30*3.141592653589793/180)", preprocessor.process("sin(30°)").toString());
Assert.assertEquals( "asin(sin(3.141592653589793/6))*3.141592653589793/180", preprocessor.process("asin(sin(π/6))°").toString());
Assert.assertEquals( "1*3.141592653589793/180*sin(1)", preprocessor.process("1°sin(1)").toString());
try {
Assert.assertEquals( "1*3.141592653589793/180^sin(1)", preprocessor.process("1°^sin(1)").toString());
junit.framework.Assert.fail();
} catch (ParseException e) {
if ( !e.getMessage().equals("Power operation after postfix function is currently unsupported!") ) {
junit.framework.Assert.fail();
}
}
}
@Test
public void testPostfixFunction() throws Exception {
}
}