new interface implementation

This commit is contained in:
serso 2011-09-11 23:19:08 +04:00
parent cb9e0332a9
commit 23b57fe7f3
4 changed files with 66 additions and 9 deletions

View File

@ -59,7 +59,7 @@
calc:textDown="atg" style="@style/digitButtonStyle" calc:textDown="atg" style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/> a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.DirectionDragButton a:id="@+id/muliplicationButton" a:text="*" <org.solovyev.android.view.DirectionDragButton a:id="@+id/muliplicationButton" a:text="×"
calc:textUp="^" calc:textUp="^"
calc:textDown="^2" style="@style/digitButtonStyle" calc:textDown="^2" style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/> a:onClick="digitButtonClickHandler"/>

View File

@ -205,7 +205,8 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster {
public void pasteButtonClickHandler(@NotNull View v) { public void pasteButtonClickHandler(@NotNull View v) {
final ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); final ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
if ( clipboard.hasText() ) { if ( clipboard.hasText() ) {
editText.getText().append(clipboard.getText()); editText.getText().insert(editText.getSelectionStart(), clipboard.getText());
eval(JsclOperation.numeric, false);
saveHistoryState(); saveHistoryState();
} }
} }

View File

@ -1,6 +1,7 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.util.math.MathEntityType;
public class Preprocessor { public class Preprocessor {
@ -11,12 +12,16 @@ public class Preprocessor {
for (int i = 0; i < s.length(); i++) { for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i); char ch = s.charAt(i);
checkMultiplicationSignBeforeFunction(sb, s, i);
if (ch == '[' || ch == '{') { if (ch == '[' || ch == '{') {
sb.append('('); sb.append('(');
} else if (ch == ']' || ch == '}') { } else if (ch == ']' || ch == '}') {
sb.append(')'); sb.append(')');
} else if (ch == 'π') { } else if (ch == 'π') {
sb.append("pi"); sb.append("pi");
} else if (ch == '×' || ch == '∙') {
sb.append("*");
} else if (s.startsWith("ln", i)) { } else if (s.startsWith("ln", i)) {
sb.append("log"); sb.append("log");
i += 1; i += 1;
@ -41,6 +46,35 @@ public class Preprocessor {
return sb.toString(); 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) { public static String wrap(@NotNull JsclOperation operation, @NotNull String s) {
return operation.name() + "(\"" + s + "\");"; return operation.name() + "(\"" + s + "\");";
} }

View File

@ -1,6 +1,8 @@
package org.solovyev.util.math; package org.solovyev.util.math;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -9,21 +11,37 @@ import org.jetbrains.annotations.Nullable;
public enum MathEntityType { public enum MathEntityType {
digit, digit,
constant,
dot,
function, function,
unary_operation, unary_operation,
binary_operation, binary_operation,
group_symbols, group_symbols,
group_symbol; group_symbol;
private static final List<Character> unaryOperations = Arrays.asList('-', '=', '!'); public static final List<Character> constants = Arrays.asList('e', ');
private static final List<Character> binaryOperations = Arrays.asList('-', '+', '*', '/', '^' ); public static final List<Character> dots = Arrays.asList('.', ',');
private static final List<String> functions = Arrays.asList("sin", "asin", "cos", "acos", "tg", "atg", "log", "ln", "mod", ""); public static final List<Character> unaryOperations = Arrays.asList('-', '=', '!');
private static final List<String> groupSymbols = Arrays.asList("[]", "()", "{}"); public static final List<Character> binaryOperations = Arrays.asList('-', '+', '*', '×', '∙', '/', '^' );
private static final List<Character> singleGroupSymbols = Arrays.asList('[', ']', '(', ')', '{', '}'); public static final List<String> functions = Arrays.asList("sin", "asin", "cos", "acos", "tg", "atg", "log", "ln", "mod", "");
public static final List<String> groupSymbols = Arrays.asList("[]", "()", "{}");
public static final List<Character> openGroupSymbols = Arrays.asList('[', '(', '{');
public static final List<Character> closeGroupSymbols = Arrays.asList(']', ')', '}');
public static final List<Character> singleGroupSymbols;
static {
final List<Character> list = new ArrayList<Character>();
list.addAll(openGroupSymbols);
list.addAll(closeGroupSymbols);
singleGroupSymbols = Collections.unmodifiableList(list);
}
@Nullable @Nullable
public static MathEntityType getType( @NotNull String s ) { public static MathEntityType getType( @NotNull String s ) {
@ -40,6 +58,10 @@ public enum MathEntityType {
result = MathEntityType.binary_operation; result = MathEntityType.binary_operation;
} else if ( singleGroupSymbols.contains(ch) ) { } else if ( singleGroupSymbols.contains(ch) ) {
result = MathEntityType.group_symbol; result = MathEntityType.group_symbol;
} else if ( constants.contains(ch) ) {
result = MathEntityType.constant;
} else if ( dots.contains(ch) ) {
result = MathEntityType.dot;
} }
} }