From 00f380f5df98d91ab889d506dac8138b6efede45 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Thu, 14 May 2026 00:08:13 +0200 Subject: [PATCH] fix null coefficient skipped --- libft | 2 +- src/parser.c | 10 +++++----- src/reducer.c | 5 +++++ src/solver.c | 33 ++++++++++++++++++++++++++++----- 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/libft b/libft index f662d7f..85423f4 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit f662d7f750d555e4be10c3e01399e20bf8cc758b +Subproject commit 85423f4211a4fb7144ddbc463618ee20ea5e7812 diff --git a/src/parser.c b/src/parser.c index 9946a11..6d1ddf8 100644 --- a/src/parser.c +++ b/src/parser.c @@ -208,7 +208,7 @@ void parse(s_token *tokens, s_term *terms, int terms_count_max) check_variables(tokens); - print_debug("PARSER STEPS :\n"); // debug + print_debug("PARSER STEPS :\n"); terms_count = 0; token_count = 0; @@ -216,7 +216,7 @@ void parse(s_token *tokens, s_term *terms, int terms_count_max) term_position = TERM_LEFT; while (tokens[i].type != TOKEN_END && terms_count < terms_count_max) { - print_debug("- token[%i]\n", i); // debug + print_debug("- token[%i]\n", i); // equal if (tokens[i].type == TOKEN_EQUAL) @@ -238,19 +238,19 @@ void parse(s_token *tokens, s_term *terms, int terms_count_max) sign = -1; } i += token_count; - print_debug("term[%i] get_sign: (%i)[%s], token_count: [%d]\n", terms_count, ret_sign, term_sign_to_str(ret_sign), token_count); // debug + print_debug("term[%i] get_sign: (%i)[%s], token_count: [%d]\n", terms_count, ret_sign, term_sign_to_str(ret_sign), token_count); // coefficient double ret_coefficient = get_coefficient(tokens, i, &token_count); terms[terms_count].coefficient = ret_coefficient * sign; i += token_count; - print_debug("term[%i] get_coefficient: [%g], token_count: [%d]\n", terms_count, ret_coefficient, token_count); // debug + print_debug("term[%i] get_coefficient: [%g], token_count: [%d]\n", terms_count, ret_coefficient, token_count); // exponent int ret_exponent = get_exponent(tokens, i, &token_count); terms[terms_count].exponent = ret_exponent; i += token_count; - print_debug("term[%i] get_exponent: [%i], token_count: [%d]\n", terms_count, ret_exponent, token_count); // debug + print_debug("term[%i] get_exponent: [%i], token_count: [%d]\n", terms_count, ret_exponent, token_count); terms_count++; } diff --git a/src/reducer.c b/src/reducer.c index 8b69663..3f50294 100644 --- a/src/reducer.c +++ b/src/reducer.c @@ -64,5 +64,10 @@ int reduce(s_term *terms, s_polynom *polynom, int max_exponent) i++; } + // fill END polynom + polynom[i].coefficient = 0.0; + polynom[i].exponent = 0; + polynom[i].sign = TERM_SIGN_END; + return degree; } \ No newline at end of file diff --git a/src/solver.c b/src/solver.c index 1cc3304..91769ae 100644 --- a/src/solver.c +++ b/src/solver.c @@ -2,6 +2,29 @@ #include "computorv1.h" +static double get_coefficient_of_power(int power, const s_polynom *polynom) +{ + int i; + double coefficient; + + // if no term found with exponent, default coefficient is 0 + coefficient = 0; + + i = 0; + while (polynom[i].sign != TERM_SIGN_END) + { + if (polynom[i].exponent == power) + { + coefficient = polynom[i].coefficient; + break; + } + i++; + } + + print_debug("- coefficient of power '%i' : %g (polynom[%i])\n", power, coefficient, i); + return coefficient; +} + static double positiv_zero(double num) { if (num == 0) @@ -48,16 +71,16 @@ void solve(const s_polynom *polynom, s_solution *solution) if (solution->degree == 1) { - a = polynom[0].coefficient; - b = polynom[1].coefficient; + a = get_coefficient_of_power(1, polynom); + b = get_coefficient_of_power(0, polynom); solve_degree_1(&solution->solution_degree_1, a, b); } else if (solution->degree == 2) { - a = polynom[0].coefficient; - b = polynom[1].coefficient; - c = polynom[2].coefficient; + a = get_coefficient_of_power(2, polynom); + b = get_coefficient_of_power(1, polynom); + c = get_coefficient_of_power(0, polynom); solve_degree_2(&solution->solution_degree_2, a, b, c); }