new interface implementation

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

View File

@ -59,7 +59,7 @@
calc:textDown="atg" style="@style/digitButtonStyle"
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:textDown="^2" style="@style/digitButtonStyle"
a:onClick="digitButtonClickHandler"/>

View File

@ -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();
}
}

View File

@ -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 + "\");";
}

View File

@ -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,21 +11,37 @@ import org.jetbrains.annotations.Nullable;
public enum MathEntityType {
digit,
constant,
dot,
function,
unary_operation,
binary_operation,
group_symbols,
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
public static MathEntityType getType( @NotNull String s ) {
@ -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;
}
}