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

@ -23,6 +23,9 @@ import java.util.Map;
*/
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 {

View File

@ -7,3 +7,4 @@ msg_6=Infinite loop is detected in expression
syntax_error=Error
result_copied=Result copied to clipboard!
ans_description=Last calculated value

View File

@ -7,3 +7,4 @@ 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");
}
}