24 | * Created by Duy on 24-Jan-17.
25 | */
26 | public class TooBigNumberException extends Exception {
27 |
28 | /**
29 | * Constructor
30 | * @param isTooBig - is value too big
31 | */
32 | public TooBigNumberException(boolean isTooBig) {
33 | super(isTooBig ? "Number too big" : "Number to small");
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
41 | * such as:
42 | *
43 | * Solve(2x + 1, x)
44 | * Int(x, x)
45 | * D(2x + 3)
46 | * Int(sqrt(x), {x, 2, 4}
47 | *
48 | * @return - input
49 | */
50 | public abstract String getInput();
51 |
52 | /**
53 | * return error of input
54 | *
55 | * @param evaluator - BigEvaluator for evaluate input if needed
56 | * @param applicationContext - mContext of application for get string language
57 | * @return - error found
58 | */
59 | public abstract String getError(MathEvaluator evaluator, Context applicationContext);
60 |
61 |
62 | }
63 |
--------------------------------------------------------------------------------
/app/src/main/java/com/app/mathpix_sample/evaluator/convert/ConvertFactory.java:
--------------------------------------------------------------------------------
1 | package com.app.mathpix_sample.evaluator.convert;
2 |
3 | import android.content.Context;
4 | import android.util.Log;
5 |
6 | import java.util.ArrayList;
7 | import java.util.regex.Matcher;
8 | import java.util.regex.Pattern;
9 |
10 | /**
11 | * 创建时间: 2018/10/12
12 | * 作者:yanyinan
13 | * 功能描述:转化器工厂
14 | */
15 | public class ConvertFactory {
16 | /**
17 | * 正则表达式匹配是否包含英文字母
18 | */
19 | private static final String regex=".*[x-zX-Z]+.*";
20 |
21 | public static String TAG = ConvertFactory.class.getSimpleName();
22 |
23 | public static Converter createConverter(ArrayList
31 | * Item for Input Equation
32 | *
33 | * such as:
34 | *
35 | * 2x + 2 = 2;
36 | *
37 | * 3x + 2y = 2t;
38 | *
39 | * 1/sin(x) + 2cos(pi) = 0;
40 | */
41 |
42 | public class EquationItem extends ExprInput {
43 | private String left;
44 | private String right = "0";
45 |
46 | /**
47 | * constructor
48 | *
49 | * 2x = 0
50 | *
51 | * @param expr - equation
52 | */
53 | public EquationItem(String expr) {
54 | expr = expr.replace("==", "=");
55 | String[] s = expr.split(Pattern.quote("="));
56 | if (s.length == 2) {
57 | this.left = FormatExpression.cleanExpression(s[0]);
58 | this.right = FormatExpression.cleanExpression(s[1]);
59 | } else {
60 | this.left = FormatExpression.cleanExpression(s[0]);
61 | this.right = "0";
62 | }
63 | }
64 |
65 | /**
66 | * constructor
67 | *
68 | * 2x = y
69 | *
70 | * @param left - left equation
71 | * @param right - right equation
72 | */
73 | public EquationItem(String left, String right) {
74 | this.left = left;
75 | this.right = right;
76 | }
77 |
78 |
79 | @Override
80 | public boolean isError(MathEvaluator evaluator) {
81 | if (evaluator.isSyntaxError(left)) {
82 | return true;
83 | } else if (evaluator.isSyntaxError(right)) {
84 | return true;
85 | }
86 | return false;
87 | }
88 |
89 | @Override
90 | public String getInput() {
91 | return this.left + "==" + this.right;
92 | }
93 |
94 | @Override
95 | public String getError(MathEvaluator evaluator, Context applicationContext) {
96 | // return evaluator.getError(getInput());
97 | return "";
98 | }
99 | }
100 |
--------------------------------------------------------------------------------
/app/src/main/java/com/app/mathpix_sample/evaluator/EvaluateConfig.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 Duy Tran Le
3 | *
4 | * This program is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * This program is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU General Public License
15 | * along with this program. If not, see
28 | * calculated -> english -> translate -> language -> show view
29 | *
30 | * view -> translate -> english -> calculate
31 | */
32 | public class Tokenizer {
33 | private final ArrayList
33 | * Created by Duy on 3/7/2016
34 | */
35 | public class CalculatorSetting {
36 |
37 | public static final long serialVersionUID = 310L;
38 |
39 | public static final String preName = "app_data";
40 | public static final String INPUT_MATH = "inp_math";
41 | public static final String RESULT_MATH = "res_math";
42 |
43 | public static final String INPUT_BASE = "INPUT_BASE";
44 | public static final String RESULT_BASE = "RESULT_BASE";
45 | public static final String BASE = "BASE";
46 |
47 | public static final String USE_RADIANS = "USE_RADIANS";
48 | public static final String NEW_UPDATE = "new_update" + serialVersionUID;
49 | public static final String GO_TO_MARKET = "GO_TO_MARKET";
50 |
51 | public static final String CMPLX_ON = "COMPLEX";
52 | public static final String INPUT_STATISTIC = "INPUT_STATISTIC";
53 | /**
54 | * duplication with key_pref_fraction on mSetting.xml file
55 | */
56 | public static final String IS_FRACTION = "fraction_mode";
57 |
58 | public static final String IS_DEGREE = "IS_DEGREE";
59 | public static final String NOTIFY_UPDATE = "NOTIFY_UPDATE";
60 | private static final String IS_UPDATE = "IS_UPDATE_" + ConfigApp.VERSION_CODE;
61 | private final SharedPreferences sharedPreferences;
62 | private final SharedPreferences.Editor editor;
63 | private Context context;
64 |
65 | public CalculatorSetting(Context context) {
66 | this.context = context;
67 | this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
68 | this.editor = sharedPreferences.edit();
69 | }
70 |
71 | public CalculatorSetting(SharedPreferences mPreferences, Context context) {
72 | this.context = context;
73 | this.sharedPreferences = mPreferences;
74 | this.editor = sharedPreferences.edit();
75 | }
76 |
77 | public boolean useRadians() {
78 | return sharedPreferences.getBoolean(USE_RADIANS, false);
79 | }
80 |
81 | public boolean useComplex() {
82 | return sharedPreferences.getBoolean(CMPLX_ON, false);
83 | }
84 |
85 | public boolean useLightTheme() {
86 | return sharedPreferences.getBoolean("THEME_STYLE", false);
87 | }
88 |
89 | public boolean instantResult() {
90 | return sharedPreferences.getBoolean(context.getString(R.string.key_pref_instant_res), true);
91 | }
92 |
93 | public boolean useFraction() {
94 | return sharedPreferences.getBoolean(CalculatorSetting.IS_FRACTION, false);
95 | }
96 |
97 | public void setFraction(boolean b) {
98 | editor.putBoolean(IS_FRACTION, b).commit();
99 | }
100 |
101 | public boolean isUpdate(Context context) {
102 | return !(new CalculatorSetting(context).getBoolean(IS_UPDATE));
103 | }
104 |
105 | public void setUpdate(boolean b) {
106 | put(IS_UPDATE, b);
107 | }
108 |
109 | public void put(String key, String value) {
110 | editor.putString(key, value);
111 | editor.commit();
112 | }
113 |
114 | public void put(String key, int value) {
115 | editor.putInt(key, value);
116 | editor.commit();
117 | }
118 |
119 | public void put(String key, boolean value) {
120 | editor.putBoolean(key, value);
121 | editor.commit();
122 | }
123 |
124 | public void put(String key, long value) {
125 | editor.putLong(key, value);
126 | editor.commit();
127 | }
128 |
129 | public void put(String key, float value) {
130 | editor.putFloat(key, value);
131 | editor.commit();
132 | }
133 |
134 | public int getInt(String key) {
135 | return getInt(key, -1);
136 | }
137 |
138 | public int getInt(String key, int def) {
139 | try {
140 | return sharedPreferences.getInt(key, def);
141 | } catch (Exception e) {
142 | String value = getString(context.getString(R.string.key_pref_precision));
143 | try {
144 | return Integer.parseInt(value);
145 | } catch (Exception e1) {
146 |
147 | }
148 | }
149 | return def;
150 | }
151 |
152 | /**
153 | * get long value from key,
154 | *
155 | * @param key - key
156 | * @return -1 if not found
157 | */
158 | public long getLong(String key) {
159 | try {
160 | return sharedPreferences.getLong(key, -1);
161 | } catch (Exception e) {
162 | return -1;
163 | }
164 | }
165 |
166 | public String getString(String key) {
167 | try {
168 | return sharedPreferences.getString(key, "");
169 | } catch (Exception e) {
170 | return "";
171 | }
172 | }
173 |
174 | public String getString(String key, String def) {
175 | try {
176 | return sharedPreferences.getString(key, def);
177 | } catch (Exception e) {
178 | return def;
179 | }
180 | }
181 |
182 | public boolean getBoolean(String key) {
183 | return getBoolean(key, false);
184 | }
185 |
186 | public boolean isNewUpdate() {
187 | return sharedPreferences.getBoolean(NEW_UPDATE, false);
188 | }
189 |
190 | public void setVar(String name, String value) {
191 | editor.putString("var_" + name, value);
192 | }
193 |
194 | public void setNotifyUpdate(boolean b) {
195 | editor.putBoolean(NOTIFY_UPDATE, b).apply();
196 | }
197 |
198 | public boolean useFullScreen() {
199 | return getBoolean(context.getString(R.string.key_hide_status_bar));
200 | }
201 |
202 | public SharedPreferences.Editor getEditor() {
203 | return sharedPreferences.edit();
204 | }
205 |
206 | public int getPrecision() {
207 | return getInt(context.getString(R.string.key_pref_precision));
208 | }
209 |
210 | public void registerOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener
211 | onSharedPreferenceChangeListener) {
212 | sharedPreferences.registerOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);
213 | }
214 |
215 | public void unregisterOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener
216 | onSharedPreferenceChangeListener) {
217 | sharedPreferences.unregisterOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);
218 | }
219 |
220 | public boolean getBoolean(String key, boolean def) {
221 | return sharedPreferences.getBoolean(key, def);
222 |
223 | }
224 |
225 | public static EvaluateConfig createEvaluateConfig(Context context) {
226 | CalculatorSetting setting = new CalculatorSetting(context);
227 | EvaluateConfig config = EvaluateConfig.newInstance();
228 | config.setEvalMode(setting.useFraction() ? EvaluateConfig.FRACTION : EvaluateConfig.DECIMAL);
229 | config.setRoundTo(setting.getPrecision());
230 | // config.setTrigMode(setting.useRadian() ? EvaluateConfig.RADIAN : EvaluateConfig.DEGREE);
231 | return config;
232 | }
233 |
234 | }
235 |
--------------------------------------------------------------------------------
/app/src/main/java/com/app/mathpix_sample/evaluator/FormatExpression.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 Duy Tran Le
3 | *
4 | * This program is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * This program is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU General Public License
15 | * along with this program. If not, see
68 | * append parentheses
69 | *
70 | * replace char (div mul add, sub...)
71 | *
72 | * @param expr - input expression
73 | * @return - math language
74 | */
75 | public static String cleanExpression(String expr, Context context) {
76 | return cleanExpression(expr, new Tokenizer());
77 | }
78 |
79 | /**
80 | * Append parenthesis at the end of unclosed functions
81 | *
9 |
17 |
22 |
29 | ").append(context.getString(R.string.syntax_error))
43 | .append("
").append(context.getString(R.string.reason))
44 | .append("").append(e.getMessage());
45 |
46 | } else if (e instanceof MathException) {
47 | StringBuilder stringBuilder = new StringBuilder();
48 | stringBuilder.append("").append(context.getString(R.string.math_error))
49 | .append("
").append(context.getString(R.string.reason))
50 | .append("").append(e.getMessage());
51 |
52 | return stringBuilder.toString();
53 | }
54 | return e.getMessage();
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/app/src/main/java/com/app/mathpix_sample/evaluator/model/StepItem.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 Duy Tran Le
3 | *
4 | * This program is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * This program is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU General Public License
15 | * along with this program. If not, see
";
63 | public static final String DERIVATIVE = "D";
64 | public static final String SIMPLIFY = "Simplify";
65 | public static final String C = "C";
66 | public static final String P = "P";
67 | public static final String K = "K";
68 |
69 | public static char DECIMAL_POINT;
70 | public static char DECIMAL_SEPARATOR;
71 | public static char BINARY_SEPARATOR;
72 | public static char HEXADECIMAL_SEPARATOR;
73 | public static char MATRIX_SEPARATOR;
74 | public static char OCTAL_SEPARATOR;
75 | public static String REGEX_NUMBER;
76 | public static String REGEX_NOT_NUMBER;
77 |
78 | static {
79 | rebuildConstants();
80 | }
81 |
82 | /**
83 | * If the locale changes, but the app is still in memory, you may need to rebuild these constants
84 | */
85 | public static void rebuildConstants() {
86 | DECIMAL_POINT = '.';
87 | DECIMAL_SEPARATOR = ' ';
88 |
89 | // Use a space for Bin and Hex
90 | BINARY_SEPARATOR = ' ';
91 | HEXADECIMAL_SEPARATOR = ' ';
92 | OCTAL_SEPARATOR = ' ';
93 |
94 | // We have to be careful with the Matrix Separator.
95 | // It defaults to "," but that's a common decimal point.
96 | MATRIX_SEPARATOR = ',';
97 | String number = "A-F0-9" +
98 | Pattern.quote(String.valueOf(DECIMAL_POINT)) +
99 | Pattern.quote(String.valueOf(DECIMAL_SEPARATOR)) +
100 | Pattern.quote(String.valueOf(BINARY_SEPARATOR)) +
101 | Pattern.quote(String.valueOf(OCTAL_SEPARATOR)) +
102 | Pattern.quote(String.valueOf(HEXADECIMAL_SEPARATOR));
103 |
104 | REGEX_NUMBER = "[" + number + "]";
105 | REGEX_NOT_NUMBER = "[^" + number + "]";
106 | }
107 | }
108 |
--------------------------------------------------------------------------------
/app/src/main/java/com/app/mathpix_sample/evaluator/thread/CalculateThread.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 Duy Tran Le
3 | *
4 | * This program is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * This program is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU General Public License
15 | * along with this program. If not, see " + context.getString(R.string.error)
154 | + "[" + (i + 1) + "," + (j + 1) + "]" + "
";
155 | }
156 | }
157 | }
158 | } else {
159 | for (int i = 0; i < equations.size(); i++) {
160 | if (evaluator.isSyntaxError(equations.get(i))) {
161 | return "" + context.getString(R.string.error)
162 | + " " + equations.get(i);
163 | }
164 | }
165 | }
166 | return "";
167 | }
168 | }
169 |
--------------------------------------------------------------------------------
/app/src/main/java/com/app/mathpix_sample/evaluator/data/CalculatorSetting.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 Duy Tran Le
3 | *
4 | * This program is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * (at your option) any later version.
8 | *
9 | * This program is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU General Public License
15 | * along with this program. If not, see