refactor + russian language support

This commit is contained in:
serso 2011-10-06 23:25:49 +04:00
parent 6b120465b5
commit 647eb27004
28 changed files with 569 additions and 417 deletions

BIN
res/drawable/paste.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

@ -100,8 +100,8 @@
style="@style/control_button_style"
a:onClick="numericButtonClickHandler"/>
<org.solovyev.android.view.widgets.DirectionDragButton a:id="@+id/pasteButton"
a:text="@string/c_paste"
<ImageButton a:id="@+id/pasteButton"
a:src="@drawable/paste"
style="@style/control_button_style"
a:onClick="pasteButtonClickHandler"/>
@ -185,7 +185,8 @@
<org.solovyev.android.view.widgets.DirectionDragButton
a:id="@+id/varsButton"
a:text="@string/c_vars"
a:text="π,e,…"
a:textStyle="italic"
a:onClick="varsButtonClickHandler"
style="@style/control_button_style"/>

View File

@ -130,8 +130,8 @@
style="@style/digit_button_style"
a:onClick="digitButtonClickHandler"/>
<org.solovyev.android.view.widgets.DirectionDragButton a:id="@+id/pasteButton"
a:text="@string/c_paste"
<ImageButton a:id="@+id/pasteButton"
a:src="@drawable/paste"
style="@style/control_button_style"
a:onClick="pasteButtonClickHandler"/>
@ -198,7 +198,8 @@
<org.solovyev.android.view.widgets.DirectionDragButton
a:id="@+id/varsButton"
a:text="@string/c_vars"
a:text="π,e,…"
a:textStyle="italic"
a:onClick="varsButtonClickHandler"
style="@style/control_button_style"/>

View File

@ -1,4 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="c_app_name">Калькулятор</string>
<string name="c_app_settings">Калькулятор</string>
<string name="c_syntax_error">Ошибка</string>
<string name="c_result_copied">Результат скопирован в буфер!</string>
<string name="c_settings">Настройки</string>
<string name="c_help">Помощь</string>
<string name="c_about">О программе</string>
<string name="c_vars_and_constants">Переменные и константы</string>
<!--PREFERENCE ACTIVITY-->
<string name="c_prefs_main_category">Основные настройки</string>
<string name="c_prefs_drag_button_category">Настройки кнопок</string>
<string name="c_down">Вниз</string>
<string name="c_up">Вверх</string>
<string name="c_restart">Перезапуск</string>
<!--ABOUT ACTIVITY-->
<string name="c_copyright">Copyright (c) 2009-2011.\nПрограмма создана serso aka se.solovyev.\n
За подробной информацией, пожалуйста,\nобращайтесь на почту\n<a href="mailto:se.solovyev@gmail.com">se.solovyev@gmail.com</a>
\nили посетите\n<a href="http://se.solovyev.org">http://se.solovyev.org</a>
</string>
<string name="c_undo">назад</string>
<string name="c_redo">вперёд</string>
<string name="c_paste">вставить</string>
<string name="c_vars">переменные</string>
<string name="c_calc_color_display_title">Подсветка выражений</string>
<string name="p_calc_result_precision_title">Точность результата</string>
<string name="c_exit">Выход</string>
<string name="c_add">Добавить</string>
<string name="c_cancel">Отмена</string>
<string name="c_save">Сохранить</string>
<string name="c_remove">Удалить</string>
<string name="c_yes">Да</string>
<string name="c_no">Нет</string>
<string name="c_var_removal_confirmation">Подтверждение удаления</string>
<string name="c_var_removal_confirmation_question">Вы действительно хотите удалить переменную \'%s\'?</string>
<string name="c_var_name">Имя</string>
<string name="c_var_value">Значение</string>
<string name="c_var_description">Описание</string>
<string name="c_var_create_var">Создать переменную</string>
<string name="c_var_edit_var">Редактировать переменную</string>
<string name="c_value.is.not.a.number">Значение - не число!</string>
<string name="c_var.name.clashes">Имя переменной не может быть зарезервированным системным именем!</string>
<string name="c_var.already.exists">Переменная с таким именем уже существует!</string>
<string name="c_name.is.empty">Имя не может быть пустым!</string>
<string name="c_sys.var.cannot.be.changed">Системная переменная не может быть изменена!</string>
<string name="c_pi_description">Отношение длины окружности к диаметру</string>
<string name="c_e_description">Вещесвтенное число, такое что производная функции f(x) = e^x в точке x = 0 равно 1</string>
<string name="c_i_description">Мнимая единица, определённая как i^2 = 1</string>
</resources>

View File

@ -27,7 +27,7 @@
<string name="c_undo">undo</string>
<string name="c_redo">redo</string>
<string name="c_clear">clear</string>
<string name="c_clear">clr</string>
<string name="c_paste">paste</string>
<string name="c_vars">vars</string>
@ -47,4 +47,14 @@
<string name="c_var_description">Description</string>
<string name="c_var_create_var">Create variable</string>
<string name="c_var_edit_var">Edit variable</string>
<string name="c_value.is.not.a.number">Value is not a number!</string>
<string name="c_var.name.clashes">Variable name clashes with function name!</string>
<string name="c_var.already.exists">Variable with same name already exists!</string>
<string name="c_name.is.empty">Name is empty!</string>
<string name="c_sys.var.cannot.be.changed">System variable cannot be changed!</string>
<string name="c_pi_description">Ratio of any circle\'s circumference to its diameter</string>
<string name="c_e_description">Unique real number such that the value of the derivative (slope of the tangent line) of the function f(x) = e^x at the point x = 0 is equal to 1</string>
<string name="c_i_description">Imaginary unit, defined such that i^2 = 1</string>
</resources>

View File

@ -2,7 +2,7 @@
<PreferenceScreen xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:range="http://schemas.android.com/apk/res/org.solovyev.android.calculator">
<PreferenceCategory a:title="@string/c_prefs_drag_button_category">
<PreferenceCategory a:title="@string/c_prefs_main_category">
<org.solovyev.android.view.prefs.NumberPickerDialogPreference
a:key="@string/p_calc_result_precision_key"

View File

@ -18,6 +18,7 @@ import android.widget.TextView;
import bsh.EvalError;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.model.CalculatorModel;
import org.solovyev.android.view.FontSizeAdjuster;
import org.solovyev.android.view.widgets.*;
import org.solovyev.common.BooleanMapper;
@ -41,9 +42,6 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
@NotNull
private CalculatorView calculatorView;
@NotNull
private CalculatorModel calculatorModel;
@NotNull
private BroadcastReceiver insertTextReceiver;
@ -125,16 +123,13 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
private synchronized void firstTimeInit() {
if (!initialized) {
try {
if (!CalculatorModel.isLoaded()) {
CalculatorModel.init(this);
}
this.calculatorModel = CalculatorModel.getInstance();
CalculatorModel.instance.init(this);
} catch (EvalError evalError) {
// todo serso: create serso runtime exception
throw new RuntimeException("Could not initialize interpreter!");
}
this.calculatorView = new CalculatorView(this, this.calculatorModel);
this.calculatorView = new CalculatorView(this, CalculatorModel.instance);
initialized = true;
}
@ -279,7 +274,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, @Nullable String s) {
dpclRegister.announce().onDragPreferencesChange(SimpleOnDragListener.getPreferences(CalculatorActivity.this));
this.calculatorModel.load(this);
CalculatorModel.instance.reset(this);
final Boolean colorExpressionsInBracketsDefault = new BooleanMapper().parseValue(this.getString(R.string.p_calc_color_display));
assert colorExpressionsInBracketsDefault != null;

View File

@ -12,7 +12,7 @@ import android.util.AttributeSet;
import android.util.Log;
import android.widget.EditText;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.math.MathEntityType;
import org.solovyev.android.calculator.math.MathType;
/**
* User: serso
@ -62,9 +62,9 @@ public class CalculatorEditor extends EditText {
final StringBuilder text1 = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
final MathEntityType.Result mathType = MathEntityType.getType(text, i);
final MathType.Result mathType = MathType.getType(text, i);
switch (mathType.getMathEntityType()) {
switch (mathType.getMathType()) {
case open_group_symbol:
numberOfOpenGroupSymbols++;
maxNumberOfOpenGroupSymbols = Math.max(maxNumberOfOpenGroupSymbols, numberOfOpenGroupSymbols);
@ -75,10 +75,10 @@ public class CalculatorEditor extends EditText {
text1.append(text.charAt(i));
break;
case function:
i = processHighlightedText(text1, i, mathType.getS(), "i");
i = processHighlightedText(text1, i, mathType.getMatch(), "i");
break;
case constant:
i = processHighlightedText(text1, i, mathType.getS(), "b");
i = processHighlightedText(text1, i, mathType.getMatch(), "b");
break;
default:
text1.append(text.charAt(i));
@ -113,15 +113,15 @@ public class CalculatorEditor extends EditText {
for (; i < s.length(); i++) {
char ch = s.charAt(i);
if (MathEntityType.openGroupSymbols.contains(ch)) {
if (MathType.openGroupSymbols.contains(ch)) {
result.append(ch);
result.append("</font>");
i = processBracketGroup(result, s, i + 1, numberOfOpenings + 1, maxNumberOfGroups);
result.append("<font color=\"").append(getColor(maxNumberOfGroups, numberOfOpenings)).append("\">");
if (i < s.length() && MathEntityType.closeGroupSymbols.contains(s.charAt(i))) {
if (i < s.length() && MathType.closeGroupSymbols.contains(s.charAt(i))) {
result.append(s.charAt(i));
}
} else if (MathEntityType.closeGroupSymbols.contains(ch)) {
} else if (MathType.closeGroupSymbols.contains(ch)) {
break;
} else {
result.append(ch);

View File

@ -1,212 +0,0 @@
/*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com
*/
package org.solovyev.android.calculator;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import bsh.EvalError;
import bsh.Interpreter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.common.NumberMapper;
import org.solovyev.common.exceptions.SersoException;
import org.solovyev.common.utils.MathUtils;
import org.solovyev.util.math.Complex;
/**
* User: serso
* Date: 9/12/11
* Time: 11:38 PM
*/
public class CalculatorModel {
@NotNull
private Interpreter interpreter;
@NotNull
private final Object interpreterMonitor = new Object();
private int numberOfFractionDigits = 5;
@NotNull
public final Preprocessor preprocessor = new ToJsclPreprocessor();
@NotNull
private final VarsRegister varsRegister = new VarsRegister();
private static CalculatorModel instance;
public CalculatorModel(@Nullable Context context) {
load(context);
reset();
}
public void reset() {
synchronized (interpreterMonitor) {
try {
interpreter = new Interpreter();
interpreter.eval(ToJsclPreprocessor.wrap(JsclOperation.importCommands, "/jscl/editorengine/commands"));
/*for (Var var : varsRegister.getVars()) {
if (!var.isSystem()) {
exec(var.getName() + "=" + var.getValue() + ";");
}
}*/
} catch (EvalError evalError) {
throw new RuntimeException(evalError);
}
}
}
public String evaluate(@NotNull JsclOperation operation, @NotNull String expression) throws EvalError, ParseException {
final StringBuilder sb = new StringBuilder();
sb.append(preprocessor.process(expression));
//Log.d(CalculatorModel.class.getName(), "Preprocessed expression: " + preprocessedExpression);
final Object evaluationObject;
synchronized (interpreterMonitor) {
evaluationObject = interpreter.eval(ToJsclPreprocessor.wrap(operation, sb.toString()));
}
String result = String.valueOf(evaluationObject).trim();
try {
result = String.valueOf(round(result));
} catch (NumberFormatException e) {
if (result.contains("sqrt(-1)")) {
try {
result = createResultForComplexNumber(result.replace("sqrt(-1)", "i"));
} catch (NumberFormatException e1) {
// throw original one
throw new ParseException(e);
}
} else {
throw new ParseException(e);
}
}
return result;
}
public String createResultForComplexNumber(@NotNull final String s) {
final Complex complex = new Complex();
String result = "";
// may be it's just complex number
int plusIndex = s.lastIndexOf("+");
if (plusIndex >= 0) {
complex.setReal(round(s.substring(0, plusIndex)));
result += complex.getReal();
result += "+";
} else {
plusIndex = s.lastIndexOf("-");
if (plusIndex >= 0) {
complex.setReal(round(s.substring(0, plusIndex)));
result += complex.getReal();
result += "-";
}
}
int multiplyIndex = s.indexOf("*");
if (multiplyIndex >= 0) {
complex.setImaginary(round(s.substring(plusIndex >= 0 ? plusIndex + 1 : 0, multiplyIndex)));
result += complex.getImaginary();
}
result += "i";
return result;
}
private Double round(@NotNull String result) {
final Double dResult = Double.valueOf(result);
return MathUtils.round(dResult, numberOfFractionDigits);
}
public synchronized void load(@Nullable Context context) {
if (context != null) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
final NumberMapper<Integer> integerNumberMapper = new NumberMapper<Integer>(Integer.class);
this.setNumberOfFractionDigits(integerNumberMapper.parseValue(preferences.getString(context.getString(R.string.p_calc_result_precision_key), context.getString(R.string.p_calc_result_precision))));
}
varsRegister.load(context);
}
public static class ParseException extends SersoException {
public ParseException(Throwable cause) {
super(cause);
}
}
public int getNumberOfFractionDigits() {
return numberOfFractionDigits;
}
public void setNumberOfFractionDigits(int numberOfFractionDigits) {
this.numberOfFractionDigits = numberOfFractionDigits;
}
public static synchronized void init(@Nullable Context context) throws EvalError {
if (!isLoaded()) {
instance = new CalculatorModel(context);
} else {
throw new RuntimeException("Calculator model already instantiated!");
}
}
public static CalculatorModel getInstance() {
if (!isLoaded()) {
throw new RuntimeException("CalculatorModel must be instantiated!");
}
return instance;
}
public static boolean isLoaded() {
return instance != null;
}
private void exec(String str) throws EvalError {
interpreter.eval(str);
}
private String eval(String str) throws EvalError {
return interpreter.eval(commands(str)).toString();
}
@NotNull
public VarsRegister getVarsRegister() {
return varsRegister;
}
String commands(String str) {
return commands(str, false);
}
String commands(String str, boolean found) {
for (int i = 0; i < cmds.length; i++) {
int n = str.length() - cmds[i].length() - 1;
if (n >= 0 && (" " + cmds[i].toLowerCase()).equals(str.substring(n)))
return commands(str.substring(0, n), true) + "." + cmds[i] + "()";
}
str = str.replaceAll("\n", "");
return found ? "jscl.math.Expression.valueOf(\"" + str + "\")" : str;
}
static final String cmds[] = new String[]{"expand", "factorize", "elementary", "simplify", "numeric", "toMathML", "toJava"};
}

View File

@ -16,7 +16,10 @@ import android.view.*;
import android.widget.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.math.MathEntityType;
import org.solovyev.android.calculator.math.MathType;
import org.solovyev.android.calculator.model.CalculatorModel;
import org.solovyev.android.calculator.model.Var;
import org.solovyev.android.calculator.model.VarsRegister;
import org.solovyev.common.utils.StringUtils;
import java.util.ArrayList;
@ -36,7 +39,7 @@ public class CalculatorVarsActivity extends ListActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter = new VarsArrayAdapter(this, R.layout.var, R.id.var_text, new ArrayList<Var>(CalculatorModel.getInstance().getVarsRegister().getVars()));
adapter = new VarsArrayAdapter(this, R.layout.var, R.id.var_text, new ArrayList<Var>(CalculatorModel.instance.getVarsRegister().getVars()));
setListAdapter(adapter);
final ListView lv = getListView();
@ -111,7 +114,7 @@ public class CalculatorVarsActivity extends ListActivity {
builder.create().show();
} else {
Toast.makeText(this, "System variable cannot be changed!", Toast.LENGTH_LONG).show();
Toast.makeText(this, getString(R.string.c_sys_var_cannot_be_changed), Toast.LENGTH_LONG).show();
}
}
@ -135,7 +138,7 @@ public class CalculatorVarsActivity extends ListActivity {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
final String error;
final Integer error;
final EditText editName = (EditText) editView.findViewById(R.id.var_edit_name);
String name = editName.getText().toString();
@ -147,13 +150,13 @@ public class CalculatorVarsActivity extends ListActivity {
String description = editDescription.getText().toString();
final VarsRegister varsRegister = CalculatorModel.getInstance().getVarsRegister();
final VarsRegister varsRegister = CalculatorModel.instance.getVarsRegister();
if (!StringUtils.isEmpty(name)) {
final Var varFromRegister = varsRegister.getVar(name);
if (varFromRegister == null || varFromRegister == editedInstance) {
final MathEntityType.Result mathType = MathEntityType.getType(name, 0);
final MathType.Result mathType = MathType.getType(name, 0);
if (mathType.getMathEntityType() == MathEntityType.text || mathType.getMathEntityType() == MathEntityType.constant) {
if (mathType.getMathType() == MathType.text || mathType.getMathType() == MathType.constant) {
boolean correctDouble = true;
try {
Double.valueOf(value);
@ -167,20 +170,20 @@ public class CalculatorVarsActivity extends ListActivity {
varBuilder.setDescription(description);
error = null;
} else {
error = "Value is not a number!";
error = R.string.c_value_is_not_a_number;
}
} else {
error = "Variable name clashes with function name!";
error = R.string.c_var_name_clashes;
}
} else {
error = "Variable with same name already exist!";
error = R.string.c_var_already_exists;
}
} else {
error = "Name is empty!";
error = R.string.c_name_is_empty;
}
if (error != null) {
Toast.makeText(CalculatorVarsActivity.this, error, Toast.LENGTH_LONG).show();
Toast.makeText(CalculatorVarsActivity.this, getString(error), Toast.LENGTH_LONG).show();
createEditVariableDialog(editedInstance, name, value, description);
} else {
if ( editedInstance == null ) {
@ -290,7 +293,7 @@ public class CalculatorVarsActivity extends ListActivity {
builder.create().show();
} else {
adapter.remove(var);
final VarsRegister varsRegister = CalculatorModel.getInstance().getVarsRegister();
final VarsRegister varsRegister = CalculatorModel.instance.getVarsRegister();
varsRegister.remove(var);
varsRegister.save(CalculatorVarsActivity.this);
CalculatorVarsActivity.this.adapter.notifyDataSetChanged();

View File

@ -19,7 +19,9 @@ 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.calculator.math.MathType;
import org.solovyev.android.calculator.model.CalculatorModel;
import org.solovyev.android.calculator.model.ParseException;
import org.solovyev.android.view.CursorControl;
import org.solovyev.android.view.HistoryControl;
import org.solovyev.common.utils.MutableObject;
@ -36,7 +38,7 @@ import org.solovyev.common.utils.history.SimpleHistoryHelper;
public class CalculatorView implements CursorControl, HistoryControl<CalculatorHistoryState> {
// millis to wait before evaluation after user edit action
public static final int EVAL_DELAY_MILLIS = 500;
public static final int EVAL_DELAY_MILLIS = 700;
@NotNull
private final CalculatorEditor editor;
@ -44,9 +46,6 @@ public class CalculatorView implements CursorControl, HistoryControl<CalculatorH
@NotNull
private final CalculatorDisplay display;
@NotNull
private final Activity activity;
@NotNull
private final CalculatorModel calculatorModel;
@ -54,7 +53,6 @@ public class CalculatorView implements CursorControl, HistoryControl<CalculatorH
private final HistoryHelper<CalculatorHistoryState> history;
public CalculatorView(@NotNull final Activity activity, @NotNull CalculatorModel calculator) {
this.activity = activity;
this.calculatorModel = calculator;
final InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
@ -159,7 +157,7 @@ public class CalculatorView implements CursorControl, HistoryControl<CalculatorH
localDisplay.setText(calculatorModel.evaluate(JsclOperation.numeric, expression));
} catch (EvalError e) {
handleEvaluationException(expression, localDisplay, e);
} catch (CalculatorModel.ParseException e) {
} catch (ParseException e) {
handleEvaluationException(expression, localDisplay, e);
}
}
@ -192,11 +190,11 @@ public class CalculatorView implements CursorControl, HistoryControl<CalculatorH
@Override
public void doOperation(@NotNull EditText editor) {
final MathEntityType.Result mathType = MathEntityType.getType(text, 0);
final MathType.Result mathType = MathType.getType(text, 0);
int cursorPositionOffset = 0;
final StringBuilder textToBeInserted = new StringBuilder(text);
switch (mathType.getMathEntityType()) {
switch (mathType.getMathType()) {
case function:
textToBeInserted.append("()");
cursorPositionOffset = -1;

View File

@ -1,14 +0,0 @@
package org.solovyev.android.calculator;
import org.jetbrains.annotations.NotNull;
/**
* User: serso
* Date: 9/26/11
* Time: 12:12 PM
*/
public interface Preprocessor {
@NotNull
String process(@NotNull String s);
}

View File

@ -14,6 +14,11 @@ import java.util.*;
*/
public class Functions {
// not intended for instantiation
private Functions() {
throw new AssertionError("Not allowed!");
}
public final static String SIN = "sin";
public final static String SINH = "sinh";
public final static String ASIN = "asin";
@ -45,8 +50,4 @@ public class Functions {
public final static Character DEGREE = '°';
public static final List<Character> allPostfix = Arrays.asList(FACT, DEGREE);
private Functions() {
throw new AssertionError("Not allowed!");
}
}

View File

@ -6,9 +6,9 @@
package org.solovyev.android.calculator.math;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.CalculatorModel;
import org.solovyev.android.calculator.CharacterAtPositionFinder;
import org.solovyev.android.calculator.StartsWithFinder;
import org.solovyev.android.calculator.model.CalculatorModel;
import org.solovyev.common.utils.Finder;
import java.util.Arrays;
@ -16,7 +16,7 @@ import java.util.List;
import static org.solovyev.common.utils.CollectionsUtils.get;
public enum MathEntityType {
public enum MathType {
digit,
constant,
@ -30,7 +30,12 @@ public enum MathEntityType {
close_group_symbol,
text;
public static final List<String> constants = Arrays.asList("e", "π", "i");
public static final String IMAGINARY_NUMBER = "i";
public static final String IMAGINARY_NUMBER_DEF = "sqrt(-1)";
public static final String PI = "π";
public static final String E = "e";
public static final List<String> constants = Arrays.asList(E, PI, IMAGINARY_NUMBER);
public static final List<String> digits = Arrays.asList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
@ -57,11 +62,6 @@ public enum MathEntityType {
* @param i index which points to start of substring
* @return math entity type of substring starting from ith index of specified text
*/
@NotNull
public static MathEntityType getMathEntityType(@NotNull String text, int i) {
return getType(text, i).getMathEntityType();
}
@NotNull
public static Result getType(@NotNull String text, int i) {
if (i < 0) {
@ -69,7 +69,7 @@ public enum MathEntityType {
} else if (i >= text.length() && i != 0) {
throw new IllegalArgumentException("I must be less than size of text.");
} else if (i == 0 && text.length() == 0) {
return new Result(MathEntityType.text, text);
return new Result(MathType.text, text);
}
final StartsWithFinder stringStartWithFinder = new StartsWithFinder(text, i);
@ -77,7 +77,7 @@ public enum MathEntityType {
String foundString = get(digits, stringStartWithFinder);
if (foundString != null) {
return new Result(MathEntityType.digit, foundString);
return new Result(MathType.digit, foundString);
}
Character foundCharacter = get(dots, characterStartWithFinder);
@ -102,7 +102,7 @@ public enum MathEntityType {
foundString = get(groupSymbols, stringStartWithFinder);
if (foundString != null) {
return new Result(MathEntityType.group_symbols, foundString);
return new Result(MathType.group_symbols, foundString);
}
foundCharacter = get(openGroupSymbols, characterStartWithFinder);
@ -117,39 +117,39 @@ public enum MathEntityType {
foundString = get(prefixFunctions, stringStartWithFinder);
if (foundString != null) {
return new Result(MathEntityType.function, foundString);
return new Result(MathType.function, foundString);
}
foundString = get(CalculatorModel.getInstance().getVarsRegister().getVarNames(), stringStartWithFinder);
foundString = get(CalculatorModel.instance.getVarsRegister().getVarNames(), stringStartWithFinder);
if (foundString != null) {
return new Result(MathEntityType.constant, foundString);
return new Result(MathType.constant, foundString);
}
return new Result(MathEntityType.text, text.substring(i));
return new Result(MathType.text, text.substring(i));
}
public static class Result {
@NotNull
private final MathEntityType mathEntityType;
private final MathType mathType;
@NotNull
private final String s;
private final String match;
private Result(@NotNull MathEntityType mathEntityType, @NotNull String s){
this.mathEntityType = mathEntityType;
private Result(@NotNull MathType mathType, @NotNull String match){
this.mathType = mathType;
this.s = s;
this.match = match;
}
@NotNull
public String getS() {
return s;
public String getMatch() {
return match;
}
@NotNull
public MathEntityType getMathEntityType() {
return mathEntityType;
public MathType getMathType() {
return mathType;
}
}

View File

@ -0,0 +1,131 @@
/*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com
*/
package org.solovyev.android.calculator.model;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import bsh.EvalError;
import bsh.Interpreter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.JsclOperation;
import org.solovyev.common.NumberMapper;
/**
* User: serso
* Date: 9/12/11
* Time: 11:38 PM
*/
public enum CalculatorModel {
instance;
private static final String RESULT_PRECISION_P_KEY = "org.solovyev.android.calculator.CalculatorModel_result_precision";
private static final String RESULT_PRECISION_DEFAULT = "5";
@NotNull
private Interpreter interpreter;
@NotNull
private final Object lock = new Object();
private int numberOfFractionDigits = 5;
@NotNull
public final TextProcessor preprocessor = new ToJsclTextProcessor();
@NotNull
public final TextProcessor postprocessor = new FromJsclTextProcessor();
@NotNull
private final VarsRegisterImpl varsRegister = new VarsRegisterImpl();
public String evaluate(@NotNull JsclOperation operation, @NotNull String expression) throws EvalError, ParseException {
synchronized (lock) {
final StringBuilder sb = new StringBuilder();
sb.append(preprocessor.process(expression));
//Log.d(CalculatorModel.class.getName(), "Preprocessed expression: " + preprocessedExpression);
final Object evaluationObject = interpreter.eval(ToJsclTextProcessor.wrap(operation, sb.toString()));
return postprocessor.process(String.valueOf(evaluationObject).trim());
}
}
public int getNumberOfFractionDigits() {
return numberOfFractionDigits;
}
public void setNumberOfFractionDigits(int numberOfFractionDigits) {
this.numberOfFractionDigits = numberOfFractionDigits;
}
public void init(@Nullable Context context) throws EvalError {
synchronized (lock) {
reset(context);
resetInterpreter();
}
}
public void reset(@Nullable Context context) {
synchronized (lock) {
if (context != null) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
final NumberMapper<Integer> integerNumberMapper = new NumberMapper<Integer>(Integer.class);
//noinspection ConstantConditions
this.setNumberOfFractionDigits(integerNumberMapper.parseValue(preferences.getString(RESULT_PRECISION_P_KEY, RESULT_PRECISION_DEFAULT)));
}
varsRegister.init(context);
}
}
public void resetInterpreter() {
synchronized (lock) {
try {
interpreter = new Interpreter();
interpreter.eval(ToJsclTextProcessor.wrap(JsclOperation.importCommands, "/jscl/editorengine/commands"));
} catch (EvalError evalError) {
throw new RuntimeException(evalError);
}
}
}
@NotNull
public VarsRegister getVarsRegister() {
return varsRegister;
}
/* private String commands(String str) {
return commands(str, false);
}
private void exec(String str) throws EvalError {
interpreter.eval(str);
}
private String eval(String str) throws EvalError {
return interpreter.eval(commands(str)).toString();
}
private String commands(String str, boolean found) {
for (int i = 0; i < cmds.length; i++) {
int n = str.length() - cmds[i].length() - 1;
if (n >= 0 && (" " + cmds[i].toLowerCase()).equals(str.substring(n)))
return commands(str.substring(0, n), true) + "." + cmds[i] + "()";
}
str = str.replaceAll("\n", "");
return found ? "jscl.math.Expression.valueOf(\"" + str + "\")" : str;
}
private static final String cmds[] = new String[]{"expand", "factorize", "elementary", "simplify", "numeric", "toMathML", "toJava"};*/
}

View File

@ -0,0 +1,81 @@
/*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com
* or visit http://se.solovyev.org
*/
package org.solovyev.android.calculator.model;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.math.MathType;
import org.solovyev.common.utils.MathUtils;
import org.solovyev.util.math.Complex;
/**
* User: serso
* Date: 10/6/11
* Time: 9:48 PM
*/
class FromJsclTextProcessor implements TextProcessor {
@NotNull
@Override
public String process(@NotNull String s) throws ParseException {
String result = null;
try {
result = String.valueOf(round(s));
} catch (NumberFormatException e) {
if (result.contains(MathType.IMAGINARY_NUMBER_DEF)) {
try {
result = createResultForComplexNumber(result.replace(MathType.IMAGINARY_NUMBER_DEF, MathType.IMAGINARY_NUMBER));
} catch (NumberFormatException e1) {
// throw original one
throw new ParseException(e);
}
} else {
throw new ParseException(e);
}
}
return result;
}
protected String createResultForComplexNumber(@NotNull final String s) {
final Complex complex = new Complex();
String result = "";
// may be it's just complex number
int plusIndex = s.lastIndexOf("+");
if (plusIndex >= 0) {
complex.setReal(round(s.substring(0, plusIndex)));
result += complex.getReal();
result += "+";
} else {
plusIndex = s.lastIndexOf("-");
if (plusIndex >= 0) {
complex.setReal(round(s.substring(0, plusIndex)));
result += complex.getReal();
result += "-";
}
}
int multiplyIndex = s.indexOf("*");
if (multiplyIndex >= 0) {
complex.setImaginary(round(s.substring(plusIndex >= 0 ? plusIndex + 1 : 0, multiplyIndex)));
result += complex.getImaginary();
}
result += MathType.IMAGINARY_NUMBER;
return result;
}
private Double round(@NotNull String result) {
final Double dResult = Double.valueOf(result);
return MathUtils.round(dResult, CalculatorModel.instance.getNumberOfFractionDigits());
}
}

View File

@ -0,0 +1,20 @@
/*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com
* or visit http://se.solovyev.org
*/
package org.solovyev.android.calculator.model;
import org.solovyev.common.exceptions.SersoException;
/**
* User: serso
* Date: 10/6/11
* Time: 9:25 PM
*/
public class ParseException extends SersoException {
public ParseException(Throwable cause) {
super(cause);
}
}

View File

@ -0,0 +1,14 @@
package org.solovyev.android.calculator.model;
import org.jetbrains.annotations.NotNull;
/**
* User: serso
* Date: 9/26/11
* Time: 12:12 PM
*/
public interface TextProcessor {
@NotNull
String process(@NotNull String s) throws ParseException;
}

View File

@ -4,17 +4,19 @@
* or visit http://se.solovyev.org
*/
package org.solovyev.android.calculator;
package org.solovyev.android.calculator.model;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.JsclOperation;
import org.solovyev.android.calculator.StartsWithFinder;
import org.solovyev.android.calculator.math.Functions;
import org.solovyev.android.calculator.math.MathEntityType;
import org.solovyev.android.calculator.math.MathType;
import org.solovyev.common.utils.CollectionsUtils;
import org.solovyev.common.utils.FilterType;
import org.solovyev.common.utils.Finder;
public class ToJsclPreprocessor implements Preprocessor {
class ToJsclTextProcessor implements TextProcessor {
@Override
@NotNull
@ -23,26 +25,26 @@ public class ToJsclPreprocessor implements Preprocessor {
final StartsWithFinder startsWithFinder = new StartsWithFinder(s, 0);
final StringBuilder sb = new StringBuilder();
MathEntityType.Result mathTypeResult = null;
MathType.Result mathTypeResult = null;
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
startsWithFinder.setI(i);
mathTypeResult = checkMultiplicationSignBeforeFunction(sb, s, i, mathTypeResult);
final MathEntityType mathType = mathTypeResult.getMathEntityType();
if (mathType == MathEntityType.open_group_symbol) {
final MathType mathType = mathTypeResult.getMathType();
if (mathType == MathType.open_group_symbol) {
sb.append('(');
} else if (mathType == MathEntityType.close_group_symbol) {
} else if (mathType == MathType.close_group_symbol) {
sb.append(')');
} else if (ch == '×' || ch == '∙') {
sb.append("*");
} else if ( mathType == MathEntityType.function ){
sb.append(toJsclFunction(mathTypeResult.getS()));
i += mathTypeResult.getS().length() - 1;
} else if ( mathType == MathEntityType.constant ) {
sb.append(mathTypeResult.getS());
i += mathTypeResult.getS().length() - 1;
} else if ( mathType == MathType.function ){
sb.append(toJsclFunction(mathTypeResult.getMatch()));
i += mathTypeResult.getMatch().length() - 1;
} else if ( mathType == MathType.constant ) {
sb.append(mathTypeResult.getMatch());
i += mathTypeResult.getMatch().length() - 1;
} else {
sb.append(ch);
}
@ -59,11 +61,11 @@ public class ToJsclPreprocessor implements Preprocessor {
startsWithFinder.setI(i);
int offset = 0;
String functionName = CollectionsUtils.get(MathEntityType.prefixFunctions, startsWithFinder);
String functionName = CollectionsUtils.get(MathType.prefixFunctions, startsWithFinder);
if (functionName == null) {
String varName = CollectionsUtils.get(CalculatorModel.getInstance().getVarsRegister().getVarNames(), startsWithFinder);
String varName = CollectionsUtils.get(CalculatorModel.instance.getVarsRegister().getVarNames(), startsWithFinder);
if (varName != null) {
final Var var = CalculatorModel.getInstance().getVarsRegister().getVar(varName);
final Var var = CalculatorModel.instance.getVarsRegister().getVar(varName);
if (var != null) {
result.append(var.getValue());
offset = varName.length();
@ -86,10 +88,10 @@ public class ToJsclPreprocessor implements Preprocessor {
}
private void replaceVariables(StringBuilder sb, String s, int i, @NotNull StartsWithFinder startsWithFinder) {
for (Var var : CalculatorModel.getInstance().getVarsRegister().getVars()) {
for (Var var : CalculatorModel.instance.getVarsRegister().getVars()) {
if (!var.isSystem()) {
if (s.startsWith(var.getName(), i)) {
if (CollectionsUtils.get(MathEntityType.prefixFunctions, startsWithFinder) == null) {
if (CollectionsUtils.get(MathType.prefixFunctions, startsWithFinder) == null) {
}
}
}
@ -103,13 +105,13 @@ public class ToJsclPreprocessor implements Preprocessor {
int result = position;
for (; result >= 0; result--) {
final MathEntityType mathEntityType = MathEntityType.getMathEntityType(s, result);
final MathType mathType = MathType.getType(s, result).getMathType();
if (CollectionsUtils.contains(mathEntityType, MathEntityType.digit, MathEntityType.dot)) {
if (CollectionsUtils.contains(mathType, MathType.digit, MathType.dot)) {
// continue
} else if (mathEntityType == MathEntityType.close_group_symbol) {
} else if (mathType == MathType.close_group_symbol) {
numberOfOpenGroups++;
} else if (mathEntityType == MathEntityType.open_group_symbol) {
} else if (mathType == MathType.open_group_symbol) {
numberOfOpenGroups--;
} else {
if (stop(s, numberOfOpenGroups, result)) break;
@ -124,9 +126,9 @@ public class ToJsclPreprocessor implements Preprocessor {
if (i > 0) {
final EndsWithFinder endsWithFinder = new EndsWithFinder(s);
endsWithFinder.setI(i + 1);
if (!CollectionsUtils.contains(MathEntityType.prefixFunctions, FilterType.included, endsWithFinder)) {
MathEntityType type = MathEntityType.getMathEntityType(s, i);
if (type != MathEntityType.constant) {
if (!CollectionsUtils.contains(MathType.prefixFunctions, FilterType.included, endsWithFinder)) {
MathType type = MathType.getType(s, i).getMathType();
if (type != MathType.constant) {
return true;
}
}
@ -175,31 +177,31 @@ public class ToJsclPreprocessor implements Preprocessor {
}
@NotNull
private static MathEntityType.Result checkMultiplicationSignBeforeFunction(@NotNull StringBuilder sb,
private static MathType.Result checkMultiplicationSignBeforeFunction(@NotNull StringBuilder sb,
@NotNull String s,
int i,
@Nullable MathEntityType.Result mathTypeBeforeResult) {
MathEntityType.Result result = MathEntityType.getType(s, i);
@Nullable MathType.Result mathTypeBeforeResult) {
MathType.Result result = MathType.getType(s, i);
if (i > 0) {
final MathEntityType mathType = result.getMathEntityType();
final MathType mathType = result.getMathType();
assert mathTypeBeforeResult != null;
final MathEntityType mathTypeBefore = mathTypeBeforeResult.getMathEntityType();
final MathType mathTypeBefore = mathTypeBeforeResult.getMathType();
if (mathTypeBefore == MathEntityType.constant || (mathTypeBefore != MathEntityType.binary_operation &&
mathTypeBefore != MathEntityType.unary_operation &&
mathTypeBefore != MathEntityType.function &&
mathTypeBefore != MathEntityType.open_group_symbol)) {
if (mathTypeBefore == MathType.constant || (mathTypeBefore != MathType.binary_operation &&
mathTypeBefore != MathType.unary_operation &&
mathTypeBefore != MathType.function &&
mathTypeBefore != MathType.open_group_symbol)) {
if (mathType == MathEntityType.constant) {
if (mathType == MathType.constant) {
sb.append("*");
} else if (mathType == MathEntityType.open_group_symbol && mathTypeBefore != null) {
} else if (mathType == MathType.open_group_symbol && mathTypeBefore != null) {
sb.append("*");
} else if (mathType == MathEntityType.digit && ((mathTypeBefore != MathEntityType.digit && mathTypeBefore != MathEntityType.dot) || mathTypeBefore == MathEntityType.constant)) {
} else if (mathType == MathType.digit && ((mathTypeBefore != MathType.digit && mathTypeBefore != MathType.dot) || mathTypeBefore == MathType.constant)) {
sb.append("*");
} else {
for (String function : MathEntityType.prefixFunctions) {
for (String function : MathType.prefixFunctions) {
if (s.startsWith(function, i)) {
sb.append("*");
break;

View File

@ -4,7 +4,7 @@
* or visit http://se.solovyev.org
*/
package org.solovyev.android.calculator;
package org.solovyev.android.calculator.model;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

View File

@ -1,4 +1,4 @@
package org.solovyev.android.calculator;
package org.solovyev.android.calculator.model;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

View File

@ -0,0 +1,41 @@
/*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com
* or visit http://se.solovyev.org
*/
package org.solovyev.android.calculator.model;
import android.content.Context;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
/**
* User: serso
* Date: 10/6/11
* Time: 9:31 PM
*/
public interface VarsRegister {
@NotNull
List<Var> getVars();
@NotNull
List<Var> getSystemVars();
Var addVar(@Nullable String name, @NotNull Var.Builder builder);
void remove(@NotNull Var var);
@NotNull
List<String> getVarNames();
@Nullable
Var getVar(@NotNull String name);
boolean contains(@NotNull String name);
void save(@NotNull Context context);
}

View File

@ -4,7 +4,7 @@
* or visit http://se.solovyev.org
*/
package org.solovyev.android.calculator;
package org.solovyev.android.calculator.model;
import android.content.Context;
import android.content.SharedPreferences;
@ -13,8 +13,9 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.math.MathEntityComparator;
import org.solovyev.android.calculator.math.MathEntityType;
import org.solovyev.android.calculator.math.MathType;
import org.solovyev.common.utils.CollectionsUtils;
import org.solovyev.common.utils.Finder;
@ -26,7 +27,7 @@ import java.util.*;
* Date: 9/29/11
* Time: 4:57 PM
*/
public class VarsRegister {
class VarsRegisterImpl implements VarsRegister {
@NotNull
private final List<Var> vars = new ArrayList<Var>();
@ -34,16 +35,22 @@ public class VarsRegister {
@NotNull
private final List<Var> systemVars = new ArrayList<Var>();
protected VarsRegisterImpl() {
}
@Override
@NotNull
public List<Var> getVars() {
return Collections.unmodifiableList(vars);
}
@Override
@NotNull
public List<Var> getSystemVars() {
return Collections.unmodifiableList(systemVars);
}
@Override
public Var addVar(@Nullable String name, @NotNull Var.Builder builder) {
final Var var = builder.create();
@ -58,10 +65,12 @@ public class VarsRegister {
return varFromRegister;
}
@Override
public void remove(@NotNull Var var) {
this.vars.remove(var);
}
@Override
@NotNull
public List<String> getVarNames() {
final List<String> result = new ArrayList<String>();
@ -75,6 +84,7 @@ public class VarsRegister {
return result;
}
@Override
@Nullable
public Var getVar(@NotNull final String name) {
return CollectionsUtils.get(vars, new Finder<Var>() {
@ -85,6 +95,7 @@ public class VarsRegister {
});
}
@Override
public boolean contains(@NotNull final String name) {
return CollectionsUtils.get(vars, new Finder<Var>() {
@Override
@ -107,7 +118,7 @@ public class VarsRegister {
vars.addAll(result);
}
public synchronized void load(@Nullable Context context) {
synchronized void init(@Nullable Context context) {
this.vars.clear();
this.systemVars.clear();
@ -128,19 +139,32 @@ public class VarsRegister {
}
for (String systemVarName : MathEntityType.constants) {
for (String systemVarName : MathType.constants) {
final Var systemVar;
if ( systemVarName.equals("e") ){
systemVar = new Var.Builder(systemVarName, Math.E).setSystem(true).create();
} else if (systemVarName.equals("π")) {
systemVar = new Var.Builder(systemVarName, Math.PI).setSystem(true).create();
} else if (systemVarName.equals("i")) {
systemVar = new Var.Builder(systemVarName, "sqrt(-1)").setSystem(true).create();
final Var.Builder builder;
final Integer varDescription;
if ( systemVarName.equals(MathType.E) ){
builder = new Var.Builder(systemVarName, Math.E);
varDescription = R.string.c_e_description;
} else if (systemVarName.equals(MathType.PI)) {
builder = new Var.Builder(systemVarName, Math.PI);
varDescription = R.string.c_pi_description;
} else if (systemVarName.equals(MathType.IMAGINARY_NUMBER)) {
builder = new Var.Builder(systemVarName, MathType.IMAGINARY_NUMBER_DEF);
varDescription = R.string.c_i_description;
} else {
throw new IllegalArgumentException(systemVarName + " is not supported yet!");
}
builder.setSystem(true);
if (context != null) {
builder.setDescription(context.getString(varDescription));
}
final Var systemVar = builder.create();
systemVars.add(systemVar);
if (!vars.contains(systemVar)) {
vars.add(systemVar);
@ -153,6 +177,7 @@ public class VarsRegister {
}*/
}
@Override
public synchronized void save(@NotNull Context context) {
final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
final SharedPreferences.Editor editor = settings.edit();

View File

@ -80,12 +80,12 @@ public class AutoResizeTextView extends TextView {
}
/**
* When text changes, set the force resize flag to true and reset the text size.
* When text changes, set the force resize flag to true and resetInterpreter the text size.
*/
@Override
protected void onTextChanged(final CharSequence text, final int start, final int before, final int after) {
mNeedsResize = true;
// Since this view may be reused, it is good to reset the text size
// Since this view may be reused, it is good to resetInterpreter the text size
resetTextSize();
}

View File

@ -1,45 +0,0 @@
/*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com
* or visit http://se.solovyev.org
*/
package org.solovyev.android.calculator.math;
import junit.framework.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.solovyev.android.calculator.CalculatorModel;
/**
* User: serso
* Date: 10/5/11
* Time: 1:25 AM
*/
public class MathEntityTypeTest {
@BeforeClass
public static void setUp() throws Exception {
CalculatorModel.init(null);
}
@Test
public void testGetType() throws Exception {
Assert.assertEquals(MathEntityType.function, MathEntityType.getType("sin", 0).getMathEntityType());
Assert.assertEquals(MathEntityType.text, MathEntityType.getType("sn", 0).getMathEntityType());
Assert.assertEquals(MathEntityType.text, MathEntityType.getType("s", 0).getMathEntityType());
Assert.assertEquals(MathEntityType.text, MathEntityType.getType("", 0).getMathEntityType());
try {
Assert.assertEquals(MathEntityType.text, MathEntityType.getType("22", -1).getMathEntityType());
Assert.fail();
} catch (IllegalArgumentException e) {
}
try {
Assert.assertEquals(MathEntityType.text, MathEntityType.getType("22", 2).getMathEntityType());
Assert.fail();
} catch (IllegalArgumentException e) {
}
}
}

View File

@ -0,0 +1,45 @@
/*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com
* or visit http://se.solovyev.org
*/
package org.solovyev.android.calculator.math;
import junit.framework.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.solovyev.android.calculator.model.CalculatorModel;
/**
* User: serso
* Date: 10/5/11
* Time: 1:25 AM
*/
public class MathTypeTest {
@BeforeClass
public static void setUp() throws Exception {
CalculatorModel.instance.init(null);
}
@Test
public void testGetType() throws Exception {
Assert.assertEquals(MathType.function, MathType.getType("sin", 0).getMathType());
Assert.assertEquals(MathType.text, MathType.getType("sn", 0).getMathType());
Assert.assertEquals(MathType.text, MathType.getType("s", 0).getMathType());
Assert.assertEquals(MathType.text, MathType.getType("", 0).getMathType());
try {
Assert.assertEquals(MathType.text, MathType.getType("22", -1).getMathType());
Assert.fail();
} catch (IllegalArgumentException e) {
}
try {
Assert.assertEquals(MathType.text, MathType.getType("22", 2).getMathType());
Assert.fail();
} catch (IllegalArgumentException e) {
}
}
}

View File

@ -3,12 +3,13 @@
* For more information, please, contact se.solovyev@gmail.com
*/
package org.solovyev.android.calculator;
package org.solovyev.android.calculator.model;
import bsh.EvalError;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.solovyev.android.calculator.JsclOperation;
/**
* User: serso
@ -19,12 +20,12 @@ public class CalculatorModelTest {
@BeforeClass
public static void setUp() throws Exception {
CalculatorModel.init(null);
CalculatorModel.instance.init(null);
}
@Test
public void testEvaluate() throws Exception {
final CalculatorModel cm = CalculatorModel.getInstance();
final CalculatorModel cm = CalculatorModel.instance;
Assert.assertEquals("4.0", cm.evaluate(JsclOperation.numeric, "2+2"));
Assert.assertEquals("-0.7568", cm.evaluate(JsclOperation.numeric, "sin(4)"));
@ -47,7 +48,7 @@ public class CalculatorModelTest {
Assert.assertEquals("-3.41007+3.41007i", cm.evaluate(JsclOperation.numeric, "(5tan(2i)+2i)/(1-i)"));
Assert.assertEquals("-0.1-0.2i", cm.evaluate(JsclOperation.numeric, "(1-i)/(2+6i)"));
CalculatorModel.getInstance().getVarsRegister().addVar(null, new Var.Builder("si", 5d));
CalculatorModel.instance.getVarsRegister().addVar(null, new Var.Builder("si", 5d));
Assert.assertEquals("5.0", cm.evaluate(JsclOperation.numeric, "si"));
try {
cm.evaluate(JsclOperation.numeric, "sin");
@ -60,17 +61,17 @@ public class CalculatorModelTest {
Assert.assertEquals("-23.97311", cm.evaluate(JsclOperation.numeric, "si*sin(5)si"));
Assert.assertEquals("-3.30879", cm.evaluate(JsclOperation.numeric, "sisin(5si)si"));
CalculatorModel.getInstance().getVarsRegister().addVar(null, new Var.Builder("s", 1d));
CalculatorModel.instance.getVarsRegister().addVar(null, new Var.Builder("s", 1d));
Assert.assertEquals("5.0", cm.evaluate(JsclOperation.numeric, "si"));
CalculatorModel.getInstance().getVarsRegister().addVar(null, new Var.Builder("k", 3.5d));
CalculatorModel.getInstance().getVarsRegister().addVar(null, new Var.Builder("k1", 4d));
CalculatorModel.instance.getVarsRegister().addVar(null, new Var.Builder("k", 3.5d));
CalculatorModel.instance.getVarsRegister().addVar(null, new Var.Builder("k1", 4d));
Assert.assertEquals("4.0", cm.evaluate(JsclOperation.numeric, "k11"));
}
@Test
public void testComplexNumbers() throws Exception {
final CalculatorModel cm = CalculatorModel.getInstance();
final FromJsclTextProcessor cm = new FromJsclTextProcessor();
Assert.assertEquals("1.22133+23123.0i", cm.createResultForComplexNumber("1.22133232+23123*i"));
Assert.assertEquals("1.22133+1.2i", cm.createResultForComplexNumber("1.22133232+1.2*i"));

View File

@ -4,7 +4,7 @@
* or visit http://se.solovyev.org
*/
package org.solovyev.android.calculator;
package org.solovyev.android.calculator.model;
import org.junit.Assert;
import org.junit.Test;
@ -18,7 +18,7 @@ public class ToJsclPreprocessorTest {
@Test
public void testProcess() throws Exception {
final ToJsclPreprocessor preprocessor = new ToJsclPreprocessor();
final ToJsclTextProcessor preprocessor = new ToJsclTextProcessor();
Assert.assertEquals( "sin(4)*cos(5)", preprocessor.process("sin(4)cos(5)"));
Assert.assertEquals( "pi*sin(4)*pi*cos(sqrt(5))", preprocessor.process("πsin(4)πcos(√(5))"));
@ -30,7 +30,7 @@ public class ToJsclPreprocessorTest {
@Test
public void testPostfixFunctionsProcessing() throws Exception {
final ToJsclPreprocessor preprocessor = new ToJsclPreprocessor();
final ToJsclTextProcessor preprocessor = new ToJsclTextProcessor();
Assert.assertEquals(-1, preprocessor.getPostfixFunctionStart("5!", 0));
Assert.assertEquals(0, preprocessor.getPostfixFunctionStart("!", 0));