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.CalculatorHistory;
import org.solovyev.android.calculator.history.CalculatorHistoryState; import org.solovyev.android.calculator.history.CalculatorHistoryState;
import org.solovyev.android.calculator.jscl.JsclOperation; 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.text.TextProcessor;
import org.solovyev.android.calculator.units.CalculatorNumeralBase; import org.solovyev.android.calculator.units.CalculatorNumeralBase;
import org.solovyev.common.history.HistoryAction; import org.solovyev.common.history.HistoryAction;
@ -388,16 +389,20 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
switch (calculatorEventType) { switch (calculatorEventType) {
case editor_state_changed: case editor_state_changed:
final CalculatorEditorChangeEventData changeEventData = (CalculatorEditorChangeEventData) data; final CalculatorEditorChangeEventData editorChangeEventData = (CalculatorEditorChangeEventData) data;
final String newText = changeEventData.getNewValue().getText(); final String newText = editorChangeEventData.getNewValue().getText();
final String oldText = changeEventData.getOldValue().getText(); final String oldText = editorChangeEventData.getOldValue().getText();
if (!newText.equals(oldText)) { if (!newText.equals(oldText)) {
evaluate(JsclOperation.numeric, changeEventData.getNewValue().getText(), calculatorEventData.getSequenceId()); evaluate(JsclOperation.numeric, editorChangeEventData.getNewValue().getText(), calculatorEventData.getSequenceId());
} }
break; break;
case display_state_changed:
onDisplayStateChanged((CalculatorDisplayChangeEventData) data);
break;
case engine_preferences_changed: case engine_preferences_changed:
evaluate(calculatorEventData.getSequenceId()); evaluate(calculatorEventData.getSequenceId());
break; 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. * Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com * For more information, please, contact se.solovyev@gmail.com
* or visit http://se.solovyev.org * or visit http://se.solovyev.org
*/ */
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import jscl.math.function.IConstant; import jscl.math.function.IConstant;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.model.Var; import org.solovyev.android.calculator.model.Var;
import org.solovyev.android.calculator.model.Vars; import org.solovyev.android.calculator.model.Vars;
import org.solovyev.common.JBuilder; import org.solovyev.common.JBuilder;
import org.solovyev.common.math.MathRegistry; import org.solovyev.common.math.MathRegistry;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
/** /**
* User: serso * User: serso
* Date: 9/29/11 * Date: 9/29/11
* Time: 4:57 PM * Time: 4:57 PM
*/ */
public class CalculatorVarsRegistry extends AbstractCalculatorMathRegistry<IConstant, Var> { public class CalculatorVarsRegistry extends AbstractCalculatorMathRegistry<IConstant, Var> {
@NotNull @NotNull
private static final Map<String, String> substitutes = new HashMap<String, String>(); public static final String ANS = "ans";
static {
substitutes.put("π", "pi"); @NotNull
substitutes.put("Π", "PI"); private static final Map<String, String> substitutes = new HashMap<String, String>();
substitutes.put("", "inf"); static {
substitutes.put("h", "h_reduced"); substitutes.put("π", "pi");
substitutes.put("NaN", "nan"); substitutes.put("Π", "PI");
} substitutes.put("", "inf");
substitutes.put("h", "h_reduced");
public CalculatorVarsRegistry(@NotNull MathRegistry<IConstant> mathRegistry, substitutes.put("NaN", "nan");
@NotNull MathEntityDao<Var> mathEntityDao) { }
super(mathRegistry, "c_var_description_", mathEntityDao);
} public CalculatorVarsRegistry(@NotNull MathRegistry<IConstant> mathRegistry,
@NotNull MathEntityDao<Var> mathEntityDao) {
@NotNull super(mathRegistry, "c_var_description_", mathEntityDao);
@Override }
protected Map<String, String> getSubstitutes() {
return substitutes; @NotNull
} @Override
protected Map<String, String> getSubstitutes() {
public synchronized void load() { return substitutes;
super.load(); }
tryToAddAuxVar("x"); public synchronized void load() {
tryToAddAuxVar("y"); super.load();
tryToAddAuxVar("t");
tryToAddAuxVar("j"); tryToAddAuxVar("x");
tryToAddAuxVar("y");
tryToAddAuxVar("t");
/*Log.d(AndroidVarsRegistry.class.getName(), vars.size() + " variables registered!"); tryToAddAuxVar("j");
for (Var var : vars) {
Log.d(AndroidVarsRegistry.class.getName(), var.toString());
}*/ /*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 JBuilder<? extends IConstant> createBuilder(@NotNull Var entity) {
@NotNull return new Var.Builder(entity);
@Override }
protected MathEntityPersistenceContainer<Var> createPersistenceContainer() {
return new Vars(); @NotNull
} @Override
protected MathEntityPersistenceContainer<Var> createPersistenceContainer() {
private void tryToAddAuxVar(@NotNull String name) { return new Vars();
if ( !contains(name) ) { }
add(new Var.Builder(name, (String)null));
} 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) { @NotNull
return (Var) entity; @Override
} else { protected Var transform(@NotNull IConstant entity) {
return new Var.Builder(entity).create(); 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()) { @Override
return var.getDescription(); public String getDescription(@NotNull String mathEntityName) {
} else { final IConstant var = get(mathEntityName);
return super.getDescription(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) ) { @Override
return category.name(); public String getCategory(@NotNull IConstant var) {
} for (VarCategory category : VarCategory.values()) {
} if ( category.isInCategory(var) ) {
return category.name();
return null; }
} }
}
return null;
}
}

View File

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

View File

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