numeral bases added

This commit is contained in:
Sergey Solovyev 2011-11-29 14:17:59 +04:00
parent 0dc95a3279
commit e629c9502b
5 changed files with 68 additions and 13 deletions

View File

@ -12,6 +12,5 @@
calc:textDown="e" calc:textDown="e"
calc:textUp="π" calc:textUp="π"
calc:directionTextScale="0.5" calc:directionTextScale="0.5"
style="?digitButtonStyle" style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/> a:onClick="digitButtonClickHandler"/>

View File

@ -5,6 +5,7 @@
package org.solovyev.android.calculator.math; package org.solovyev.android.calculator.math;
import jscl.NumeralBase;
import jscl.math.function.Constant; import jscl.math.function.Constant;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -14,18 +15,27 @@ import org.solovyev.android.calculator.model.ParseException;
import org.solovyev.common.utils.CollectionsUtils; import org.solovyev.common.utils.CollectionsUtils;
import org.solovyev.common.utils.Finder; import org.solovyev.common.utils.Finder;
import java.util.Arrays; import java.util.*;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public enum MathType { public enum MathType {
digit(100, true, true, "0", "1", "2", "3", "4", "5", "6", "7", "8", "9") { numeral_base(50, true, false) {
private final List<String> tokens = new ArrayList<String>(10);
{
for (NumeralBase numeralBase : NumeralBase.values()) {
final String jsclPrefix = numeralBase.getJsclPrefix();
if (jsclPrefix != null) {
tokens.add(jsclPrefix);
}
}
}
@NotNull
@Override @Override
public boolean isNeedMultiplicationSignBefore(@NotNull MathType mathTypeBefore) { public List<String> getTokens() {
return super.isNeedMultiplicationSignBefore(mathTypeBefore) && mathTypeBefore != digit && mathTypeBefore != dot; return tokens;
} }
}, },
@ -118,6 +128,26 @@ public enum MathType {
} }
}, },
digit(1125, true, true) {
private final List<String> tokens = new ArrayList<String>(16);
{
for (Character character : NumeralBase.hex.getAcceptableCharacters()) {
tokens.add(character.toString());
}
}
@Override
public boolean isNeedMultiplicationSignBefore(@NotNull MathType mathTypeBefore) {
return super.isNeedMultiplicationSignBefore(mathTypeBefore) && mathTypeBefore != digit && mathTypeBefore != dot && mathTypeBefore != numeral_base;
}
@NotNull
@Override
public List<String> getTokens() {
return tokens;
}
},
comma(1150, false, false, ","), comma(1150, false, false, ","),
text(1200, false, false) { text(1200, false, false) {

View File

@ -147,7 +147,7 @@ public enum CalculatorEngine {
public Result evaluate(@NotNull JsclOperation operation, public Result evaluate(@NotNull JsclOperation operation,
@NotNull String expression, @NotNull String expression,
@Nullable MessageRegistry<AndroidMessage> mr) throws ParseException { @Nullable MessageRegistry mr) throws ParseException {
synchronized (lock) { synchronized (lock) {
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();

View File

@ -6,6 +6,7 @@
package org.solovyev.android.calculator.model; package org.solovyev.android.calculator.model;
import jscl.NumeralBase;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.math.MathType; import org.solovyev.android.calculator.math.MathType;
@ -30,6 +31,9 @@ public class NumberBuilder {
private final boolean simpleFormat; private final boolean simpleFormat;
@Nullable
private NumeralBase nb;
public NumberBuilder(boolean simpleFormat) { public NumberBuilder(boolean simpleFormat) {
this.simpleFormat = simpleFormat; this.simpleFormat = simpleFormat;
} }
@ -39,13 +43,17 @@ public class NumberBuilder {
number = null; number = null;
final MathType.Result possibleResult; final MathType.Result possibleResult;
if (CollectionsUtils.contains(mathTypeResult.getMathType(), MathType.digit, MathType.dot, MathType.grouping_separator, MathType.power_10) || if ((CollectionsUtils.contains(mathTypeResult.getMathType(), MathType.digit, MathType.numeral_base, MathType.dot, MathType.grouping_separator, MathType.power_10) ||
isSignAfterE(mathTypeResult)) { isSignAfterE(mathTypeResult)) && numeralBaseCheck(mathTypeResult)) {
if (numberBuilder == null) { if (numberBuilder == null) {
numberBuilder = new StringBuilder(); numberBuilder = new StringBuilder();
} }
if (mathTypeResult.getMathType() != MathType.numeral_base) {
numberBuilder.append(mathTypeResult.getMatch()); numberBuilder.append(mathTypeResult.getMatch());
} else {
nb = NumeralBase.getByPrefix(mathTypeResult.getMatch());
}
possibleResult = null; possibleResult = null;
} else { } else {
@ -55,6 +63,23 @@ public class NumberBuilder {
return possibleResult == null ? mathTypeResult : possibleResult; return possibleResult == null ? mathTypeResult : possibleResult;
} }
private boolean numeralBaseCheck( @NotNull MathType.Result mathType ) {
if ( mathType.getMathType() == MathType.digit ) {
final Character ch = mathType.getMatch().charAt(0);
if ( NumeralBase.hex.getAcceptableCharacters().contains(ch) && !NumeralBase.dec.getAcceptableCharacters().contains(ch) ) {
if ( nb == NumeralBase.hex ) {
return true;
} else {
return false;
}
} else {
return true;
}
} else {
return true;
}
}
private boolean isSignAfterE(@NotNull MathType.Result mathTypeResult) { private boolean isSignAfterE(@NotNull MathType.Result mathTypeResult) {
if ("-".equals(mathTypeResult.getMatch()) || "+".equals(mathTypeResult.getMatch())) { if ("-".equals(mathTypeResult.getMatch()) || "+".equals(mathTypeResult.getMatch())) {
if (numberBuilder != null && numberBuilder.length() > 0) { if (numberBuilder != null && numberBuilder.length() > 0) {
@ -88,6 +113,7 @@ public class NumberBuilder {
} }
numberBuilder = null; numberBuilder = null;
nb = null;
} else { } else {
number = null; number = null;
} }

View File

@ -160,7 +160,7 @@ public class Var implements IConstant {
Double result = null; Double result = null;
if (value != null) { if (value != null) {
try { try {
result = ExtendedConstant.getDoubleValue0(getName(), value); result = Double.valueOf(value);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
// do nothing - string is not a double // do nothing - string is not a double
} }