diff --git a/src/main/java/org/solovyev/android/calculator/jscl/FromJsclNumericTextProcessor.java b/src/main/java/org/solovyev/android/calculator/jscl/FromJsclNumericTextProcessor.java index d2ca5202..5c0102b9 100644 --- a/src/main/java/org/solovyev/android/calculator/jscl/FromJsclNumericTextProcessor.java +++ b/src/main/java/org/solovyev/android/calculator/jscl/FromJsclNumericTextProcessor.java @@ -12,6 +12,7 @@ import org.solovyev.android.calculator.math.MathType; import org.solovyev.android.calculator.model.CalculatorEngine; import org.solovyev.android.calculator.model.ParseException; import org.solovyev.android.calculator.model.TextProcessor; +import org.solovyev.common.utils.StringUtils; /** * User: serso @@ -47,40 +48,54 @@ class FromJsclNumericTextProcessor implements TextProcessor { return result; } - private String format(@NotNull String value) { + private String format(@NotNull String value) throws java.lang.NumberFormatException { return CalculatorEngine.instance.format(Double.valueOf(value)); } protected String createResultForComplexNumber(@NotNull final String s) { final Complex complex = new Complex(); - String result = ""; - // may be it's just complex number - int plusIndex = s.lastIndexOf("+"); - if (plusIndex >= 0) { - complex.setReal(format(s.substring(0, plusIndex))); - result += complex.getReal(); - result += "+"; - } else { - plusIndex = s.lastIndexOf("-"); - if (plusIndex >= 0) { - complex.setReal(format(s.substring(0, plusIndex))); - result += complex.getReal(); - result += "-"; - } - } + final StringBuilder result = new StringBuilder(); + // may be it's just complex number + int signIndex = tryRealPart(s, complex, result, "+"); + if (signIndex < 0) { + signIndex = tryRealPart(s, complex, result, "-"); + } int multiplyIndex = s.indexOf("*"); if (multiplyIndex >= 0) { - complex.setImaginary(format(s.substring(plusIndex >= 0 ? plusIndex + 1 : 0, multiplyIndex))); - result += complex.getImaginary(); + complex.setImaginary(format(s.substring(signIndex >= 0 ? signIndex + 1 : 0, multiplyIndex))); + result.append(complex.getImaginary()); } - result += MathType.IMAGINARY_NUMBER; + result.append(MathType.IMAGINARY_NUMBER); - return result; + return result.toString(); + } + + private int tryRealPart(@NotNull String s, + @NotNull Complex complex, + @NotNull StringBuilder result, + @NotNull String sign) { + int index = s.lastIndexOf(sign); + if (index >= 0) { + final String substring = s.substring(0, index); + + if (!StringUtils.isEmpty(substring)) { + try { + complex.setReal(format(substring)); + result.append(complex.getReal()); + } catch (NumberFormatException e) { + // do nothing + } + } + + result.append(sign); + } + + return index; } private class Complex { diff --git a/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java b/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java index f9c4a85d..15879350 100644 --- a/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java +++ b/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java @@ -178,6 +178,31 @@ public class CalculatorEngineTest { } + @Test + public void testI() throws ParseException { + final CalculatorEngine cm = CalculatorEngine.instance; + + Assert.assertEquals("-i", cm.evaluate(JsclOperation.numeric, "i^3").getResult()); + for (int i = 0; i < 1000; i++) { + double real = (Math.random()-0.5) * 1000; + double imag = (Math.random()-0.5) * 1000; + int exp = (int)(Math.random() * 10); + + final StringBuilder sb = new StringBuilder(); + sb.append(real); + if ( imag > 0 ) { + sb.append("+"); + } + sb.append(imag); + sb.append("^").append(exp); + try { + cm.evaluate(JsclOperation.numeric, sb.toString()).getResult(); + } catch (Throwable e) { + fail(sb.toString()); + } + } + } + @Test public void testEmptyFunction() throws Exception { final CalculatorEngine cm = CalculatorEngine.instance;