numeral bases added
This commit is contained in:
parent
a211c76fd8
commit
9867ca9949
@ -12,6 +12,5 @@
|
||||
calc:textDown="e"
|
||||
calc:textUp="π"
|
||||
calc:directionTextScale="0.5"
|
||||
|
||||
style="?digitButtonStyle"
|
||||
a:onClick="digitButtonClickHandler"/>
|
@ -5,6 +5,7 @@
|
||||
|
||||
package org.solovyev.android.calculator.math;
|
||||
|
||||
import jscl.NumeralBase;
|
||||
import jscl.math.function.Constant;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
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.Finder;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
|
||||
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
|
||||
public boolean isNeedMultiplicationSignBefore(@NotNull MathType mathTypeBefore) {
|
||||
return super.isNeedMultiplicationSignBefore(mathTypeBefore) && mathTypeBefore != digit && mathTypeBefore != dot;
|
||||
public List<String> getTokens() {
|
||||
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, ","),
|
||||
|
||||
text(1200, false, false) {
|
||||
|
@ -147,7 +147,7 @@ public enum CalculatorEngine {
|
||||
|
||||
public Result evaluate(@NotNull JsclOperation operation,
|
||||
@NotNull String expression,
|
||||
@Nullable MessageRegistry<AndroidMessage> mr) throws ParseException {
|
||||
@Nullable MessageRegistry mr) throws ParseException {
|
||||
synchronized (lock) {
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
package org.solovyev.android.calculator.model;
|
||||
|
||||
import jscl.NumeralBase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.math.MathType;
|
||||
@ -30,6 +31,9 @@ public class NumberBuilder {
|
||||
|
||||
private final boolean simpleFormat;
|
||||
|
||||
@Nullable
|
||||
private NumeralBase nb;
|
||||
|
||||
public NumberBuilder(boolean simpleFormat) {
|
||||
this.simpleFormat = simpleFormat;
|
||||
}
|
||||
@ -39,13 +43,17 @@ public class NumberBuilder {
|
||||
number = null;
|
||||
|
||||
final MathType.Result possibleResult;
|
||||
if (CollectionsUtils.contains(mathTypeResult.getMathType(), MathType.digit, MathType.dot, MathType.grouping_separator, MathType.power_10) ||
|
||||
isSignAfterE(mathTypeResult)) {
|
||||
if ((CollectionsUtils.contains(mathTypeResult.getMathType(), MathType.digit, MathType.numeral_base, MathType.dot, MathType.grouping_separator, MathType.power_10) ||
|
||||
isSignAfterE(mathTypeResult)) && numeralBaseCheck(mathTypeResult)) {
|
||||
if (numberBuilder == null) {
|
||||
numberBuilder = new StringBuilder();
|
||||
}
|
||||
|
||||
numberBuilder.append(mathTypeResult.getMatch());
|
||||
if (mathTypeResult.getMathType() != MathType.numeral_base) {
|
||||
numberBuilder.append(mathTypeResult.getMatch());
|
||||
} else {
|
||||
nb = NumeralBase.getByPrefix(mathTypeResult.getMatch());
|
||||
}
|
||||
|
||||
possibleResult = null;
|
||||
} else {
|
||||
@ -55,6 +63,23 @@ public class NumberBuilder {
|
||||
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) {
|
||||
if ("-".equals(mathTypeResult.getMatch()) || "+".equals(mathTypeResult.getMatch())) {
|
||||
if (numberBuilder != null && numberBuilder.length() > 0) {
|
||||
@ -88,6 +113,7 @@ public class NumberBuilder {
|
||||
}
|
||||
|
||||
numberBuilder = null;
|
||||
nb = null;
|
||||
} else {
|
||||
number = null;
|
||||
}
|
||||
|
@ -160,7 +160,7 @@ public class Var implements IConstant {
|
||||
Double result = null;
|
||||
if (value != null) {
|
||||
try {
|
||||
result = ExtendedConstant.getDoubleValue0(getName(), value);
|
||||
result = Double.valueOf(value);
|
||||
} catch (NumberFormatException e) {
|
||||
// do nothing - string is not a double
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user