fix X and reduced form

This commit is contained in:
hugogogo
2026-06-05 12:14:21 +02:00
parent 3732c0b777
commit 1962b87895
7 changed files with 274 additions and 203 deletions

View File

@@ -101,7 +101,7 @@ typedef struct
int exponent; int exponent;
} s_term; } s_term;
void parse(s_token *tokens, s_term *terms, int terms_count_max); void parse(s_token *tokens, s_term *terms);
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* REDUCE.C * REDUCE.C

View File

@@ -54,16 +54,17 @@ static int get_max_exponent(s_term *terms)
i++; i++;
} }
print_debug("-> max_exponent: %i\n\n", max_exponent);
return max_exponent; return max_exponent;
} }
static int get_number_of_exponents(s_term *terms, int max_exponent) static int get_number_of_exponents(s_term *terms, int max_exponent)
{ {
int i; int i;
int nbr_of_exponent; int nbr_of_exponents;
if (max_exponent <= 0 || max_exponent > MAX_VLA_SIZE) if (max_exponent < 0 || max_exponent > MAX_VLA_SIZE)
stop_errors("max_exponent should be between 1 and %d, but got : %d\n", MAX_VLA_SIZE, max_exponent); stop_errors("max_exponent should be between 0 and %d, but got : %d\n", MAX_VLA_SIZE, max_exponent);
int exponent_present[max_exponent]; int exponent_present[max_exponent];
ft_bzero(exponent_present, sizeof(exponent_present)); ft_bzero(exponent_present, sizeof(exponent_present));
@@ -76,16 +77,17 @@ static int get_number_of_exponents(s_term *terms, int max_exponent)
} }
// Count unique exponents // Count unique exponents
nbr_of_exponent = 0; nbr_of_exponents = 0;
i = 0; i = 0;
while (i < max_exponent) while (i < max_exponent)
{ {
if (exponent_present[i] == 1) if (exponent_present[i] == 1)
nbr_of_exponent++; nbr_of_exponents++;
i++; i++;
} }
return nbr_of_exponent; print_debug("-> nbr_of_exponents: %i\n\n", nbr_of_exponents);
return nbr_of_exponents;
} }
void launch_computorv1(char *input) void launch_computorv1(char *input)
@@ -118,15 +120,11 @@ void launch_computorv1(char *input)
s_term terms[terms_count_prediction]; s_term terms[terms_count_prediction];
terms_g_err = terms; terms_g_err = terms;
ft_bzero(terms, sizeof(terms)); ft_bzero(terms, sizeof(terms));
parse(tokens, terms, terms_count_prediction); parse(tokens, terms);
// reduce // reduce
max_exponent = get_max_exponent(terms); max_exponent = get_max_exponent(terms);
print_debug("-> max_exponent: %i\n\n", max_exponent);
nbr_of_exponents = get_number_of_exponents(terms, max_exponent); nbr_of_exponents = get_number_of_exponents(terms, max_exponent);
print_debug("-> nbr_of_exponents: %i\n\n", nbr_of_exponents);
if (nbr_of_exponents <= 0 || nbr_of_exponents > MAX_VLA_SIZE)
stop_errors("nbr_of_exponents should be between 1 and %d, but got : %d\n", MAX_VLA_SIZE, nbr_of_exponents);
s_polynom polynom[nbr_of_exponents + 2]; // +1 for last term, +1 for the degree (eg. degree 2 means 3 terms) s_polynom polynom[nbr_of_exponents + 2]; // +1 for last term, +1 for the degree (eg. degree 2 means 3 terms)
polynom_g_err = polynom; polynom_g_err = polynom;
ft_bzero(polynom, sizeof(polynom)); ft_bzero(polynom, sizeof(polynom));

View File

@@ -256,9 +256,9 @@ void lexerize(const char *input, s_token *tokens)
int tokens_count; int tokens_count;
int input_pos; int input_pos;
int token_size; int token_size;
bool has_equal; // bool has_equal;
has_equal = false; // has_equal = false;
tokens_count = 0; tokens_count = 0;
input_pos = 0; input_pos = 0;
while (input[input_pos]) while (input[input_pos])
@@ -326,7 +326,7 @@ void lexerize(const char *input, s_token *tokens)
} }
else if (token_is_equal(input, input_pos, &token_size)) else if (token_is_equal(input, input_pos, &token_size))
{ {
has_equal = true; // has_equal = true;
tokens[tokens_count].type = TOKEN_EQUAL; tokens[tokens_count].type = TOKEN_EQUAL;
tokens[tokens_count].tag = TOKEN_NO_TAG; tokens[tokens_count].tag = TOKEN_NO_TAG;
tokens[tokens_count].value_char = '='; tokens[tokens_count].value_char = '=';
@@ -344,8 +344,8 @@ void lexerize(const char *input, s_token *tokens)
input_pos += token_size; input_pos += token_size;
} }
if (!has_equal) // if (!has_equal)
stop_errors("the input polynomial does not contains an equal sign, it's an expression not an equation.\n"); // stop_errors("the input polynomial does not contains an equal sign, it's an expression not an equation.\n");
tokens[tokens_count].type = TOKEN_END; tokens[tokens_count].type = TOKEN_END;
tokens[tokens_count].tag = TOKEN_NO_TAG; tokens[tokens_count].tag = TOKEN_NO_TAG;

View File

@@ -222,7 +222,7 @@ static void check_variables(s_token *tokens)
} }
} }
void parse(s_token *tokens, s_term *terms, int terms_count_max) void parse(s_token *tokens, s_term *terms)
{ {
int i; int i;
int terms_count; int terms_count;
@@ -238,7 +238,7 @@ void parse(s_token *tokens, s_term *terms, int terms_count_max)
token_count = 0; token_count = 0;
i = 0; i = 0;
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)
{ {
print_debug("- token[%i]\n", i); print_debug("- token[%i]\n", i);
@@ -277,16 +277,9 @@ void parse(s_token *tokens, s_term *terms, int terms_count_max)
terms_count++; terms_count++;
} }
// last token is TOKEN_END, and terms[] should have at least one more spot for the END term // last token is TOKEN_END
if (tokens[i].type == TOKEN_END && terms_count < terms_count_max) terms[terms_count].position = TERM_POS_END;
{ terms[terms_count].sign = TERM_SIGN_END;
terms[terms_count].position = TERM_POS_END; terms[terms_count].coefficient = 0;
terms[terms_count].sign = TERM_SIGN_END; terms[terms_count].exponent = 0;
terms[terms_count].coefficient = 0;
terms[terms_count].exponent = 0;
}
else
{
stop_errors("terms_count: %i, terms_count_max: %i, tokens[%i].type: %s", terms_count, terms_count_max, i, token_type_to_str(tokens[i].type));
}
} }

View File

@@ -61,18 +61,18 @@ static void print_reduced_form_beautify(s_polynom *polynom)
if (exponent == 1) if (exponent == 1)
{ {
if (coefficient == 1) if (coefficient == 1)
printf("x "); printf("X ");
if (coefficient > 1) if (coefficient > 1)
printf("%gx ", coefficient); printf("%gX ", coefficient);
} }
// for x² // for x²
if (exponent >= 2) if (exponent >= 2)
{ {
if (coefficient == 1) if (coefficient == 1)
printf("x%s ", ft_superscript(exponent + '0')); printf("X%s ", ft_superscript(exponent + '0'));
if (coefficient > 1) if (coefficient > 1)
printf("%gx%s ", coefficient, ft_superscript(exponent + '0')); printf("%gX%s ", coefficient, ft_superscript(exponent + '0'));
} }
fflush(stdout); fflush(stdout);
@@ -116,7 +116,7 @@ void print_reduced_form(s_polynom *polynom, int degree)
// print term // print term
if (!is_first_term) if (!is_first_term)
ft_putchar(' '); ft_putchar(' ');
printf("%g * x^%i ", ft_fabs(found_term->coefficient), i); printf("%g * X^%i ", ft_fabs(found_term->coefficient), i);
fflush(stdout); fflush(stdout);
is_first_term = false; is_first_term = false;

View File

@@ -135,14 +135,13 @@ static void print_solution_delta_zero(s_solution_degree_2 solution)
static void print_solution_delta_positiv(s_solution_degree_2 solution) static void print_solution_delta_positiv(s_solution_degree_2 solution)
{ {
ft_printf("Discriminant is strictly positive, the two solutions are:\n"); ft_printf("Discriminant is strictly positive, the two solutions are:\n");
printf("%g\n", positiv_zero(solution.left_term + solution.right_term));
printf("%g\n", positiv_zero(solution.left_term - solution.right_term)); printf("%g\n", positiv_zero(solution.left_term - solution.right_term));
printf("%g\n", positiv_zero(solution.left_term + solution.right_term));
} }
static void print_solution_delta_negativ(s_solution_degree_2 solution) static void print_solution_delta_negativ(s_solution_degree_2 solution)
{ {
double denominator; double denominator;
double denominator_abs;
int denominator_sign; int denominator_sign;
double right_term_abs; double right_term_abs;
int right_term_sign; int right_term_sign;
@@ -154,32 +153,45 @@ static void print_solution_delta_negativ(s_solution_degree_2 solution)
if (solution.all_int) if (solution.all_int)
{ {
double first_term_numerator;
double first_term_denominator_abs;
double second_term_numerator;
double second_term_denominator_abs;
denominator = positiv_zero(solution.a * 2); denominator = positiv_zero(solution.a * 2);
denominator_abs = ft_fabs(denominator);
denominator_sign = ft_fsign(denominator); denominator_sign = ft_fsign(denominator);
first_term_denominator_abs = ft_fabs(denominator);
first_term_numerator = solution.b;
reduce_fraction(&first_term_numerator, &first_term_denominator_abs);
second_term_denominator_abs = ft_fabs(denominator);
second_term_numerator = positiv_zero(solution.delta_sqrt);
reduce_fraction(&second_term_numerator, &second_term_denominator_abs);
// solution 1 // solution 1
if (!is_nearly_equal_zero(solution.b)) if (!is_nearly_equal_zero(first_term_numerator))
{ {
has_first_term = true; has_first_term = true;
printf("%g/%g ", solution.b * -1 * denominator_sign, denominator_abs); printf("%g/%g ", first_term_numerator * -1 * denominator_sign, first_term_denominator_abs);
} }
if (denominator_sign == -1) if (denominator_sign == -1)
printf("- "); printf("- ");
else if (has_first_term) else if (has_first_term)
printf("+ "); // dont print '+' if it's first term printf("+ "); // dont print '+' if it's first term
printf("%gi/%g\n", positiv_zero(solution.delta_sqrt), denominator_abs); printf("%gi/%g\n", second_term_numerator, second_term_denominator_abs);
// solution 2 // solution 2
if (!is_nearly_equal_zero(solution.b)) if (!is_nearly_equal_zero(first_term_numerator))
{ {
has_first_term = true; has_first_term = true;
printf("%g/%g ", solution.b * -1 * denominator_sign, denominator_abs); printf("%g/%g ", first_term_numerator * -1 * denominator_sign, first_term_denominator_abs);
} }
if (denominator_sign == 1) if (denominator_sign == 1)
printf("- "); printf("- ");
else if (has_first_term) else if (has_first_term)
printf("+ "); // dont print '+' if it's first term printf("+ "); // dont print '+' if it's first term
printf("%gi/%g\n", positiv_zero(solution.delta_sqrt), denominator_abs); printf("%gi/%g\n", second_term_numerator, second_term_denominator_abs);
} }
else else
{ {
@@ -192,7 +204,7 @@ static void print_solution_delta_negativ(s_solution_degree_2 solution)
has_first_term = true; has_first_term = true;
printf("%g ", solution.left_term); printf("%g ", solution.left_term);
} }
if (right_term_sign == -1) if (right_term_sign == 1)
printf("- "); printf("- ");
else if (has_first_term) else if (has_first_term)
printf("+ "); // dont print '+' if it's first term printf("+ "); // dont print '+' if it's first term
@@ -205,7 +217,7 @@ static void print_solution_delta_negativ(s_solution_degree_2 solution)
has_first_term = true; has_first_term = true;
printf("%g ", solution.left_term); printf("%g ", solution.left_term);
} }
if (right_term_sign == 1) if (right_term_sign == -1)
printf("- "); printf("- ");
else if (has_first_term) else if (has_first_term)
printf("+ "); // dont print '+' if it's first term printf("+ "); // dont print '+' if it's first term

380
tester.sh

File diff suppressed because one or more lines are too long