From b1bd694a8a7c5db25da30774a958537fb3ecc11f Mon Sep 17 00:00:00 2001 From: serso Date: Tue, 15 Mar 2016 21:16:04 +0100 Subject: [PATCH] 1. Check undefined variables in the function 2. Automatically add parameters if a variable has been inserted --- .../calculator/PreparedExpression.java | 5 +++++ .../functions/BaseFunctionFragment.java | 20 ++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/PreparedExpression.java b/app/src/main/java/org/solovyev/android/calculator/PreparedExpression.java index 78ad5600..cdc19d42 100644 --- a/app/src/main/java/org/solovyev/android/calculator/PreparedExpression.java +++ b/app/src/main/java/org/solovyev/android/calculator/PreparedExpression.java @@ -50,6 +50,11 @@ public class PreparedExpression implements CharSequence { return !undefinedVariables.isEmpty(); } + @Nonnull + public List getUndefinedVariables() { + return undefinedVariables; + } + @Override public int length() { return value.length(); diff --git a/app/src/main/java/org/solovyev/android/calculator/functions/BaseFunctionFragment.java b/app/src/main/java/org/solovyev/android/calculator/functions/BaseFunctionFragment.java index db6ce26f..c673167f 100644 --- a/app/src/main/java/org/solovyev/android/calculator/functions/BaseFunctionFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/functions/BaseFunctionFragment.java @@ -38,6 +38,7 @@ import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import butterknife.Bind; import butterknife.ButterKnife; +import jscl.math.function.IConstant; import org.solovyev.android.Check; import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.keyboard.FloatingKeyboardWindow; @@ -275,7 +276,17 @@ public abstract class BaseFunctionFragment extends BaseDialogFragment implements return false; } try { - calculator.prepare(body); + final PreparedExpression pe = calculator.prepare(body); + if (pe.hasUndefinedVariables()) { + // check that all undefined variables are actually function parameters + final List parameters = collectParameters(); + for (IConstant undefinedVariable : pe.getUndefinedVariables()) { + if (!parameters.contains(undefinedVariable.getName())) { + setError(bodyLabel, getString(R.string.c_error)); + return false; + } + } + } clearError(bodyLabel); return true; } catch (ParseException e) { @@ -472,6 +483,13 @@ public abstract class BaseFunctionFragment extends BaseDialogFragment implements bodyView.setSelection(newSelection); } } + // if a parameter has been inserted - update the parameter's list + if (TextUtils.equals(text, "x") || TextUtils.equals(text, "y")) { + final String possibleParam = text.toString(); + if (!collectParameters().contains(possibleParam)) { + paramsView.addParam(possibleParam); + } + } } @Override