summaryrefslogtreecommitdiff
path: root/src/math-equation.c
diff options
context:
space:
mode:
authormbkma <[email protected]>2020-10-05 14:39:03 +0200
committerRobert Antoni Buj Gelonch <[email protected]>2020-11-20 12:02:58 +0100
commit0ee339f2571c7266646dc522969b6121d6a03ce7 (patch)
tree5290233eb2419a6daf05b0116cab99cea6259814 /src/math-equation.c
parentd65993cad3792b7e46c51f8e8b8e51662151e93e (diff)
downloadmate-calc-0ee339f2571c7266646dc522969b6121d6a03ce7.tar.bz2
mate-calc-0ee339f2571c7266646dc522969b6121d6a03ce7.tar.xz
Show factorization in history view.
Show exponents in factorization.
Diffstat (limited to 'src/math-equation.c')
-rw-r--r--src/math-equation.c43
1 files changed, 39 insertions, 4 deletions
diff --git a/src/math-equation.c b/src/math-equation.c
index 9b46e73..c739197 100644
--- a/src/math-equation.c
+++ b/src/math-equation.c
@@ -971,6 +971,24 @@ math_equation_set(MathEquation *equation, const gchar *text)
clear_ans(equation, FALSE);
}
+static void
+math_equation_set_with_history(MathEquation *equation, const gchar *text)
+{
+ MathEquationState *state;
+ MPNumber x = mp_new();
+
+ math_equation_get_number(equation, &x);
+ math_equation_set(equation, text);
+
+ /* Notify history */
+ state = get_current_state(equation);
+ g_signal_emit_by_name(equation, "history", state->expression, &x,
+ mp_serializer_get_base(equation->priv->serializer));
+
+ free_state(state);
+ mp_clear(&x);
+}
+
void
math_equation_set_number(MathEquation *equation, const MPNumber *x)
@@ -1313,7 +1331,7 @@ math_equation_look_for_answer(gpointer data)
g_slice_free(MPNumber, result->number_result);
}
else if (result->text_result != NULL) {
- math_equation_set(equation, result->text_result);
+ math_equation_set_with_history(equation, result->text_result);
g_free(result->text_result);
}
g_slice_free(SolveData, result);
@@ -1355,7 +1373,7 @@ static gpointer
math_equation_factorize_real(gpointer data)
{
GString *text;
- GList *factors, *factor;
+ GList *factors, *factor, *next_factor;
MPNumber x = mp_new();
MathEquation *equation = MATH_EQUATION(data);
SolveData *result = g_slice_new0(SolveData);
@@ -1365,15 +1383,32 @@ math_equation_factorize_real(gpointer data)
text = g_string_new("");
- for (factor = factors; factor; factor = factor->next) {
+ int e = 1;
+ for (factor = factors; factor != NULL; factor = factor->next)
+ {
gchar *temp;
MPNumber *n;
n = factor->data;
+ next_factor = factor->next;
+ if (next_factor != NULL && mp_compare(n, next_factor->data) == 0)
+ {
+ e++;
+ continue;
+ }
temp = mp_serializer_to_string(equation->priv->serializer, n);
g_string_append(text, temp);
+
+ if (e > 1)
+ {
+ g_string_append(text, "^");
+ g_string_append_printf(text, "%d", e);
+ }
if (factor->next)
- g_string_append(text, "×");
+ g_string_append(text, " × ");
+
+ e = 1;
+
mp_clear(n);
g_slice_free(MPNumber, n);
g_free(temp);