This commit is contained in:
serso 2011-10-22 19:31:35 +04:00
parent 192e06a7d4
commit f9fa6b3ca5
18 changed files with 80 additions and 39 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.7 KiB

After

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 KiB

After

Width:  |  Height:  |  Size: 807 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.7 KiB

After

Width:  |  Height:  |  Size: 4.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 3.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

@ -12,26 +12,26 @@
</style> </style>
<style name="display_style_parent" parent="default_text"> <style name="display_style_parent" parent="default_text">
<item name="android:textSize">50dp</item> <item name="android:textSize">35dp</item>
<item name="android:padding">10dp</item> <item name="android:padding">5dp</item>
</style> </style>
<style name="editor_style_parent" parent="default_text"> <style name="editor_style_parent" parent="default_text">
<item name="android:textSize">50dp</item> <item name="android:textSize">35dp</item>
<item name="android:padding">10dp</item> <item name="android:padding">5dp</item>
</style> </style>
<style name="about_style_parent" parent="default_text"> <style name="about_style_parent" parent="default_text">
<item name="android:textSize">30dp</item> <item name="android:textSize">15dp</item>
<item name="android:padding">10dp</item> <item name="android:padding">5dp</item>
</style> </style>
<style name="var_text_parent"> <style name="var_text_parent">
<item name="android:textSize">40dp</item> <item name="android:textSize">20dp</item>
</style> </style>
<style name="var_description_parent"> <style name="var_description_parent">
<item name="android:textSize">30dp</item> <item name="android:textSize">15dp</item>
</style> </style>
<style name="add_var_button_parent"> <style name="add_var_button_parent">

View File

@ -12,26 +12,26 @@
</style> </style>
<style name="display_style_parent" parent="default_text"> <style name="display_style_parent" parent="default_text">
<item name="android:textSize">37dp</item> <item name="android:textSize">30dp</item>
<item name="android:padding">7dp</item> <item name="android:padding">5dp</item>
</style> </style>
<style name="editor_style_parent" parent="default_text"> <style name="editor_style_parent" parent="default_text">
<item name="android:textSize">37dp</item> <item name="android:textSize">30dp</item>
<item name="android:padding">7dp</item> <item name="android:padding">5dp</item>
</style> </style>
<style name="about_style_parent" parent="default_text"> <style name="about_style_parent" parent="default_text">
<item name="android:textSize">23dp</item> <item name="android:textSize">15dp</item>
<item name="android:padding">7dp</item> <item name="android:padding">5dp</item>
</style> </style>
<style name="var_text_parent"> <style name="var_text_parent">
<item name="android:textSize">30dp</item> <item name="android:textSize">20dp</item>
</style> </style>
<style name="var_description_parent"> <style name="var_description_parent">
<item name="android:textSize">23dp</item> <item name="android:textSize">15dp</item>
</style> </style>
<style name="add_var_button_parent"> <style name="add_var_button_parent">

View File

@ -13,9 +13,6 @@ import org.solovyev.android.calculator.model.CalculatorEngine;
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;
import java.math.BigDecimal;
import java.text.DecimalFormat;
/** /**
* User: serso * User: serso
* Date: 10/6/11 * Date: 10/6/11
@ -32,7 +29,7 @@ class FromJsclNumericTextProcessor implements TextProcessor<String> {
if (doubleValue.isInfinite()) { if (doubleValue.isInfinite()) {
result = MathType.INFINITY; result = MathType.INFINITY;
} else { } else {
result = format(doubleValue); result = CalculatorEngine.instance.format(doubleValue);
} }
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
result = result.replace(MathType.INFINITY_JSCL, MathType.INFINITY); result = result.replace(MathType.INFINITY_JSCL, MathType.INFINITY);
@ -53,18 +50,7 @@ class FromJsclNumericTextProcessor implements TextProcessor<String> {
} }
private String format(@NotNull String value) { private String format(@NotNull String value) {
return format(Double.valueOf(value)); return CalculatorEngine.instance.format(Double.valueOf(value));
}
private String format(@NotNull Double value) {
if (!value.isInfinite() && !value.isNaN()) {
final DecimalFormat df = new DecimalFormat();
df.setGroupingUsed(false);
df.setMaximumFractionDigits(CalculatorEngine.instance.getPrecision());
return df.format(new BigDecimal(value).setScale(CalculatorEngine.instance.getPrecision(), BigDecimal.ROUND_HALF_UP).doubleValue());
} else {
return String.valueOf(value);
}
} }
protected String createResultForComplexNumber(@NotNull final String s) { protected String createResultForComplexNumber(@NotNull final String s) {

View File

@ -21,7 +21,11 @@ import org.solovyev.common.utils.CollectionsUtils;
import org.solovyev.common.utils.Formatter; import org.solovyev.common.utils.Formatter;
import org.solovyev.common.utils.MutableObject; import org.solovyev.common.utils.MutableObject;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.HashSet; import java.util.HashSet;
import java.util.Locale;
import java.util.Set; import java.util.Set;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -56,6 +60,27 @@ public enum CalculatorEngine {
@NotNull @NotNull
private final static Set<String> tooLongExecutionCache = new HashSet<String>(); private final static Set<String> tooLongExecutionCache = new HashSet<String>();
@NotNull
private DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols(Locale.getDefault());
@NotNull
public String format(@NotNull Double value) {
if (!value.isInfinite() && !value.isNaN()) {
final DecimalFormat df = new DecimalFormat();
df.setDecimalFormatSymbols(decimalGroupSymbols);
df.setGroupingUsed(true);
df.setMaximumFractionDigits(instance.getPrecision());
return df.format(new BigDecimal(value).setScale(instance.getPrecision(), BigDecimal.ROUND_HALF_UP).doubleValue());
} else {
return String.valueOf(value);
}
}
{
decimalGroupSymbols.setDecimalSeparator('.');
decimalGroupSymbols.setGroupingSeparator(' ');
}
public String evaluate(@NotNull JsclOperation operation, public String 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);
@ -191,6 +216,12 @@ public enum CalculatorEngine {
} }
} }
public void setDecimalGroupSymbols(@NotNull DecimalFormatSymbols decimalGroupSymbols) {
synchronized (lock) {
this.decimalGroupSymbols = decimalGroupSymbols;
}
}
@NotNull @NotNull
public VarsRegister getVarsRegister() { public VarsRegister getVarsRegister() {
return varsRegister; return varsRegister;

View File

@ -27,6 +27,9 @@ public class FromJsclSimplifyTextProcessor implements TextProcessor<String> {
String number = null; String number = null;
for (int i = 0; i < s.length(); i++) { for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i); char ch = s.charAt(i);
if ( Character.isWhitespace(ch) ) {
continue;
}
mathTypeResult = MathType.getType(s, i); mathTypeResult = MathType.getType(s, i);
@ -48,6 +51,8 @@ public class FromJsclSimplifyTextProcessor implements TextProcessor<String> {
} }
numberBuilder = null; numberBuilder = null;
} else {
number = null;
} }
} }
@ -73,6 +78,8 @@ public class FromJsclSimplifyTextProcessor implements TextProcessor<String> {
} }
numberBuilder = null; numberBuilder = null;
} else {
number = null;
} }
replaceSystemVars(sb, number); replaceSystemVars(sb, number);
@ -171,6 +178,9 @@ public class FromJsclSimplifyTextProcessor implements TextProcessor<String> {
sb.delete(sb.length() - number.length(), sb.length()); sb.delete(sb.length() - number.length(), sb.length());
sb.append(var.getName()); sb.append(var.getName());
result = MathType.constant; result = MathType.constant;
} else {
sb.delete(sb.length() - number.length(), sb.length());
sb.append(CalculatorEngine.instance.format(Double.valueOf(number)));
} }
} }

View File

@ -33,6 +33,10 @@ class ToJsclTextProcessor implements TextProcessor<PreparedExpression> {
char ch = s.charAt(i); char ch = s.charAt(i);
startsWithFinder.setI(i); startsWithFinder.setI(i);
if ( Character.isWhitespace(ch)) {
continue;
}
mathTypeResult = checkMultiplicationSignBeforeFunction(sb, s, i, mathTypeResult); mathTypeResult = checkMultiplicationSignBeforeFunction(sb, s, i, mathTypeResult);
final MathType mathType = mathTypeResult.getMathType(); final MathType mathType = mathTypeResult.getMathType();

View File

@ -11,6 +11,9 @@ import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.android.calculator.jscl.JsclOperation;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
/** /**
* User: serso * User: serso
* Date: 9/17/11 * Date: 9/17/11
@ -99,9 +102,9 @@ public class CalculatorEngineTest {
Assert.assertEquals("4", cm.evaluate(JsclOperation.numeric, "k11")); Assert.assertEquals("4", cm.evaluate(JsclOperation.numeric, "k11"));
CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("t", (String)null)); CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("t", (String)null));
Assert.assertEquals("11×t", cm.evaluate(JsclOperation.numeric, "t11")); Assert.assertEquals("11t", cm.evaluate(JsclOperation.numeric, "t11"));
Assert.assertEquals("11×e×t", cm.evaluate(JsclOperation.numeric, "t11e")); Assert.assertEquals("11et", cm.evaluate(JsclOperation.numeric, "t11e"));
Assert.assertEquals("11×Infinity×t", cm.evaluate(JsclOperation.numeric, "t11∞")); Assert.assertEquals("11×Infinityt", cm.evaluate(JsclOperation.numeric, "t11∞"));
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)"));
} }
@ -134,12 +137,16 @@ public class CalculatorEngineTest {
public void testRounding() throws Exception { public void testRounding() throws Exception {
final CalculatorEngine cm = CalculatorEngine.instance; final CalculatorEngine cm = CalculatorEngine.instance;
DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols(Locale.getDefault());
decimalGroupSymbols.setDecimalSeparator('.');
decimalGroupSymbols.setGroupingSeparator('\'');
cm.setDecimalGroupSymbols(decimalGroupSymbols);
cm.setPrecision(2); cm.setPrecision(2);
Assert.assertEquals("12345678.9", cm.evaluate(JsclOperation.numeric, "1.23456789E7")); Assert.assertEquals("12'345'678.9", cm.evaluate(JsclOperation.numeric, "1.23456789E7"));
cm.setPrecision(10); cm.setPrecision(10);
Assert.assertEquals("12345678.899999999", cm.evaluate(JsclOperation.numeric, "1.23456789E7")); Assert.assertEquals("12'345'678.899999999", cm.evaluate(JsclOperation.numeric, "1.23456789E7"));
Assert.assertEquals("123456788.99999999", cm.evaluate(JsclOperation.numeric, "1.234567890E8")); Assert.assertEquals("123'456'788.99999999", cm.evaluate(JsclOperation.numeric, "1.234567890E8"));
Assert.assertEquals("1234567890.1", cm.evaluate(JsclOperation.numeric, "1.2345678901E9")); Assert.assertEquals("1'234'567'890.1", cm.evaluate(JsclOperation.numeric, "1.2345678901E9"));
} }

View File

@ -19,6 +19,9 @@ public class FromJsclSimplifyTextProcessorTest {
@Test @Test
public void testProcess() throws Exception { public void testProcess() throws Exception {
FromJsclSimplifyTextProcessor tp = new FromJsclSimplifyTextProcessor(); FromJsclSimplifyTextProcessor tp = new FromJsclSimplifyTextProcessor();
Assert.assertEquals("(e)", tp.process("(2.718281828459045)"));
Assert.assertEquals("123 456 789e", tp.process("123456789*2.718281828459045"));
Assert.assertEquals("123 456 789e", tp.process("123 456 789 * 2.718281828459045"));
Assert.assertEquals("t11e", tp.process("t11*2.718281828459045")); Assert.assertEquals("t11e", tp.process("t11*2.718281828459045"));
Assert.assertEquals("e", tp.process("2.718281828459045")); Assert.assertEquals("e", tp.process("2.718281828459045"));
Assert.assertEquals("tee", tp.process("t2.718281828459045*2.718281828459045")); Assert.assertEquals("tee", tp.process("t2.718281828459045*2.718281828459045"));