fix null coefficient skipped

This commit is contained in:
hugogogo
2026-05-14 00:08:13 +02:00
parent 7978f0d706
commit 00f380f5df
4 changed files with 39 additions and 11 deletions

View File

@@ -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++;
}

View File

@@ -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;
}

View File

@@ -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);
}