From cf5f9e2b809b1edf2e0c9478b193f460209a11c4 Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 11 Sep 2011 23:19:08 +0400 Subject: [PATCH] new interface implementation --- res/layout/main.xml | 2 +- .../calculator/CalculatorActivity.java | 3 +- .../android/calculator/Preprocessor.java | 34 ++++++++++++++++++ .../solovyev/util/math/MathEntityType.java | 36 +++++++++++++++---- 4 files changed, 66 insertions(+), 9 deletions(-) diff --git a/res/layout/main.xml b/res/layout/main.xml index 0a1a4c40..8938fac8 100644 --- a/res/layout/main.xml +++ b/res/layout/main.xml @@ -59,7 +59,7 @@ calc:textDown="atg" style="@style/digitButtonStyle" a:onClick="digitButtonClickHandler"/> - diff --git a/src/org/solovyev/android/calculator/CalculatorActivity.java b/src/org/solovyev/android/calculator/CalculatorActivity.java index 116f71b5..75bd56a1 100644 --- a/src/org/solovyev/android/calculator/CalculatorActivity.java +++ b/src/org/solovyev/android/calculator/CalculatorActivity.java @@ -205,7 +205,8 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster { public void pasteButtonClickHandler(@NotNull View v) { final ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); if ( clipboard.hasText() ) { - editText.getText().append(clipboard.getText()); + editText.getText().insert(editText.getSelectionStart(), clipboard.getText()); + eval(JsclOperation.numeric, false); saveHistoryState(); } } diff --git a/src/org/solovyev/android/calculator/Preprocessor.java b/src/org/solovyev/android/calculator/Preprocessor.java index 49701d76..5769b9b7 100644 --- a/src/org/solovyev/android/calculator/Preprocessor.java +++ b/src/org/solovyev/android/calculator/Preprocessor.java @@ -1,6 +1,7 @@ package org.solovyev.android.calculator; import org.jetbrains.annotations.NotNull; +import org.solovyev.util.math.MathEntityType; public class Preprocessor { @@ -11,12 +12,16 @@ public class Preprocessor { for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); + checkMultiplicationSignBeforeFunction(sb, s, i); + if (ch == '[' || ch == '{') { sb.append('('); } else if (ch == ']' || ch == '}') { sb.append(')'); } else if (ch == 'π') { sb.append("pi"); + } else if (ch == '×' || ch == '∙') { + sb.append("*"); } else if (s.startsWith("ln", i)) { sb.append("log"); i += 1; @@ -41,6 +46,35 @@ public class Preprocessor { return sb.toString(); } + private static void checkMultiplicationSignBeforeFunction(@NotNull StringBuilder sb, @NotNull String s, int i) { + if (i > 0) { + // get character before function + char chBefore = s.charAt(i - 1); + char ch = s.charAt(i); + + final MathEntityType mathTypeBefore = MathEntityType.getType(String.valueOf(chBefore)); + final MathEntityType mathType = MathEntityType.getType(String.valueOf(ch)); + + if (mathTypeBefore != MathEntityType.binary_operation && + mathTypeBefore != MathEntityType.unary_operation && + !MathEntityType.openGroupSymbols.contains(chBefore)) { + + if (mathType == MathEntityType.constant) { + sb.append("*"); + } else if (mathType == MathEntityType.digit && mathTypeBefore != MathEntityType.digit && mathTypeBefore != MathEntityType.dot) { + sb.append("*"); + } else { + for (String function : MathEntityType.functions) { + if (s.startsWith(function, i)) { + sb.append("*"); + break; + } + } + } + } + } + } + public static String wrap(@NotNull JsclOperation operation, @NotNull String s) { return operation.name() + "(\"" + s + "\");"; } diff --git a/src/org/solovyev/util/math/MathEntityType.java b/src/org/solovyev/util/math/MathEntityType.java index f7800b55..757b34e7 100644 --- a/src/org/solovyev/util/math/MathEntityType.java +++ b/src/org/solovyev/util/math/MathEntityType.java @@ -1,6 +1,8 @@ package org.solovyev.util.math; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; import org.jetbrains.annotations.NotNull; @@ -9,22 +11,38 @@ import org.jetbrains.annotations.Nullable; public enum MathEntityType { digit, + constant, + dot, function, unary_operation, binary_operation, group_symbols, group_symbol; - private static final List unaryOperations = Arrays.asList('-', '=', '!'); - - private static final List binaryOperations = Arrays.asList('-', '+', '*', '/', '^' ); + public static final List constants = Arrays.asList('e', 'π'); - private static final List functions = Arrays.asList("sin", "asin", "cos", "acos", "tg", "atg", "log", "ln", "mod", "√"); - - private static final List groupSymbols = Arrays.asList("[]", "()", "{}"); + public static final List dots = Arrays.asList('.', ','); - private static final List singleGroupSymbols = Arrays.asList('[', ']', '(', ')', '{', '}'); + public static final List unaryOperations = Arrays.asList('-', '=', '!'); + + public static final List binaryOperations = Arrays.asList('-', '+', '*', '×', '∙', '/', '^' ); + + public static final List functions = Arrays.asList("sin", "asin", "cos", "acos", "tg", "atg", "log", "ln", "mod", "√"); + public static final List groupSymbols = Arrays.asList("[]", "()", "{}"); + + public static final List openGroupSymbols = Arrays.asList('[', '(', '{'); + + public static final List closeGroupSymbols = Arrays.asList(']', ')', '}'); + + public static final List singleGroupSymbols; + static { + final List list = new ArrayList(); + list.addAll(openGroupSymbols); + list.addAll(closeGroupSymbols); + singleGroupSymbols = Collections.unmodifiableList(list); + } + @Nullable public static MathEntityType getType( @NotNull String s ) { MathEntityType result = null; @@ -40,6 +58,10 @@ public enum MathEntityType { result = MathEntityType.binary_operation; } else if ( singleGroupSymbols.contains(ch) ) { result = MathEntityType.group_symbol; + } else if ( constants.contains(ch) ) { + result = MathEntityType.constant; + } else if ( dots.contains(ch) ) { + result = MathEntityType.dot; } }