fix null coefficient skipped
This commit is contained in:
2
libft
2
libft
Submodule libft updated: f662d7f750...85423f4211
10
src/parser.c
10
src/parser.c
@@ -208,7 +208,7 @@ void parse(s_token *tokens, s_term *terms, int terms_count_max)
|
|||||||
|
|
||||||
check_variables(tokens);
|
check_variables(tokens);
|
||||||
|
|
||||||
print_debug("PARSER STEPS :\n"); // debug
|
print_debug("PARSER STEPS :\n");
|
||||||
|
|
||||||
terms_count = 0;
|
terms_count = 0;
|
||||||
token_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;
|
term_position = TERM_LEFT;
|
||||||
while (tokens[i].type != TOKEN_END && terms_count < terms_count_max)
|
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
|
// equal
|
||||||
if (tokens[i].type == TOKEN_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;
|
sign = -1;
|
||||||
}
|
}
|
||||||
i += token_count;
|
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
|
// coefficient
|
||||||
double ret_coefficient = get_coefficient(tokens, i, &token_count);
|
double ret_coefficient = get_coefficient(tokens, i, &token_count);
|
||||||
terms[terms_count].coefficient = ret_coefficient * sign;
|
terms[terms_count].coefficient = ret_coefficient * sign;
|
||||||
i += token_count;
|
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
|
// exponent
|
||||||
int ret_exponent = get_exponent(tokens, i, &token_count);
|
int ret_exponent = get_exponent(tokens, i, &token_count);
|
||||||
terms[terms_count].exponent = ret_exponent;
|
terms[terms_count].exponent = ret_exponent;
|
||||||
i += token_count;
|
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++;
|
terms_count++;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,5 +64,10 @@ int reduce(s_term *terms, s_polynom *polynom, int max_exponent)
|
|||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// fill END polynom
|
||||||
|
polynom[i].coefficient = 0.0;
|
||||||
|
polynom[i].exponent = 0;
|
||||||
|
polynom[i].sign = TERM_SIGN_END;
|
||||||
|
|
||||||
return degree;
|
return degree;
|
||||||
}
|
}
|
||||||
33
src/solver.c
33
src/solver.c
@@ -2,6 +2,29 @@
|
|||||||
|
|
||||||
#include "computorv1.h"
|
#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)
|
static double positiv_zero(double num)
|
||||||
{
|
{
|
||||||
if (num == 0)
|
if (num == 0)
|
||||||
@@ -48,16 +71,16 @@ void solve(const s_polynom *polynom, s_solution *solution)
|
|||||||
|
|
||||||
if (solution->degree == 1)
|
if (solution->degree == 1)
|
||||||
{
|
{
|
||||||
a = polynom[0].coefficient;
|
a = get_coefficient_of_power(1, polynom);
|
||||||
b = polynom[1].coefficient;
|
b = get_coefficient_of_power(0, polynom);
|
||||||
|
|
||||||
solve_degree_1(&solution->solution_degree_1, a, b);
|
solve_degree_1(&solution->solution_degree_1, a, b);
|
||||||
}
|
}
|
||||||
else if (solution->degree == 2)
|
else if (solution->degree == 2)
|
||||||
{
|
{
|
||||||
a = polynom[0].coefficient;
|
a = get_coefficient_of_power(2, polynom);
|
||||||
b = polynom[1].coefficient;
|
b = get_coefficient_of_power(1, polynom);
|
||||||
c = polynom[2].coefficient;
|
c = get_coefficient_of_power(0, polynom);
|
||||||
|
|
||||||
solve_degree_2(&solution->solution_degree_2, a, b, c);
|
solve_degree_2(&solution->solution_degree_2, a, b, c);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user