From c7f93700f108b25287534cebaa3cbfc7cae25526 Mon Sep 17 00:00:00 2001 From: Sergey Solovyev Date: Fri, 11 Nov 2011 19:18:33 +0400 Subject: [PATCH] changes --- res/values-ru/strings.xml | 2 +- res/values/strings.xml | 2 +- .../CalculatorActivityLauncher.java | 4 +-- .../CalculatorFunctionsActivity.java | 17 ++++++++++- .../calculator/CalculatorVarsActivity.java | 28 +++++++++++++++---- .../calculator/model/ToJsclTextProcessor.java | 6 ++-- .../android/calculator/model/Var.java | 10 ------- .../model/CalculatorEngineTest.java | 3 +- .../model/ToJsclTextProcessorTest.java | 28 +++++++++---------- 9 files changed, 62 insertions(+), 38 deletions(-) diff --git a/res/values-ru/strings.xml b/res/values-ru/strings.xml index 6a28fd44..3313a599 100644 --- a/res/values-ru/strings.xml +++ b/res/values-ru/strings.xml @@ -57,7 +57,7 @@ Значение должно либо оставаться пустым либо быть числом! Имя переменной не может быть зарезервированным системным именем! Переменная с таким именем уже существует! - Имя не может быть пустым! + Имя переменной не валидно: им ядолжно начинаться с буквы, может содержать буквы, цифры и знак подчёркивания. Системная переменная не может быть изменена! Отношение длины окружности к диаметру diff --git a/res/values/strings.xml b/res/values/strings.xml index dd5fa161..de54b5cd 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -60,7 +60,7 @@ Value must be either number or empty! Variable name clashes with function name! Variable with same name already exists! - Name is empty! + Name of constant is not valid: name must start with letter, can contain letters, digits and underscore. System variable cannot be changed! Ratio of any circle\'s circumference to its diameter diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java b/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java index 536e0498..d9a60845 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java @@ -2,10 +2,8 @@ package org.solovyev.android.calculator; import android.content.Context; import android.content.Intent; -import android.view.View; import android.widget.Toast; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import org.solovyev.common.utils.StringUtils; /** @@ -39,7 +37,7 @@ public class CalculatorActivityLauncher { if (calculatorModel.getDisplay().isValid() ) { final String varValue = calculatorModel.getDisplay().getText().toString(); if (!StringUtils.isEmpty(varValue)) { - if (CalculatorVarsActivity.isValid(varValue)) { + if (CalculatorVarsActivity.isValidValue(varValue)) { final Intent intent = new Intent(context, CalculatorVarsActivity.class); intent.putExtra(CalculatorVarsActivity.CREATE_VAR_EXTRA_STRING, varValue); context.startActivity(intent); diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorFunctionsActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorFunctionsActivity.java index 7b79290b..819ada86 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorFunctionsActivity.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorFunctionsActivity.java @@ -16,9 +16,11 @@ import android.widget.*; import jscl.math.function.Function; import org.jetbrains.annotations.NotNull; import org.solovyev.android.calculator.model.CalculatorEngine; +import org.solovyev.android.calculator.model.Var; import org.solovyev.common.utils.StringUtils; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; /** @@ -26,7 +28,7 @@ import java.util.List; * Date: 10/29/11 * Time: 4:55 PM */ -public class CalculatorFunctionsActivity extends ListActivity{ +public class CalculatorFunctionsActivity extends ListActivity { @NotNull private FunctionsArrayAdapter adapter; @@ -55,6 +57,19 @@ public class CalculatorFunctionsActivity extends ListActivity{ } }); + sort(); + + } + + private void sort() { + CalculatorFunctionsActivity.this.adapter.sort(new Comparator() { + @Override + public int compare(Function function1, Function function2) { + return function1.getName().compareTo(function2.getName()); + } + }); + + CalculatorFunctionsActivity.this.adapter.notifyDataSetChanged(); } private class FunctionsArrayAdapter extends ArrayAdapter { diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java index 5ee36b7a..874b08db 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorVarsActivity.java @@ -16,6 +16,9 @@ import android.text.Editable; import android.text.TextWatcher; import android.view.*; import android.widget.*; +import jscl.text.Identifier; +import jscl.text.MutableInt; +import jscl.text.ParseException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.solovyev.android.calculator.math.MathType; @@ -38,7 +41,7 @@ public class CalculatorVarsActivity extends ListActivity { public static final String CREATE_VAR_EXTRA_STRING = "org.solovyev.android.calculator.CalculatorVarsActivity_create_var"; - private final static List acceptableChars = Arrays.asList(StringUtils.toObject("1234567890abcdefghijklmnopqrstuvwxyzйцукенгшщзхъфывапролджэячсмитьбюё".toCharArray())); + private final static List acceptableChars = Arrays.asList(StringUtils.toObject("1234567890abcdefghijklmnopqrstuvwxyzйцукенгшщзхъфывапролджэячсмитьбюё_".toCharArray())); @NotNull private VarsArrayAdapter adapter; @@ -208,7 +211,7 @@ public class CalculatorVarsActivity extends ListActivity { final AndroidVarsRegistry varsRegistry = CalculatorEngine.instance.getVarsRegister(); - if (!StringUtils.isEmpty(name)) { + if (isValidName(name)) { boolean canBeSaved = false; @@ -232,7 +235,7 @@ public class CalculatorVarsActivity extends ListActivity { error = null; } else { // value is not empty => must be a number - boolean valid = isValid(value); + boolean valid = isValidValue(value); if (valid) { varBuilder.setName(name); @@ -250,7 +253,7 @@ public class CalculatorVarsActivity extends ListActivity { error = R.string.c_var_already_exists; } } else { - error = R.string.c_name_is_empty; + error = R.string.c_name_is_not_valid; } if (error != null) { @@ -284,7 +287,22 @@ public class CalculatorVarsActivity extends ListActivity { CalculatorVarsActivity.this.adapter.notifyDataSetChanged(); } - public static boolean isValid(@NotNull String value) { + private static boolean isValidName(@Nullable String name) { + boolean result = false; + + if (!StringUtils.isEmpty(name)) { + try { + Identifier.parser.parse(name, new MutableInt(0)); + result = true; + } catch (ParseException e) { + // not valid name; + } + } + + return result; + } + + public static boolean isValidValue(@NotNull String value) { // now every string might be constant return true; } diff --git a/src/main/java/org/solovyev/android/calculator/model/ToJsclTextProcessor.java b/src/main/java/org/solovyev/android/calculator/model/ToJsclTextProcessor.java index 8109e987..209adea6 100644 --- a/src/main/java/org/solovyev/android/calculator/model/ToJsclTextProcessor.java +++ b/src/main/java/org/solovyev/android/calculator/model/ToJsclTextProcessor.java @@ -18,7 +18,7 @@ import java.util.List; class ToJsclTextProcessor implements TextProcessor { @NotNull - private static final Integer MAX_DEPTH = 10; + private static final Integer MAX_DEPTH = 20; @Override @NotNull @@ -93,7 +93,9 @@ class ToJsclTextProcessor implements TextProcessor { assert value != null; if ( var.getDoubleValue() != null ) { - result.append(value); + //result.append(value); + // NOTE: append varName as JSCL engine will convert it to double if needed + result.append(varName); } else { result.append("(").append(processWithDepth(value, depth, undefinedVars)).append(")"); } diff --git a/src/main/java/org/solovyev/android/calculator/model/Var.java b/src/main/java/org/solovyev/android/calculator/model/Var.java index 9a5d3559..b8051682 100644 --- a/src/main/java/org/solovyev/android/calculator/model/Var.java +++ b/src/main/java/org/solovyev/android/calculator/model/Var.java @@ -189,16 +189,6 @@ public class Var implements IConstant { this.id = id; } - @Override - public boolean same(@Nullable MathEntity mathEntity) { - if (mathEntity instanceof IConstant) { - if (mathEntity.getId().equals(this.getId())) { - return true; - } - } - return false; - } - @NotNull public String getName() { return name; diff --git a/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java b/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java index 559d65f6..b8f9670b 100644 --- a/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java +++ b/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java @@ -148,7 +148,8 @@ public class CalculatorEngineTest { CalculatorEngine.instance.getVarsRegister().add(null, new Var.Builder("t", (String) null)); Assert.assertEquals("11t", cm.evaluate(JsclOperation.numeric, "t11").getResult()); Assert.assertEquals("11et", cm.evaluate(JsclOperation.numeric, "t11e").getResult()); - Assert.assertEquals("11×Infinityt", cm.evaluate(JsclOperation.numeric, "t11∞").getResult()); + Assert.assertEquals("∞", cm.evaluate(JsclOperation.numeric, "∞").getResult()); + Assert.assertEquals("11t∞", cm.evaluate(JsclOperation.numeric, "t11∞").getResult()); Assert.assertEquals("-t+t^3", cm.evaluate(JsclOperation.numeric, "t(t-1)(t+1)").getResult()); Assert.assertEquals("3.957", cm.evaluate(JsclOperation.numeric, "ln(8)lg(8)+ln(8)").getResult()); diff --git a/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java b/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java index 2400e872..e7db7941 100644 --- a/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java +++ b/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java @@ -45,13 +45,13 @@ public class ToJsclTextProcessorTest { Assert.assertEquals( "(0)*ln(1)*(2*10^-1)", preprocessor.process("[0]ln(1)[2*E-1]").toString()); Assert.assertEquals( "sin(4)*asin(0.5)*√(2)", preprocessor.process("sin(4)asin(0.5)√(2)").toString()); Assert.assertEquals( "sin(4)*cos(5)", preprocessor.process("sin(4)cos(5)").toString()); - Assert.assertEquals( "3.141592653589793*sin(4)*3.141592653589793*cos(√(5))", preprocessor.process("πsin(4)πcos(√(5))").toString()); - Assert.assertEquals( "3.141592653589793*sin(4)+3.141592653589793*cos(√(5))", preprocessor.process("πsin(4)+πcos(√(5))").toString()); - Assert.assertEquals( "3.141592653589793*sin(4)+3.141592653589793*cos(√(5+(√(-1))))", preprocessor.process("πsin(4)+πcos(√(5+i))").toString()); - Assert.assertEquals( "3.141592653589793*sin(4.01)+3.141592653589793*cos(√(5+(√(-1))))", preprocessor.process("πsin(4.01)+πcos(√(5+i))").toString()); - Assert.assertEquals( "2.718281828459045^3.141592653589793*sin(4.01)+3.141592653589793*cos(√(5+(√(-1))))", preprocessor.process("e^πsin(4.01)+πcos(√(5+i))").toString()); - Assert.assertEquals( "2.718281828459045^3.141592653589793*sin(4.01)+3.141592653589793*cos(√(5+(√(-1))))*10^2", preprocessor.process("e^πsin(4.01)+πcos(√(5+i))E2").toString()); - Assert.assertEquals( "2.718281828459045^3.141592653589793*sin(4.01)+3.141592653589793*cos(√(5+(√(-1))))*10^-2", preprocessor.process("e^πsin(4.01)+πcos(√(5+i))E-2").toString()); + Assert.assertEquals( "π*sin(4)*π*cos(√(5))", preprocessor.process("πsin(4)πcos(√(5))").toString()); + Assert.assertEquals( "π*sin(4)+π*cos(√(5))", preprocessor.process("πsin(4)+πcos(√(5))").toString()); + Assert.assertEquals( "π*sin(4)+π*cos(√(5+(√(-1))))", preprocessor.process("πsin(4)+πcos(√(5+i))").toString()); + Assert.assertEquals( "π*sin(4.01)+π*cos(√(5+(√(-1))))", preprocessor.process("πsin(4.01)+πcos(√(5+i))").toString()); + Assert.assertEquals( "e^π*sin(4.01)+π*cos(√(5+(√(-1))))", preprocessor.process("e^πsin(4.01)+πcos(√(5+i))").toString()); + Assert.assertEquals( "e^π*sin(4.01)+π*cos(√(5+(√(-1))))*10^2", preprocessor.process("e^πsin(4.01)+πcos(√(5+i))E2").toString()); + Assert.assertEquals( "e^π*sin(4.01)+π*cos(√(5+(√(-1))))*10^-2", preprocessor.process("e^πsin(4.01)+πcos(√(5+i))E-2").toString()); Assert.assertEquals( "10^2", preprocessor.process("E2").toString()); Assert.assertEquals( "10^-2", preprocessor.process("E-2").toString()); Assert.assertEquals( "10^-1/2", preprocessor.process("E-1/2").toString()); @@ -94,19 +94,19 @@ public class ToJsclTextProcessorTest { Assert.assertEquals( "", preprocessor.process("").toString()); /* try { - Assert.assertEquals( "3.141592653589793/180", preprocessor.process("°").toString()); + Assert.assertEquals( "π/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("1°").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()); + Assert.assertEquals( "1*π/180", preprocessor.process("1°").toString()); + Assert.assertEquals( "20.0*π/180", preprocessor.process("20.0°").toString()); + Assert.assertEquals( "sin(30*π/180)", preprocessor.process("sin(30°)").toString()); + Assert.assertEquals( "asin(sin(π/6))*π/180", preprocessor.process("asin(sin(π/6))°").toString()); + Assert.assertEquals( "1*π/180*sin(1)", preprocessor.process("1°sin(1)").toString()); try { - Assert.assertEquals( "1*3.141592653589793/180^sin(1)", preprocessor.process("1°^sin(1)").toString()); + Assert.assertEquals( "1*π/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!") ) {