Ans variable

This commit is contained in:
serso 2012-10-15 12:40:17 +04:00
parent 686ca8f901
commit 2e2735148f
5 changed files with 200 additions and 133 deletions

View File

@ -13,6 +13,7 @@ import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.history.CalculatorHistory;
import org.solovyev.android.calculator.history.CalculatorHistoryState;
import org.solovyev.android.calculator.jscl.JsclOperation;
import org.solovyev.android.calculator.model.Var;
import org.solovyev.android.calculator.text.TextProcessor;
import org.solovyev.android.calculator.units.CalculatorNumeralBase;
import org.solovyev.common.history.HistoryAction;
@ -388,16 +389,20 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
switch (calculatorEventType) {
case editor_state_changed:
final CalculatorEditorChangeEventData changeEventData = (CalculatorEditorChangeEventData) data;
final CalculatorEditorChangeEventData editorChangeEventData = (CalculatorEditorChangeEventData) data;
final String newText = changeEventData.getNewValue().getText();
final String oldText = changeEventData.getOldValue().getText();
final String newText = editorChangeEventData.getNewValue().getText();
final String oldText = editorChangeEventData.getOldValue().getText();
if (!newText.equals(oldText)) {
evaluate(JsclOperation.numeric, changeEventData.getNewValue().getText(), calculatorEventData.getSequenceId());
evaluate(JsclOperation.numeric, editorChangeEventData.getNewValue().getText(), calculatorEventData.getSequenceId());
}
break;
case display_state_changed:
onDisplayStateChanged((CalculatorDisplayChangeEventData) data);
break;
case engine_preferences_changed:
evaluate(calculatorEventData.getSequenceId());
break;
@ -420,6 +425,30 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
}
}
private void onDisplayStateChanged(@NotNull CalculatorDisplayChangeEventData displayChangeEventData) {
final CalculatorDisplayViewState newState = displayChangeEventData.getNewValue();
if (newState.isValid()) {
final String result = newState.getStringResult();
if ( !StringUtils.isEmpty(result) ) {
final CalculatorMathRegistry<IConstant> varsRegistry = CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry();
final IConstant ansVar = varsRegistry.get(CalculatorVarsRegistry.ANS);
final Var.Builder varBuilder;
if (ansVar != null) {
varBuilder = new Var.Builder(ansVar);
} else {
varBuilder = new Var.Builder();
}
varBuilder.setName(CalculatorVarsRegistry.ANS);
varBuilder.setValue(result);
varBuilder.setDescription(CalculatorMessages.getBundle().getString("ans_description"));
varsRegistry.add(varBuilder);
}
}
}
/*
**********************************************************************
*

View File

@ -1,111 +1,114 @@
/*
* 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;
import jscl.math.function.IConstant;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.model.Var;
import org.solovyev.android.calculator.model.Vars;
import org.solovyev.common.JBuilder;
import org.solovyev.common.math.MathRegistry;
import java.util.HashMap;
import java.util.Map;
/**
* User: serso
* Date: 9/29/11
* Time: 4:57 PM
*/
public class CalculatorVarsRegistry extends AbstractCalculatorMathRegistry<IConstant, Var> {
@NotNull
private static final Map<String, String> substitutes = new HashMap<String, String>();
static {
substitutes.put("π", "pi");
substitutes.put("Π", "PI");
substitutes.put("", "inf");
substitutes.put("h", "h_reduced");
substitutes.put("NaN", "nan");
}
public CalculatorVarsRegistry(@NotNull MathRegistry<IConstant> mathRegistry,
@NotNull MathEntityDao<Var> mathEntityDao) {
super(mathRegistry, "c_var_description_", mathEntityDao);
}
@NotNull
@Override
protected Map<String, String> getSubstitutes() {
return substitutes;
}
public synchronized void load() {
super.load();
tryToAddAuxVar("x");
tryToAddAuxVar("y");
tryToAddAuxVar("t");
tryToAddAuxVar("j");
/*Log.d(AndroidVarsRegistry.class.getName(), vars.size() + " variables registered!");
for (Var var : vars) {
Log.d(AndroidVarsRegistry.class.getName(), var.toString());
}*/
}
@NotNull
@Override
protected JBuilder<? extends IConstant> createBuilder(@NotNull Var entity) {
return new Var.Builder(entity);
}
@NotNull
@Override
protected MathEntityPersistenceContainer<Var> createPersistenceContainer() {
return new Vars();
}
private void tryToAddAuxVar(@NotNull String name) {
if ( !contains(name) ) {
add(new Var.Builder(name, (String)null));
}
}
@NotNull
@Override
protected Var transform(@NotNull IConstant entity) {
if (entity instanceof Var) {
return (Var) entity;
} else {
return new Var.Builder(entity).create();
}
}
@Override
public String getDescription(@NotNull String mathEntityName) {
final IConstant var = get(mathEntityName);
if (var != null && !var.isSystem()) {
return var.getDescription();
} else {
return super.getDescription(mathEntityName);
}
}
@Override
public String getCategory(@NotNull IConstant var) {
for (VarCategory category : VarCategory.values()) {
if ( category.isInCategory(var) ) {
return category.name();
}
}
return null;
}
}
/*
* 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;
import jscl.math.function.IConstant;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.model.Var;
import org.solovyev.android.calculator.model.Vars;
import org.solovyev.common.JBuilder;
import org.solovyev.common.math.MathRegistry;
import java.util.HashMap;
import java.util.Map;
/**
* User: serso
* Date: 9/29/11
* Time: 4:57 PM
*/
public class CalculatorVarsRegistry extends AbstractCalculatorMathRegistry<IConstant, Var> {
@NotNull
public static final String ANS = "ans";
@NotNull
private static final Map<String, String> substitutes = new HashMap<String, String>();
static {
substitutes.put("π", "pi");
substitutes.put("Π", "PI");
substitutes.put("", "inf");
substitutes.put("h", "h_reduced");
substitutes.put("NaN", "nan");
}
public CalculatorVarsRegistry(@NotNull MathRegistry<IConstant> mathRegistry,
@NotNull MathEntityDao<Var> mathEntityDao) {
super(mathRegistry, "c_var_description_", mathEntityDao);
}
@NotNull
@Override
protected Map<String, String> getSubstitutes() {
return substitutes;
}
public synchronized void load() {
super.load();
tryToAddAuxVar("x");
tryToAddAuxVar("y");
tryToAddAuxVar("t");
tryToAddAuxVar("j");
/*Log.d(AndroidVarsRegistry.class.getName(), vars.size() + " variables registered!");
for (Var var : vars) {
Log.d(AndroidVarsRegistry.class.getName(), var.toString());
}*/
}
@NotNull
@Override
protected JBuilder<? extends IConstant> createBuilder(@NotNull Var entity) {
return new Var.Builder(entity);
}
@NotNull
@Override
protected MathEntityPersistenceContainer<Var> createPersistenceContainer() {
return new Vars();
}
private void tryToAddAuxVar(@NotNull String name) {
if ( !contains(name) ) {
add(new Var.Builder(name, (String)null));
}
}
@NotNull
@Override
protected Var transform(@NotNull IConstant entity) {
if (entity instanceof Var) {
return (Var) entity;
} else {
return new Var.Builder(entity).create();
}
}
@Override
public String getDescription(@NotNull String mathEntityName) {
final IConstant var = get(mathEntityName);
if (var != null && !var.isSystem()) {
return var.getDescription();
} else {
return super.getDescription(mathEntityName);
}
}
@Override
public String getCategory(@NotNull IConstant var) {
for (VarCategory category : VarCategory.values()) {
if ( category.isInCategory(var) ) {
return category.name();
}
}
return null;
}
}

View File

@ -1,9 +1,10 @@
msg_1=Arithmetic error occurred: {0}
msg_2=Too complex expression
msg_3=Too long execution time - check the expression
msg_4=Evaluation was cancelled
msg_5=No parameters are specified for function: {0}
msg_6=Infinite loop is detected in expression
syntax_error=Error
result_copied=Result copied to clipboard!
msg_1=Arithmetic error occurred: {0}
msg_2=Too complex expression
msg_3=Too long execution time - check the expression
msg_4=Evaluation was cancelled
msg_5=No parameters are specified for function: {0}
msg_6=Infinite loop is detected in expression
syntax_error=Error
result_copied=Result copied to clipboard!
ans_description=Last calculated value

View File

@ -1,9 +1,10 @@
msg_1=Арифметическая ошибка: {0}
msg_2=Слишком сложное выражение
msg_3=Вычисление выражения занимает слишком много времени - проверьте выражение
msg_4=Вычисление было отменено
msg_5=Для функции {0} не определены параметры
msg_6=В выражении найден Бесконечный цикл - проверьте выражение
syntax_error=Ошибка
result_copied=Результат скопирован в буфер!
msg_1=Арифметическая ошибка: {0}
msg_2=Слишком сложное выражение
msg_3=Вычисление выражения занимает слишком много времени - проверьте выражение
msg_4=Вычисление было отменено
msg_5=Для функции {0} не определены параметры
msg_6=В выражении найден Бесконечный цикл - проверьте выражение
syntax_error=Ошибка
result_copied=Результат скопирован в буфер!
ans_description=Последний посчитанный результат

View File

@ -0,0 +1,33 @@
package org.solovyev.android.calculator;
import org.junit.Before;
import org.junit.Test;
/**
* User: Solovyev_S
* Date: 15.10.12
* Time: 12:30
*/
public class CalculatorImplTest extends AbstractCalculatorTest {
@Before
public void setUp() throws Exception {
super.setUp();
}
@Test
public void testAnsVariable() throws Exception {
CalculatorTestUtils.assertError("ans");
CalculatorTestUtils.assertError("ans");
CalculatorTestUtils.assertEval("2", "2");
CalculatorTestUtils.assertEval("2", "ans");
CalculatorTestUtils.assertEval("4", "ans^2");
CalculatorTestUtils.assertEval("16", "ans^2");
CalculatorTestUtils.assertEval("0", "0");
CalculatorTestUtils.assertEval("0", "ans");
CalculatorTestUtils.assertEval("3", "3");
CalculatorTestUtils.assertEval("9", "ans*ans");
CalculatorTestUtils.assertError("ans*an");
CalculatorTestUtils.assertEval("81", "ans*ans");
}
}