symbolic calculations added
This commit is contained in:
parent
a6caf0be93
commit
5abb206529
@ -25,9 +25,9 @@
|
|||||||
calc:directionTextScale="0.5"
|
calc:directionTextScale="0.5"
|
||||||
style="?controlButtonStyle"
|
style="?controlButtonStyle"
|
||||||
a:onClick="moveLeftButtonClickHandler"
|
a:onClick="moveLeftButtonClickHandler"
|
||||||
a:layout_width="fill_parent"
|
a:layout_width="0dp"
|
||||||
a:layout_height="fill_parent"
|
a:layout_height="fill_parent"
|
||||||
a:layout_weight="1.5"/>
|
a:layout_weight="1"/>
|
||||||
|
|
||||||
|
|
||||||
<org.solovyev.android.view.widgets.ColorButton xmlns:a="http://schemas.android.com/apk/res/android"
|
<org.solovyev.android.view.widgets.ColorButton xmlns:a="http://schemas.android.com/apk/res/android"
|
||||||
@ -36,15 +36,15 @@
|
|||||||
a:drawableTop="@drawable/sym_keyboard_delete"
|
a:drawableTop="@drawable/sym_keyboard_delete"
|
||||||
style="?controlImageButtonStyle"
|
style="?controlImageButtonStyle"
|
||||||
a:onClick="eraseButtonClickHandler"
|
a:onClick="eraseButtonClickHandler"
|
||||||
a:layout_width="fill_parent"
|
a:layout_width="0dp"
|
||||||
a:layout_height="fill_parent"
|
a:layout_height="fill_parent"
|
||||||
a:layout_weight="1.5"/>
|
a:layout_weight="1"/>
|
||||||
|
|
||||||
|
|
||||||
<include layout="@layout/calc_display"
|
<include layout="@layout/calc_display"
|
||||||
a:layout_width="fill_parent"
|
a:layout_width="0dp"
|
||||||
a:layout_height="fill_parent"
|
a:layout_height="fill_parent"
|
||||||
a:layout_weight="1"/>
|
a:layout_weight="4"/>
|
||||||
|
|
||||||
<org.solovyev.android.view.widgets.ColorButton xmlns:a="http://schemas.android.com/apk/res/android"
|
<org.solovyev.android.view.widgets.ColorButton xmlns:a="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
|
xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
|
||||||
@ -53,9 +53,9 @@
|
|||||||
a:textStyle="bold"
|
a:textStyle="bold"
|
||||||
style="?controlImageButtonStyle"
|
style="?controlImageButtonStyle"
|
||||||
a:onClick="clearButtonClickHandler"
|
a:onClick="clearButtonClickHandler"
|
||||||
a:layout_width="fill_parent"
|
a:layout_width="0dp"
|
||||||
a:layout_height="fill_parent"
|
a:layout_height="fill_parent"
|
||||||
a:layout_weight="1.5"/>
|
a:layout_weight="1"/>
|
||||||
|
|
||||||
<org.solovyev.android.view.widgets.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android"
|
<org.solovyev.android.view.widgets.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
|
xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
|
||||||
@ -65,9 +65,9 @@
|
|||||||
calc:directionTextScale="0.5"
|
calc:directionTextScale="0.5"
|
||||||
style="?controlButtonStyle"
|
style="?controlButtonStyle"
|
||||||
a:onClick="moveRightButtonClickHandler"
|
a:onClick="moveRightButtonClickHandler"
|
||||||
a:layout_width="fill_parent"
|
a:layout_width="0dp"
|
||||||
a:layout_height="fill_parent"
|
a:layout_height="fill_parent"
|
||||||
a:layout_weight="1.5"/>
|
a:layout_weight="1"/>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
@ -80,6 +80,7 @@
|
|||||||
<include layout="@layout/calc_three_digit_button"/>
|
<include layout="@layout/calc_three_digit_button"/>
|
||||||
<include layout="@layout/calc_multiplication_button"/>
|
<include layout="@layout/calc_multiplication_button"/>
|
||||||
<include layout="@layout/calc_division_button"/>
|
<include layout="@layout/calc_division_button"/>
|
||||||
|
<include layout="@layout/calc_equals_button"/>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
@ -92,19 +93,20 @@
|
|||||||
<include layout="@layout/calc_six_digit_button"/>
|
<include layout="@layout/calc_six_digit_button"/>
|
||||||
<include layout="@layout/calc_plus_button"/>
|
<include layout="@layout/calc_plus_button"/>
|
||||||
<include layout="@layout/calc_subtraction_button"/>
|
<include layout="@layout/calc_subtraction_button"/>
|
||||||
|
<include layout="@layout/calc_empty_button"/>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
<LinearLayout a:layout_weight="1" a:layout_width="fill_parent" a:layout_height="0dp">
|
<LinearLayout a:layout_weight="1" a:layout_width="fill_parent" a:layout_height="0dp">
|
||||||
|
|
||||||
<include layout="@layout/calc_donate_button"/>
|
<include layout="@layout/calc_donate_button"/>
|
||||||
<include layout="@layout/calc_square_brackets_button"/>
|
<include layout="@layout/calc_empty_button"/>
|
||||||
<include layout="@layout/calc_seven_digit_button"/>
|
<include layout="@layout/calc_seven_digit_button"/>
|
||||||
<include layout="@layout/calc_eight_digit_button"/>
|
<include layout="@layout/calc_eight_digit_button"/>
|
||||||
<include layout="@layout/calc_nine_digit_button"/>
|
<include layout="@layout/calc_nine_digit_button"/>
|
||||||
<include layout="@layout/calc_zero_digit_button"/>
|
<include layout="@layout/calc_zero_digit_button"/>
|
||||||
<include layout="@layout/calc_round_brackets_button"/>
|
<include layout="@layout/calc_round_brackets_button"/>
|
||||||
|
<include layout="@layout/calc_square_brackets_button"/>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
|
@ -16,7 +16,13 @@
|
|||||||
|
|
||||||
<LinearLayout a:layout_weight="1" a:layout_width="fill_parent" a:layout_height="0dp">
|
<LinearLayout a:layout_weight="1" a:layout_width="fill_parent" a:layout_height="0dp">
|
||||||
|
|
||||||
|
<include layout="@layout/calc_equals_button"
|
||||||
|
a:layout_weight="4"
|
||||||
|
a:layout_width="fill_parent"
|
||||||
|
a:layout_height="fill_parent"/>
|
||||||
|
|
||||||
<include layout="@layout/calc_display"
|
<include layout="@layout/calc_display"
|
||||||
|
a:layout_weight="1"
|
||||||
a:layout_width="fill_parent"
|
a:layout_width="fill_parent"
|
||||||
a:layout_height="fill_parent"/>
|
a:layout_height="fill_parent"/>
|
||||||
|
|
||||||
|
@ -10,7 +10,6 @@
|
|||||||
xmlns:a="http://schemas.android.com/apk/res/android"
|
xmlns:a="http://schemas.android.com/apk/res/android"
|
||||||
a:id="@+id/calculatorDisplay"
|
a:id="@+id/calculatorDisplay"
|
||||||
style="@style/display_style"
|
style="@style/display_style"
|
||||||
a:layout_weight="1"
|
|
||||||
a:minLines="1"
|
a:minLines="1"
|
||||||
a:maxLines="1"
|
a:maxLines="1"
|
||||||
a:textStyle="bold"
|
a:textStyle="bold"
|
||||||
|
10
res/layout/calc_empty_button.xml
Normal file
10
res/layout/calc_empty_button.xml
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
~ 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
|
||||||
|
-->
|
||||||
|
<org.solovyev.android.view.widgets.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
|
||||||
|
style="?controlButtonStyle"/>
|
@ -7,8 +7,11 @@
|
|||||||
-->
|
-->
|
||||||
<org.solovyev.android.view.widgets.DirectionDragButton
|
<org.solovyev.android.view.widgets.DirectionDragButton
|
||||||
xmlns:a="http://schemas.android.com/apk/res/android"
|
xmlns:a="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
|
||||||
a:id="@+id/equalsButton"
|
a:id="@+id/equalsButton"
|
||||||
|
calc:textUp="≡"
|
||||||
a:text="="
|
a:text="="
|
||||||
|
calc:directionTextScale="0.5"
|
||||||
a:layout_width="fill_parent"
|
a:layout_width="fill_parent"
|
||||||
a:layout_height="fill_parent"
|
a:layout_height="fill_parent"
|
||||||
style="?controlButtonStyle"
|
style="?controlButtonStyle"
|
||||||
|
@ -92,6 +92,13 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
|
|||||||
((DragButton) findViewById(R.id.leftButton)).setOnDragListener(toPositionOnDragListener);
|
((DragButton) findViewById(R.id.leftButton)).setOnDragListener(toPositionOnDragListener);
|
||||||
dpclRegister.addListener(toPositionOnDragListener);
|
dpclRegister.addListener(toPositionOnDragListener);
|
||||||
|
|
||||||
|
final DragButton equalsButton = (DragButton) findViewById(R.id.equalsButton);
|
||||||
|
if (equalsButton != null) {
|
||||||
|
final SimpleOnDragListener evalOnDragListener = new SimpleOnDragListener(new EvalDragProcessor(calculatorModel), dragPreferences);
|
||||||
|
equalsButton.setOnDragListener(evalOnDragListener);
|
||||||
|
dpclRegister.addListener(evalOnDragListener);
|
||||||
|
}
|
||||||
|
|
||||||
CalculatorEngine.instance.reset(this, preferences);
|
CalculatorEngine.instance.reset(this, preferences);
|
||||||
|
|
||||||
preferences.registerOnSharedPreferenceChangeListener(this);
|
preferences.registerOnSharedPreferenceChangeListener(this);
|
||||||
@ -389,10 +396,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
|
|||||||
}
|
}
|
||||||
|
|
||||||
calculatorModel = CalculatorModel.instance.init(this, preferences, CalculatorEngine.instance);
|
calculatorModel = CalculatorModel.instance.init(this, preferences, CalculatorEngine.instance);
|
||||||
|
|
||||||
AndroidMessageRegistry.instance.init(this);
|
AndroidMessageRegistry.instance.init(this);
|
||||||
|
|
||||||
this.calculatorModel.evaluate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -12,6 +12,7 @@ import android.util.AttributeSet;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||||
import org.solovyev.android.calculator.model.ParseException;
|
import org.solovyev.android.calculator.model.ParseException;
|
||||||
import org.solovyev.android.calculator.model.TextProcessor;
|
import org.solovyev.android.calculator.model.TextProcessor;
|
||||||
|
|
||||||
@ -24,6 +25,9 @@ public class CalculatorDisplay extends TextView {
|
|||||||
|
|
||||||
private boolean valid = true;
|
private boolean valid = true;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private JsclOperation jsclOperation = JsclOperation.numeric;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final static TextProcessor<TextHighlighter.Result> textHighlighter = new TextHighlighter(Color.WHITE, true);
|
private final static TextProcessor<TextHighlighter.Result> textHighlighter = new TextHighlighter(Color.WHITE, true);
|
||||||
|
|
||||||
@ -47,6 +51,15 @@ public class CalculatorDisplay extends TextView {
|
|||||||
this.valid = valid;
|
this.valid = valid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setJsclOperation(@NotNull JsclOperation jsclOperation) {
|
||||||
|
this.jsclOperation = jsclOperation;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public JsclOperation getJsclOperation() {
|
||||||
|
return jsclOperation;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setText(CharSequence text, BufferType type) {
|
public void setText(CharSequence text, BufferType type) {
|
||||||
super.setText(text, type);
|
super.setText(text, type);
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
package org.solovyev.android.calculator;
|
package org.solovyev.android.calculator;
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* User: serso
|
* User: serso
|
||||||
@ -18,45 +18,49 @@ public class CalculatorDisplayHistoryState {
|
|||||||
private boolean valid = true;
|
private boolean valid = true;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final EditorHistoryState editorHistoryState;
|
private EditorHistoryState editorHistoryState;
|
||||||
|
|
||||||
public CalculatorDisplayHistoryState() {
|
@NotNull
|
||||||
this.editorHistoryState = new EditorHistoryState();
|
private JsclOperation jsclOperation;
|
||||||
|
|
||||||
|
private CalculatorDisplayHistoryState() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public CalculatorDisplayHistoryState(boolean valid) {
|
@NotNull
|
||||||
this.editorHistoryState = new EditorHistoryState();
|
public static CalculatorDisplayHistoryState newInstance(@NotNull CalculatorDisplay display) {
|
||||||
this.valid = valid;
|
final CalculatorDisplayHistoryState result = new CalculatorDisplayHistoryState();
|
||||||
}
|
|
||||||
|
|
||||||
public CalculatorDisplayHistoryState(int cursorPosition, @Nullable String text, boolean valid) {
|
result.editorHistoryState = EditorHistoryState.newInstance(display);
|
||||||
this.editorHistoryState = new EditorHistoryState(cursorPosition, text);
|
result.valid = display.isValid();
|
||||||
this.valid = valid;
|
result.jsclOperation = display.getJsclOperation();
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isValid() {
|
public boolean isValid() {
|
||||||
return valid;
|
return valid;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setValid(boolean valid) {
|
|
||||||
this.valid = valid;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public EditorHistoryState getEditorHistoryState() {
|
public EditorHistoryState getEditorHistoryState() {
|
||||||
return editorHistoryState;
|
return editorHistoryState;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public JsclOperation getJsclOperation() {
|
||||||
|
return jsclOperation;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) return true;
|
||||||
if (!(o instanceof CalculatorDisplayHistoryState)) return false;
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
|
||||||
CalculatorDisplayHistoryState that = (CalculatorDisplayHistoryState) o;
|
CalculatorDisplayHistoryState that = (CalculatorDisplayHistoryState) o;
|
||||||
|
|
||||||
if (valid != that.valid) return false;
|
if (valid != that.valid) return false;
|
||||||
if (editorHistoryState != null ? !editorHistoryState.equals(that.editorHistoryState) : that.editorHistoryState != null)
|
if (!editorHistoryState.equals(that.editorHistoryState)) return false;
|
||||||
return false;
|
if (jsclOperation != that.jsclOperation) return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -64,7 +68,8 @@ public class CalculatorDisplayHistoryState {
|
|||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
int result = (valid ? 1 : 0);
|
int result = (valid ? 1 : 0);
|
||||||
result = 31 * result + (editorHistoryState != null ? editorHistoryState.hashCode() : 0);
|
result = 31 * result + editorHistoryState.hashCode();
|
||||||
|
result = 31 * result + jsclOperation.hashCode();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,6 +77,7 @@ public class CalculatorDisplayHistoryState {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return "CalculatorDisplayHistoryState{" +
|
return "CalculatorDisplayHistoryState{" +
|
||||||
"valid=" + valid +
|
"valid=" + valid +
|
||||||
|
"jsclOperation=" + jsclOperation +
|
||||||
", editorHistoryState=" + editorHistoryState +
|
", editorHistoryState=" + editorHistoryState +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,19 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User: serso
|
||||||
|
* Date: 10/24/11
|
||||||
|
* Time: 9:55 PM
|
||||||
|
*/
|
||||||
|
public interface CalculatorEngineControl {
|
||||||
|
|
||||||
|
void evaluate();
|
||||||
|
|
||||||
|
void simplify();
|
||||||
|
}
|
@ -12,6 +12,7 @@ import android.os.Bundle;
|
|||||||
import android.view.*;
|
import android.view.*;
|
||||||
import android.widget.*;
|
import android.widget.*;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||||
import org.solovyev.common.utils.Filter;
|
import org.solovyev.common.utils.Filter;
|
||||||
import org.solovyev.common.utils.FilterRule;
|
import org.solovyev.common.utils.FilterRule;
|
||||||
import org.solovyev.common.utils.FilterRulesChain;
|
import org.solovyev.common.utils.FilterRulesChain;
|
||||||
@ -53,14 +54,16 @@ public class CalculatorHistoryActivity extends ListActivity {
|
|||||||
final int position,
|
final int position,
|
||||||
final long id) {
|
final long id) {
|
||||||
|
|
||||||
|
final CalculatorHistoryState historyState = (CalculatorHistoryState) parent.getItemAtPosition(position);
|
||||||
|
|
||||||
CalculatorModel.instance.doTextOperation(new CalculatorModel.TextOperation() {
|
CalculatorModel.instance.doTextOperation(new CalculatorModel.TextOperation() {
|
||||||
@Override
|
@Override
|
||||||
public void doOperation(@NotNull EditText editor) {
|
public void doOperation(@NotNull EditText editor) {
|
||||||
final EditorHistoryState editorState = ((CalculatorHistoryState) parent.getItemAtPosition(position)).getEditorState();
|
final EditorHistoryState editorState = historyState.getEditorState();
|
||||||
editor.setText(editorState.getText());
|
editor.setText(editorState.getText());
|
||||||
editor.setSelection(editorState.getCursorPosition());
|
editor.setSelection(editorState.getCursorPosition());
|
||||||
}
|
}
|
||||||
}, false);
|
}, false, historyState.getDisplayState().getJsclOperation(), true);
|
||||||
|
|
||||||
CalculatorModel.instance.setCursorOnEnd();
|
CalculatorModel.instance.setCursorOnEnd();
|
||||||
|
|
||||||
@ -108,10 +111,15 @@ public class CalculatorHistoryActivity extends ListActivity {
|
|||||||
time.setText(new SimpleDateFormat().format(state.getTime()));
|
time.setText(new SimpleDateFormat().format(state.getTime()));
|
||||||
|
|
||||||
final TextView editor = (TextView) result.findViewById(R.id.history_item);
|
final TextView editor = (TextView) result.findViewById(R.id.history_item);
|
||||||
editor.setText(state.getEditorState().getText() + "=" + state.getDisplayState().getEditorHistoryState().getText());
|
editor.setText(state.getEditorState().getText() + getIdentitySign(state.getDisplayState().getJsclOperation()) + state.getDisplayState().getEditorHistoryState().getText());
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private String getIdentitySign(@NotNull JsclOperation jsclOperation) {
|
||||||
|
return jsclOperation == JsclOperation.simplify ? "≡" : "=";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -34,7 +34,7 @@ import org.solovyev.common.utils.history.HistoryAction;
|
|||||||
* Date: 9/12/11
|
* Date: 9/12/11
|
||||||
* Time: 11:15 PM
|
* Time: 11:15 PM
|
||||||
*/
|
*/
|
||||||
public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorHistoryState> {
|
public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorHistoryState>, CalculatorEngineControl {
|
||||||
|
|
||||||
instance;
|
instance;
|
||||||
|
|
||||||
@ -51,6 +51,7 @@ public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorH
|
|||||||
private CalculatorEngine calculatorEngine;
|
private CalculatorEngine calculatorEngine;
|
||||||
|
|
||||||
public CalculatorModel init(@NotNull final Activity activity, @NotNull SharedPreferences preferences, @NotNull CalculatorEngine calculator) {
|
public CalculatorModel init(@NotNull final Activity activity, @NotNull SharedPreferences preferences, @NotNull CalculatorEngine calculator) {
|
||||||
|
Log.d(this.getClass().getName(), "CalculatorModel initialization with activity: " + activity);
|
||||||
this.calculatorEngine = calculator;
|
this.calculatorEngine = calculator;
|
||||||
|
|
||||||
this.editor = (CalculatorEditor) activity.findViewById(R.id.calculatorEditor);
|
this.editor = (CalculatorEditor) activity.findViewById(R.id.calculatorEditor);
|
||||||
@ -119,24 +120,29 @@ public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorH
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void doTextOperation(@NotNull TextOperation operation, boolean delayEvaluate) {
|
public void doTextOperation(@NotNull TextOperation operation, boolean delayEvaluate) {
|
||||||
|
doTextOperation(operation, delayEvaluate, JsclOperation.numeric, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void doTextOperation(@NotNull TextOperation operation, boolean delayEvaluate, @NotNull JsclOperation jsclOperation, boolean forceEval) {
|
||||||
final String editorStateBefore = this.editor.getText().toString();
|
final String editorStateBefore = this.editor.getText().toString();
|
||||||
|
|
||||||
|
Log.d(CalculatorModel.class.getName(), "Editor state changed before '" + editorStateBefore + "'");
|
||||||
operation.doOperation(this.editor);
|
operation.doOperation(this.editor);
|
||||||
//Log.d(CalculatorModel.class.getName(), "Doing text operation" + StringUtils.fromStackTrace(Thread.currentThread().getStackTrace()));
|
//Log.d(CalculatorModel.class.getName(), "Doing text operation" + StringUtils.fromStackTrace(Thread.currentThread().getStackTrace()));
|
||||||
|
|
||||||
final String editorStateAfter = this.editor.getText().toString();
|
final String editorStateAfter = this.editor.getText().toString();
|
||||||
if (!editorStateBefore.equals(editorStateAfter)) {
|
if (forceEval ||!editorStateBefore.equals(editorStateAfter)) {
|
||||||
|
|
||||||
editor.redraw();
|
editor.redraw();
|
||||||
|
|
||||||
evaluate(delayEvaluate, editorStateAfter);
|
evaluate(delayEvaluate, editorStateAfter, jsclOperation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final static MutableObject<Runnable> pendingOperation = new MutableObject<Runnable>();
|
private final static MutableObject<Runnable> pendingOperation = new MutableObject<Runnable>();
|
||||||
|
|
||||||
private void evaluate(boolean delayEvaluate, @NotNull final String expression) {
|
private void evaluate(boolean delayEvaluate, @NotNull final String expression, @NotNull final JsclOperation operation) {
|
||||||
final CalculatorHistoryState historyState = getCurrentHistoryState();
|
final CalculatorHistoryState historyState = getCurrentHistoryState();
|
||||||
|
|
||||||
pendingOperation.setObject(new Runnable() {
|
pendingOperation.setObject(new Runnable() {
|
||||||
@ -147,7 +153,7 @@ public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorH
|
|||||||
//lock all operations with history
|
//lock all operations with history
|
||||||
if (pendingOperation.getObject() == this) {
|
if (pendingOperation.getObject() == this) {
|
||||||
// actually nothing shall be logged while text operations are done
|
// actually nothing shall be logged while text operations are done
|
||||||
evaluate(expression);
|
evaluate(expression, operation);
|
||||||
|
|
||||||
historyState.setDisplayState(getCurrentHistoryState().getDisplayState());
|
historyState.setDisplayState(getCurrentHistoryState().getDisplayState());
|
||||||
|
|
||||||
@ -166,19 +172,27 @@ public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorH
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void evaluate() {
|
public void evaluate() {
|
||||||
evaluate(false, this.editor.getText().toString());
|
evaluate(false, this.editor.getText().toString(), JsclOperation.numeric);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void evaluate(@Nullable final String expression) {
|
@Override
|
||||||
|
public void simplify() {
|
||||||
|
evaluate(false, this.editor.getText().toString(), JsclOperation.simplify);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void evaluate(@Nullable final String expression, @NotNull JsclOperation operation) {
|
||||||
if (!StringUtils.isEmpty(expression)) {
|
if (!StringUtils.isEmpty(expression)) {
|
||||||
try {
|
try {
|
||||||
Log.d(CalculatorModel.class.getName(), "Trying to evaluate: " + expression /*+ StringUtils.fromStackTrace(Thread.currentThread().getStackTrace())*/);
|
Log.d(CalculatorModel.class.getName(), "Trying to evaluate '" + operation + "': " + expression /*+ StringUtils.fromStackTrace(Thread.currentThread().getStackTrace())*/);
|
||||||
display.setText(calculatorEngine.evaluate(JsclOperation.numeric, expression));
|
final CalculatorEngine.Result result = calculatorEngine.evaluate(operation, expression);
|
||||||
|
display.setText(result.getResult());
|
||||||
|
display.setJsclOperation(result.getUserOperation());
|
||||||
} catch (EvalError e) {
|
} catch (EvalError e) {
|
||||||
handleEvaluationException(expression, display, e);
|
handleEvaluationException(expression, display, operation, e);
|
||||||
} catch (ParseException e) {
|
} catch (ParseException e) {
|
||||||
handleEvaluationException(expression, display, e);
|
handleEvaluationException(expression, display, operation, e);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this.display.setText("");
|
this.display.setText("");
|
||||||
@ -187,9 +201,13 @@ public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorH
|
|||||||
this.display.redraw();
|
this.display.redraw();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleEvaluationException(@NotNull String expression, @NotNull CalculatorDisplay localDisplay, @NotNull Exception e) {
|
private void handleEvaluationException(@NotNull String expression,
|
||||||
|
@NotNull CalculatorDisplay localDisplay,
|
||||||
|
@NotNull JsclOperation operation,
|
||||||
|
@NotNull Exception e) {
|
||||||
Log.d(CalculatorModel.class.getName(), "Evaluation failed for : " + expression + ". Error message: " + e.getMessage());
|
Log.d(CalculatorModel.class.getName(), "Evaluation failed for : " + expression + ". Error message: " + e.getMessage());
|
||||||
localDisplay.setText(R.string.c_syntax_error);
|
localDisplay.setText(R.string.c_syntax_error);
|
||||||
|
localDisplay.setJsclOperation(operation);
|
||||||
localDisplay.setValid(false);
|
localDisplay.setValid(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -268,6 +286,7 @@ public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorH
|
|||||||
private void setValuesFromHistory(@NotNull CalculatorDisplay display, CalculatorDisplayHistoryState editorHistoryState) {
|
private void setValuesFromHistory(@NotNull CalculatorDisplay display, CalculatorDisplayHistoryState editorHistoryState) {
|
||||||
setValuesFromHistory(display, editorHistoryState.getEditorHistoryState());
|
setValuesFromHistory(display, editorHistoryState.getEditorHistoryState());
|
||||||
display.setValid(editorHistoryState.isValid());
|
display.setValid(editorHistoryState.isValid());
|
||||||
|
display.setJsclOperation(editorHistoryState.getJsclOperation());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setValuesFromHistory(@NotNull TextView editText, EditorHistoryState editorHistoryState) {
|
private void setValuesFromHistory(@NotNull TextView editText, EditorHistoryState editorHistoryState) {
|
||||||
@ -286,22 +305,11 @@ public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorH
|
|||||||
}
|
}
|
||||||
|
|
||||||
private EditorHistoryState getEditorHistoryState(@NotNull TextView textView) {
|
private EditorHistoryState getEditorHistoryState(@NotNull TextView textView) {
|
||||||
final EditorHistoryState result = new EditorHistoryState();
|
return EditorHistoryState.newInstance(textView);
|
||||||
|
|
||||||
result.setText(String.valueOf(textView.getText()));
|
|
||||||
result.setCursorPosition(textView.getSelectionStart());
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private CalculatorDisplayHistoryState getCalculatorDisplayHistoryState(@NotNull CalculatorDisplay display) {
|
private CalculatorDisplayHistoryState getCalculatorDisplayHistoryState(@NotNull CalculatorDisplay display) {
|
||||||
final CalculatorDisplayHistoryState result = new CalculatorDisplayHistoryState();
|
return CalculatorDisplayHistoryState.newInstance(display);
|
||||||
|
|
||||||
result.getEditorHistoryState().setText(String.valueOf(display.getText()));
|
|
||||||
result.getEditorHistoryState().setCursorPosition(display.getSelectionStart());
|
|
||||||
result.setValid(display.isValid());
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
@ -36,8 +36,10 @@ public class CursorDragProcessor implements SimpleOnDragListener.DragProcessor{
|
|||||||
String text = ((DirectionDragButton) dragButton).getText(dragDirection);
|
String text = ((DirectionDragButton) dragButton).getText(dragDirection);
|
||||||
if ("◀◀".equals(text)) {
|
if ("◀◀".equals(text)) {
|
||||||
cursorControl.setCursorOnStart();
|
cursorControl.setCursorOnStart();
|
||||||
|
result = true;
|
||||||
} else if ("▶▶".equals(text)) {
|
} else if ("▶▶".equals(text)) {
|
||||||
cursorControl.setCursorOnEnd();
|
cursorControl.setCursorOnEnd();
|
||||||
|
result = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,8 @@
|
|||||||
|
|
||||||
package org.solovyev.android.calculator;
|
package org.solovyev.android.calculator;
|
||||||
|
|
||||||
|
import android.widget.TextView;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
public class EditorHistoryState {
|
public class EditorHistoryState {
|
||||||
@ -14,16 +16,18 @@ public class EditorHistoryState {
|
|||||||
@Nullable
|
@Nullable
|
||||||
private String text;
|
private String text;
|
||||||
|
|
||||||
public EditorHistoryState() {
|
private EditorHistoryState() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public EditorHistoryState( int cursorPosition, @Nullable String text ) {
|
|
||||||
this.cursorPosition = cursorPosition;
|
|
||||||
this.text = text;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setText(@Nullable String text) {
|
@NotNull
|
||||||
this.text = text;
|
public static EditorHistoryState newInstance(@NotNull TextView textView) {
|
||||||
|
final EditorHistoryState result = new EditorHistoryState();
|
||||||
|
|
||||||
|
result.text = String.valueOf(textView.getText());
|
||||||
|
result.cursorPosition = textView.getSelectionStart();
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@ -31,10 +35,6 @@ public class EditorHistoryState {
|
|||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCursorPosition(int cursorPosition) {
|
|
||||||
this.cursorPosition = cursorPosition;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getCursorPosition() {
|
public int getCursorPosition() {
|
||||||
return cursorPosition;
|
return cursorPosition;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* 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 android.view.MotionEvent;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.solovyev.android.view.widgets.DirectionDragButton;
|
||||||
|
import org.solovyev.android.view.widgets.DragButton;
|
||||||
|
import org.solovyev.android.view.widgets.DragDirection;
|
||||||
|
import org.solovyev.android.view.widgets.SimpleOnDragListener;
|
||||||
|
import org.solovyev.common.utils.Point2d;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User: serso
|
||||||
|
* Date: 10/24/11
|
||||||
|
* Time: 9:52 PM
|
||||||
|
*/
|
||||||
|
public class EvalDragProcessor implements SimpleOnDragListener.DragProcessor {
|
||||||
|
@NotNull
|
||||||
|
private final CalculatorEngineControl calculatorControl;
|
||||||
|
|
||||||
|
public EvalDragProcessor(@NotNull CalculatorEngineControl calculatorControl) {
|
||||||
|
this.calculatorControl = calculatorControl;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean processDragEvent(@NotNull DragDirection dragDirection, @NotNull DragButton dragButton, @NotNull Point2d startPoint2d, @NotNull MotionEvent motionEvent) {
|
||||||
|
boolean result = false;
|
||||||
|
|
||||||
|
if (dragButton instanceof DirectionDragButton) {
|
||||||
|
String text = ((DirectionDragButton) dragButton).getText(dragDirection);
|
||||||
|
if ("≡".equals(text)) {
|
||||||
|
calculatorControl.simplify();
|
||||||
|
result = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
@ -98,12 +98,35 @@ public enum CalculatorEngine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String evaluate(@NotNull JsclOperation operation,
|
public static class Result {
|
||||||
|
@NotNull
|
||||||
|
private String result;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private JsclOperation userOperation;
|
||||||
|
|
||||||
|
public Result(@NotNull String result, @NotNull JsclOperation userOperation) {
|
||||||
|
this.result = result;
|
||||||
|
this.userOperation = userOperation;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public String getResult() {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public JsclOperation getUserOperation() {
|
||||||
|
return userOperation;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Result evaluate(@NotNull JsclOperation operation,
|
||||||
@NotNull String expression) throws EvalError, ParseException {
|
@NotNull String expression) throws EvalError, ParseException {
|
||||||
return evaluate(operation, expression, null);
|
return evaluate(operation, expression, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String evaluate(@NotNull JsclOperation operation,
|
public Result evaluate(@NotNull JsclOperation operation,
|
||||||
@NotNull String expression,
|
@NotNull String expression,
|
||||||
@Nullable MessageRegistry<AndroidMessage> mr) throws EvalError, ParseException {
|
@Nullable MessageRegistry<AndroidMessage> mr) throws EvalError, ParseException {
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
@ -190,7 +213,7 @@ public enum CalculatorEngine {
|
|||||||
throw new ParseException("Too long calculation for: " + jsclExpression);
|
throw new ParseException("Too long calculation for: " + jsclExpression);
|
||||||
}
|
}
|
||||||
|
|
||||||
return operation.getFromProcessor().process(result);
|
return new Result(operation.getFromProcessor().process(result), operation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,54 +66,54 @@ public class CalculatorEngineTest {
|
|||||||
public void testEvaluate() throws Exception {
|
public void testEvaluate() throws Exception {
|
||||||
final CalculatorEngine cm = CalculatorEngine.instance;
|
final CalculatorEngine cm = CalculatorEngine.instance;
|
||||||
|
|
||||||
Assert.assertEquals("4", cm.evaluate(JsclOperation.numeric, "2+2"));
|
Assert.assertEquals("4", cm.evaluate(JsclOperation.numeric, "2+2").getResult());
|
||||||
Assert.assertEquals("-0.757", cm.evaluate(JsclOperation.numeric, "sin(4)"));
|
Assert.assertEquals("-0.757", cm.evaluate(JsclOperation.numeric, "sin(4)").getResult());
|
||||||
Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "asin(0.5)"));
|
Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "asin(0.5)").getResult());
|
||||||
Assert.assertEquals("-0.396", cm.evaluate(JsclOperation.numeric, "sin(4)asin(0.5)"));
|
Assert.assertEquals("-0.396", cm.evaluate(JsclOperation.numeric, "sin(4)asin(0.5)").getResult());
|
||||||
Assert.assertEquals("-0.56", cm.evaluate(JsclOperation.numeric, "sin(4)asin(0.5)sqrt(2)"));
|
Assert.assertEquals("-0.56", cm.evaluate(JsclOperation.numeric, "sin(4)asin(0.5)sqrt(2)").getResult());
|
||||||
Assert.assertEquals("-0.56", cm.evaluate(JsclOperation.numeric, "sin(4)asin(0.5)√(2)"));
|
Assert.assertEquals("-0.56", cm.evaluate(JsclOperation.numeric, "sin(4)asin(0.5)√(2)").getResult());
|
||||||
Assert.assertEquals("7.389", cm.evaluate(JsclOperation.numeric, "e^2"));
|
Assert.assertEquals("7.389", cm.evaluate(JsclOperation.numeric, "e^2").getResult());
|
||||||
Assert.assertEquals("7.389", cm.evaluate(JsclOperation.numeric, "exp(1)^2"));
|
Assert.assertEquals("7.389", cm.evaluate(JsclOperation.numeric, "exp(1)^2").getResult());
|
||||||
Assert.assertEquals("7.389", cm.evaluate(JsclOperation.numeric, "exp(2)"));
|
Assert.assertEquals("7.389", cm.evaluate(JsclOperation.numeric, "exp(2)").getResult());
|
||||||
Assert.assertEquals("2+i", cm.evaluate(JsclOperation.numeric, "2*1+sqrt(-1)"));
|
Assert.assertEquals("2+i", cm.evaluate(JsclOperation.numeric, "2*1+sqrt(-1)").getResult());
|
||||||
Assert.assertEquals("0.921+3.142i", cm.evaluate(JsclOperation.numeric, "ln(5cosh(38π√(2cos(2))))"));
|
Assert.assertEquals("0.921+3.142i", cm.evaluate(JsclOperation.numeric, "ln(5cosh(38π√(2cos(2))))").getResult());
|
||||||
Assert.assertEquals("7.389i", cm.evaluate(JsclOperation.numeric, "iexp(2)"));
|
Assert.assertEquals("7.389i", cm.evaluate(JsclOperation.numeric, "iexp(2)").getResult());
|
||||||
Assert.assertEquals("2+7.389i", cm.evaluate(JsclOperation.numeric, "2+iexp(2)"));
|
Assert.assertEquals("2+7.389i", cm.evaluate(JsclOperation.numeric, "2+iexp(2)").getResult());
|
||||||
Assert.assertEquals("2+7.389i", cm.evaluate(JsclOperation.numeric, "2+√(-1)exp(2)"));
|
Assert.assertEquals("2+7.389i", cm.evaluate(JsclOperation.numeric, "2+√(-1)exp(2)").getResult());
|
||||||
Assert.assertEquals("2-2.5i", cm.evaluate(JsclOperation.numeric, "2-2.5i"));
|
Assert.assertEquals("2-2.5i", cm.evaluate(JsclOperation.numeric, "2-2.5i").getResult());
|
||||||
Assert.assertEquals("-2-2.5i", cm.evaluate(JsclOperation.numeric, "-2-2.5i"));
|
Assert.assertEquals("-2-2.5i", cm.evaluate(JsclOperation.numeric, "-2-2.5i").getResult());
|
||||||
Assert.assertEquals("-2+2.5i", cm.evaluate(JsclOperation.numeric, "-2+2.5i"));
|
Assert.assertEquals("-2+2.5i", cm.evaluate(JsclOperation.numeric, "-2+2.5i").getResult());
|
||||||
Assert.assertEquals("-2+2.1i", cm.evaluate(JsclOperation.numeric, "-2+2.1i"));
|
Assert.assertEquals("-2+2.1i", cm.evaluate(JsclOperation.numeric, "-2+2.1i").getResult());
|
||||||
Assert.assertEquals("-3.41+3.41i", cm.evaluate(JsclOperation.numeric, "(5tan(2i)+2i)/(1-i)"));
|
Assert.assertEquals("-3.41+3.41i", cm.evaluate(JsclOperation.numeric, "(5tan(2i)+2i)/(1-i)").getResult());
|
||||||
Assert.assertEquals("-0.1-0.2i", cm.evaluate(JsclOperation.numeric, "(1-i)/(2+6i)"));
|
Assert.assertEquals("-0.1-0.2i", cm.evaluate(JsclOperation.numeric, "(1-i)/(2+6i)").getResult());
|
||||||
|
|
||||||
CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("si", 5d));
|
CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("si", 5d));
|
||||||
|
|
||||||
Assert.assertEquals("-0.959", cm.evaluate(JsclOperation.numeric, "sin(5)"));
|
Assert.assertEquals("-0.959", cm.evaluate(JsclOperation.numeric, "sin(5)").getResult());
|
||||||
Assert.assertEquals("-4.795", cm.evaluate(JsclOperation.numeric, "sin(5)si"));
|
Assert.assertEquals("-4.795", cm.evaluate(JsclOperation.numeric, "sin(5)si").getResult());
|
||||||
Assert.assertEquals("-23.973", cm.evaluate(JsclOperation.numeric, "sisin(5)si"));
|
Assert.assertEquals("-23.973", cm.evaluate(JsclOperation.numeric, "sisin(5)si").getResult());
|
||||||
Assert.assertEquals("-23.973", cm.evaluate(JsclOperation.numeric, "si*sin(5)si"));
|
Assert.assertEquals("-23.973", cm.evaluate(JsclOperation.numeric, "si*sin(5)si").getResult());
|
||||||
Assert.assertEquals("-3.309", cm.evaluate(JsclOperation.numeric, "sisin(5si)si"));
|
Assert.assertEquals("-3.309", cm.evaluate(JsclOperation.numeric, "sisin(5si)si").getResult());
|
||||||
|
|
||||||
CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("s", 1d));
|
CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("s", 1d));
|
||||||
Assert.assertEquals("5", cm.evaluate(JsclOperation.numeric, "si"));
|
Assert.assertEquals("5", cm.evaluate(JsclOperation.numeric, "si").getResult());
|
||||||
|
|
||||||
CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("k", 3.5d));
|
CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("k", 3.5d));
|
||||||
CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("k1", 4d));
|
CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("k1", 4d));
|
||||||
Assert.assertEquals("4", cm.evaluate(JsclOperation.numeric, "k11"));
|
Assert.assertEquals("4", cm.evaluate(JsclOperation.numeric, "k11").getResult());
|
||||||
|
|
||||||
CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("t", (String) null));
|
CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("t", (String) null));
|
||||||
Assert.assertEquals("11t", cm.evaluate(JsclOperation.numeric, "t11"));
|
Assert.assertEquals("11t", cm.evaluate(JsclOperation.numeric, "t11").getResult());
|
||||||
Assert.assertEquals("11et", cm.evaluate(JsclOperation.numeric, "t11e"));
|
Assert.assertEquals("11et", cm.evaluate(JsclOperation.numeric, "t11e").getResult());
|
||||||
Assert.assertEquals("11×Infinityt", cm.evaluate(JsclOperation.numeric, "t11∞"));
|
Assert.assertEquals("11×Infinityt", cm.evaluate(JsclOperation.numeric, "t11∞").getResult());
|
||||||
Assert.assertEquals("-t+t^3", cm.evaluate(JsclOperation.numeric, "t(t-1)(t+1)"));
|
Assert.assertEquals("-t+t^3", cm.evaluate(JsclOperation.numeric, "t(t-1)(t+1)").getResult());
|
||||||
|
|
||||||
|
|
||||||
Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "30°"));
|
Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "30°").getResult());
|
||||||
Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "(10+20)°"));
|
Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "(10+20)°").getResult());
|
||||||
Assert.assertEquals("1.047", cm.evaluate(JsclOperation.numeric, "(10+20)°*2"));
|
Assert.assertEquals("1.047", cm.evaluate(JsclOperation.numeric, "(10+20)°*2").getResult());
|
||||||
try {
|
try {
|
||||||
Assert.assertEquals("0.278", cm.evaluate(JsclOperation.numeric, "30°^2"));
|
Assert.assertEquals("0.278", cm.evaluate(JsclOperation.numeric, "30°^2").getResult());
|
||||||
junit.framework.Assert.fail();
|
junit.framework.Assert.fail();
|
||||||
} catch (ParseException e) {
|
} catch (ParseException e) {
|
||||||
if ( !e.getMessage().equals("Power operation after postfix function is currently unsupported!") ) {
|
if ( !e.getMessage().equals("Power operation after postfix function is currently unsupported!") ) {
|
||||||
@ -130,16 +130,16 @@ public class CalculatorEngineTest {
|
|||||||
Assert.fail();
|
Assert.fail();
|
||||||
} catch (ParseException e) {
|
} catch (ParseException e) {
|
||||||
}
|
}
|
||||||
Assert.assertEquals("NaN", cm.evaluate(JsclOperation.numeric, "ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(100)))))))))))))))"));
|
Assert.assertEquals("NaN", cm.evaluate(JsclOperation.numeric, "ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(100)))))))))))))))").getResult());
|
||||||
try {
|
try {
|
||||||
cm.evaluate(JsclOperation.numeric, "cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos())))))))))))))))))))))))))))))))))))");
|
cm.evaluate(JsclOperation.numeric, "cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos())))))))))))))))))))))))))))))))))))");
|
||||||
Assert.fail();
|
Assert.fail();
|
||||||
} catch (ParseException e) {
|
} catch (ParseException e) {
|
||||||
}
|
}
|
||||||
Assert.assertEquals("0.739", cm.evaluate(JsclOperation.numeric, "cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(1))))))))))))))))))))))))))))))))))))"));
|
Assert.assertEquals("0.739", cm.evaluate(JsclOperation.numeric, "cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(1))))))))))))))))))))))))))))))))))))").getResult());
|
||||||
|
|
||||||
CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("si", 5d));
|
CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("si", 5d));
|
||||||
Assert.assertEquals("5", cm.evaluate(JsclOperation.numeric, "si"));
|
Assert.assertEquals("5", cm.evaluate(JsclOperation.numeric, "si").getResult());
|
||||||
try {
|
try {
|
||||||
cm.evaluate(JsclOperation.numeric, "sin");
|
cm.evaluate(JsclOperation.numeric, "sin");
|
||||||
Assert.fail();
|
Assert.fail();
|
||||||
@ -156,11 +156,11 @@ public class CalculatorEngineTest {
|
|||||||
decimalGroupSymbols.setGroupingSeparator('\'');
|
decimalGroupSymbols.setGroupingSeparator('\'');
|
||||||
cm.setDecimalGroupSymbols(decimalGroupSymbols);
|
cm.setDecimalGroupSymbols(decimalGroupSymbols);
|
||||||
cm.setPrecision(2);
|
cm.setPrecision(2);
|
||||||
Assert.assertEquals("12'345'678.9", cm.evaluate(JsclOperation.numeric, "1.23456789E7"));
|
Assert.assertEquals("12'345'678.9", cm.evaluate(JsclOperation.numeric, "1.23456789E7").getResult());
|
||||||
cm.setPrecision(10);
|
cm.setPrecision(10);
|
||||||
Assert.assertEquals("12'345'678.899999999", cm.evaluate(JsclOperation.numeric, "1.23456789E7"));
|
Assert.assertEquals("12'345'678.899999999", cm.evaluate(JsclOperation.numeric, "1.23456789E7").getResult());
|
||||||
Assert.assertEquals("123'456'788.99999999", cm.evaluate(JsclOperation.numeric, "1.234567890E8"));
|
Assert.assertEquals("123'456'788.99999999", cm.evaluate(JsclOperation.numeric, "1.234567890E8").getResult());
|
||||||
Assert.assertEquals("1'234'567'890.1", cm.evaluate(JsclOperation.numeric, "1.2345678901E9"));
|
Assert.assertEquals("1'234'567'890.1", cm.evaluate(JsclOperation.numeric, "1.2345678901E9").getResult());
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user