refactor
This commit is contained in:
@@ -7,18 +7,9 @@ package org.solovyev.android.calculator;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.ColorStateList;
|
||||
import android.text.Html;
|
||||
import android.util.AttributeSet;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
import org.solovyev.common.definitions.Pair;
|
||||
import org.solovyev.common.exceptions.SersoException;
|
||||
import org.solovyev.common.utils.CollectionsUtils;
|
||||
import org.solovyev.util.math.MathEntityType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
|
@@ -10,7 +10,6 @@ import bsh.Interpreter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.common.exceptions.SersoException;
|
||||
import org.solovyev.common.utils.MathUtils;
|
||||
import org.solovyev.common.utils.StringUtils;
|
||||
import org.solovyev.util.math.Complex;
|
||||
|
||||
/**
|
||||
@@ -42,7 +41,7 @@ public class CalculatorModel {
|
||||
String result = String.valueOf(evaluationObject).trim();
|
||||
|
||||
try {
|
||||
result = round(result);
|
||||
result = String.valueOf(round(result));
|
||||
} catch (NumberFormatException e) {
|
||||
if (result.contains("sqrt(-1)")) {
|
||||
try {
|
||||
@@ -82,8 +81,8 @@ public class CalculatorModel {
|
||||
|
||||
int multiplyIndex = s.indexOf("*");
|
||||
if (multiplyIndex >= 0) {
|
||||
complex.setImag(round(s.substring(plusIndex >= 0 ? plusIndex+1 : 0, multiplyIndex)));
|
||||
result += complex.getImag();
|
||||
complex.setImaginary(round(s.substring(plusIndex >= 0 ? plusIndex + 1 : 0, multiplyIndex)));
|
||||
result += complex.getImaginary();
|
||||
|
||||
}
|
||||
|
||||
@@ -92,10 +91,9 @@ public class CalculatorModel {
|
||||
return result;
|
||||
}
|
||||
|
||||
private String round(@NotNull String result) {
|
||||
private Double round(@NotNull String result) {
|
||||
final Double dResult = Double.valueOf(result);
|
||||
result = String.valueOf(MathUtils.round(dResult, NUMBER_OF_FRACTION_DIGITS));
|
||||
return result;
|
||||
return MathUtils.round(dResult, NUMBER_OF_FRACTION_DIGITS);
|
||||
}
|
||||
|
||||
public static class ParseException extends SersoException {
|
||||
|
@@ -19,6 +19,7 @@ import android.widget.Toast;
|
||||
import bsh.EvalError;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.math.MathEntityType;
|
||||
import org.solovyev.android.view.CursorControl;
|
||||
import org.solovyev.android.view.HistoryControl;
|
||||
import org.solovyev.common.utils.MutableObject;
|
||||
@@ -26,7 +27,6 @@ import org.solovyev.common.utils.StringUtils;
|
||||
import org.solovyev.common.utils.history.HistoryAction;
|
||||
import org.solovyev.common.utils.history.HistoryHelper;
|
||||
import org.solovyev.common.utils.history.SimpleHistoryHelper;
|
||||
import org.solovyev.util.math.MathEntityType;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
|
@@ -7,11 +7,10 @@ package org.solovyev.android.calculator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.math.Functions;
|
||||
import org.solovyev.android.calculator.math.MathEntityType;
|
||||
import org.solovyev.common.utils.CollectionsUtils;
|
||||
import org.solovyev.common.utils.EqualsFinder;
|
||||
import org.solovyev.common.utils.Finder;
|
||||
import org.solovyev.util.math.Functions;
|
||||
import org.solovyev.util.math.MathEntityType;
|
||||
|
||||
public class Preprocessor {
|
||||
|
||||
|
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator.math;
|
||||
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/17/11
|
||||
* Time: 10:01 PM
|
||||
*/
|
||||
public interface Functions {
|
||||
|
||||
String SIN = "sin";
|
||||
String SINH = "sinh";
|
||||
String ASIN = "asin";
|
||||
String ASINH = "asinh";
|
||||
String COS = "cos";
|
||||
String COSH = "cosh";
|
||||
String ACOS = "acos";
|
||||
String ACOSH = "acosh";
|
||||
String TAN = "tan";
|
||||
String TANH = "tanh";
|
||||
String ATAN = "atan";
|
||||
String ATANH = "atanh";
|
||||
String LOG = "log";
|
||||
String LN = "ln";
|
||||
String MOD = "mod";
|
||||
String EXP = "exp";
|
||||
String SQRT_SIGN = "√";
|
||||
String SQRT = "sqrt";
|
||||
|
||||
public static final List<String> all = Arrays.asList(SIN, SINH, ASIN, ASINH, COS, COSH, ACOS, ACOSH, TAN, TANH, ATAN, ATANH, LOG, LN, MOD, SQRT, SQRT_SIGN, EXP);
|
||||
}
|
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator.math;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public enum MathEntityType {
|
||||
|
||||
digit,
|
||||
constant,
|
||||
dot,
|
||||
function,
|
||||
unary_operation,
|
||||
binary_operation,
|
||||
group_symbols,
|
||||
group_symbol;
|
||||
|
||||
public static final List<Character> constants = Arrays.asList('e', 'π', 'i');
|
||||
|
||||
public static final List<Character> dots = Arrays.asList('.', ',');
|
||||
|
||||
public static final List<Character> unaryOperations = Arrays.asList('-', '=', '!');
|
||||
|
||||
public static final List<Character> binaryOperations = Arrays.asList('-', '+', '*', '×', '∙', '/', '^' );
|
||||
|
||||
public static final List<String> functions = Functions.all;
|
||||
|
||||
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 ) {
|
||||
MathEntityType result = null;
|
||||
|
||||
if ( s.length() == 1 ) {
|
||||
result = getType(s.charAt(0));
|
||||
}
|
||||
|
||||
if ( result == null ) {
|
||||
if ( functions.contains(s) ) {
|
||||
result = MathEntityType.function;
|
||||
} else if ( groupSymbols.contains(s) ) {
|
||||
result = MathEntityType.group_symbols;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static MathEntityType getType(char ch) {
|
||||
MathEntityType result = null;
|
||||
|
||||
if ( Character.isDigit(ch) ) {
|
||||
result = MathEntityType.digit;
|
||||
} else if ( unaryOperations.contains(ch) ) {
|
||||
result = MathEntityType.unary_operation;
|
||||
} else if ( binaryOperations.contains(ch) ) {
|
||||
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;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user