Skip to content

Commit

Permalink
Merge pull request tranleduy2000#69 from axkr/master
Browse files Browse the repository at this point in the history
tranleduy2000#59 improve Solve()
  • Loading branch information
axkr authored Jul 7, 2019
2 parents 127c7d0 + 5dde5a1 commit e286200
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,31 +250,49 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) {
break;
case R.id.nav_permutation:
intent = new Intent(getApplicationContext(), PermutationActivity.class);
if (this instanceof PermutationActivity) {
finish();
}
intent.putExtra(PermutationActivity.TYPE_NUMBER, PermutationActivity.TYPE_PERMUTATION);
postStartActivity(intent);
break;
case R.id.nav_combination:
intent = new Intent(getApplicationContext(), PermutationActivity.class);
if (this instanceof PermutationActivity) {
finish();
}
intent.putExtra(PermutationActivity.TYPE_NUMBER, PermutationActivity.TYPE_COMBINATION);
postStartActivity(intent);
break;
case R.id.nav_catalan:
intent = new Intent(getApplicationContext(), NumberActivity.class);
if (this instanceof NumberActivity) {
finish();
}
intent.putExtra(NumberActivity.DATA, NumberActivity.NumberType.CATALAN);
postStartActivity(intent);
break;
case R.id.nav_fibo:
intent = new Intent(getApplicationContext(), NumberActivity.class);
if (this instanceof NumberActivity) {
finish();
}
intent.putExtra(NumberActivity.DATA, NumberActivity.NumberType.FIBONACCI);
postStartActivity(intent);
break;
case R.id.nav_prime:
intent = new Intent(getApplicationContext(), NumberActivity.class);
if (this instanceof NumberActivity) {
finish();
}
intent.putExtra(NumberActivity.DATA, NumberActivity.NumberType.PRIME);
postStartActivity(intent);
break;
case R.id.action_divisors:
intent = new Intent(getApplicationContext(), NumberActivity.class);
if (this instanceof NumberActivity) {
finish();
}
intent.putExtra(NumberActivity.DATA, NumberActivity.NumberType.DIVISORS);
postStartActivity(intent);
break;
Expand Down
65 changes: 27 additions & 38 deletions app/src/main/java/com/duy/calculator/evaluator/MathEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ public static Exception getError(String expr) {
* @return The value of the expression
* @throws IllegalArgumentException If the user has input a invalid expression
*/
public IExpr evaluateSimple(String exprInput, EvaluateConfig config) {
public IExpr evalStrSimple(String exprInput, EvaluateConfig config) {
IExpr result;
if (config.getEvaluateMode() == EvaluateConfig.DECIMAL) {
result = evaluate("N(" + exprInput + ")");
result = evalStr("N(" + exprInput + ")");
} else {
result = evaluate(exprInput);
result = evalStr(exprInput);
}
return result;
}
Expand All @@ -133,7 +133,7 @@ public IExpr evaluateSimple(IAST function, EvaluateConfig config) {
return result;
}

public IExpr evaluate(String exprInput) {
public IExpr evalStr(String exprInput) {
return mExprEvaluator.eval(exprInput);
}

Expand Down Expand Up @@ -167,7 +167,7 @@ public void evaluateWithResultNormal(String expression, LogicEvaluator.EvaluateC
expression = addUserDefinedVariable(expression); //$ans = ...

try {
IExpr iExpr = evaluateSimple(expression, config);
IExpr iExpr = evalStrSimple(expression, config);
callback.onEvaluated(expression, toFormattedString(iExpr), RESULT_OK);
} catch (Exception e) {
callback.onCalculateError(e);
Expand Down Expand Up @@ -206,7 +206,7 @@ public String getString(int id) {
}

public String evaluateWithResultNormal(String expression) {
IExpr iExpr = evaluateSimple(expression,
IExpr iExpr = evalStrSimple(expression,
EvaluateConfig.newInstance().setEvalMode(EvaluateConfig.FRACTION));
try {
return toFormattedString(iExpr);
Expand Down Expand Up @@ -248,7 +248,7 @@ public String evaluateWithResultAsTex(String exprStr, EvaluateConfig config) {

ExpressionChecker.checkExpression(exprStr);

IExpr result = evaluateSimple(exprStr, config);
IExpr result = evalStrSimple(exprStr, config);
return LaTexFactory.toLaTeX(result);
}

Expand All @@ -261,7 +261,7 @@ public String evaluateWithResultAsTex(EvaluateConfig config, IExpr head, String.
IExpr[] exprArgs = new IExpr[args.length];

for (int i = 0; i < args.length; i++) {
exprArgs[i] = evaluate(args[i]);
exprArgs[i] = evalStr(args[i]);
}
IAST function = F.ast(exprArgs, head);

Expand Down Expand Up @@ -293,31 +293,14 @@ public String solveEquation(String solveStr, final EvaluateConfig config, Contex
if (config.getEvaluateMode() == EvaluateConfig.DECIMAL) {
solveStr = "N(" + solveStr + ")";
}
String roots = mExprEvaluator.evaluate(solveStr).toString();
IExpr roots = mExprEvaluator.evaluate(solveStr);

if (roots.toLowerCase().contains("solve")) {
if (!roots.isFree(F.Solve)) {
return context.getString(R.string.not_find_root);
} else if (roots.contains("{}")) {
} else if (!roots.isListOfLists()) {
return context.getString(R.string.no_root);
}

roots = roots.replaceAll("\\s+", "").replaceAll("->", "==");
int j = 1;
ArrayList<String> listRoot = new ArrayList<>();
for (int i = 1; i < roots.length() - 1; i++) {
if (roots.charAt(i) == '}') {
String tmp = roots.substring(j + 1, i);
i += 2;
j = i;
listRoot.add(tmp);
}
}

StringBuilder result = new StringBuilder();
for (int i = 0; i < listRoot.size(); i++) {
result.append(evaluateWithResultAsTex(listRoot.get(i), config));
}
return result.toString();
return LaTexFactory.toLaTeX(roots);
}


Expand Down Expand Up @@ -426,16 +409,22 @@ public String factorPrime(String input) {
* Solve system equations and return string result
*/
public String solveSystemEquation(String expr, EvaluateConfig config, Context context) {
if (config.getEvaluateMode() == EvaluateConfig.DECIMAL) {
expr = "N(" + expr + ")";
}
IExpr result = evaluate(expr);
if (result.toString().toLowerCase().contains("solve")) {
return context.getString(R.string.not_find_root);
} else if (result.toString().equalsIgnoreCase("{}")) {
return context.getString(R.string.no_root);
try {
IExpr temp = mExprEvaluator.parse(expr);
if (config.getEvaluateMode() == EvaluateConfig.DECIMAL) {
temp = F.N(temp);
}
IExpr result = evaluate(temp);
if (result.toString().toLowerCase().contains("solve")) {
return context.getString(R.string.not_find_root);
} else if (result.toString().equalsIgnoreCase("{}")) {
return context.getString(R.string.no_root);
}
return LaTexFactory.toLaTeX(result);
} catch (RuntimeException rex) {

}
return LaTexFactory.toLaTeX(result);
return LaTexFactory.toLaTeX(F.stringx("Error in input: " + expr.toString()));
/* ArrayList<String> listRoots = new ArrayList<>();
String[] s1 = result.split(Pattern.quote("},{"));
for (int i = 0; i < s1.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
finish();
return;
}
type = bundle;

if (bundle == TYPE_PERMUTATION) {
setTitle(R.string.permutation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public Command<ArrayList<String>, String> getCommand() {
@WorkerThread
@Override
public ArrayList<String> execute(String input) {
IExpr iExpr = MathEvaluator.getInstance().evaluate(input);
IExpr iExpr = MathEvaluator.getInstance().evalStr(input);
String result = LaTexFactory.toLaTeX(iExpr);
return Lists.newArrayList(result);
}
Expand Down

0 comments on commit e286200

Please sign in to comment.