wip print before solution

This commit is contained in:
hugogogo
2026-05-07 13:18:28 +02:00
parent 8c22b98d88
commit f373fef4ac
8 changed files with 77 additions and 19 deletions

View File

@@ -117,7 +117,9 @@ run: $(NAME)
@echo $(B_PURPLE)"\n---------------------------------------------\n8. run with power 4 \n"$(RESET)
-./$(NAME) -d "3x^2 + 2x -7x^4 = x^4"
@echo $(B_PURPLE)"\n---------------------------------------------\n9. run with utf8 \n"$(RESET)
-./$(NAME) -d "3x² + 2x -7x³ = x³"
-./$(NAME) -d "3x² + 2x -7x¹ = x"
@echo $(B_PURPLE)"\n---------------------------------------------\n9. run normal \n"$(RESET)
-./$(NAME) -d "3x² + 2x -7 = x"
clean:
$(RM_OBJS)

View File

@@ -155,6 +155,7 @@ const char *delta_sign_to_str(e_delta_sign sign);
*/
void print_debug(const char *description, ...);
void print_before_solution(double *polynom, int max_exponent);
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* GLOBALS

2
libft

Submodule libft updated: b768ac1a14...413e149003

View File

@@ -91,7 +91,7 @@ static void polynom_fill_null(double *polynom, int len)
int i;
i = 0;
while (i < len)
while (i <= len)
{
polynom[i] = 0.0;
i++;
@@ -125,14 +125,16 @@ void launch_computorv1(char *input)
parse(tokens, terms, terms_count_prediction);
// reduce
max_exponent = get_max_exponent(terms) + 1;
double polynom[max_exponent];
max_exponent = get_max_exponent(terms);
print_debug("-> max_exponent: %i\n\n", max_exponent); // debug
double polynom[max_exponent + 1];
polynom_g_err = polynom;
polynom_len_g_err = max_exponent;
polynom_fill_null(polynom, max_exponent);
reduce(terms, polynom);
// print before solution
print_before_solution(polynom, max_exponent);
// solve
s_solution solution[1];

View File

@@ -22,13 +22,15 @@ static e_term_sign get_sign(s_token *tokens, int i, int *token_count)
{
*token_count = 1;
}
else if (i == 0) // if most left term, the sign can be ommited for a '+' sign in front of a number or variable
else if (i == 0)
{
// if most left term, the sign can be ommited for a '+' sign in front of a number or variable
*token_count = 0;
return TERM_PLUS;
}
else if (tokens[i - 1].type == TOKEN_EQUAL) // if first token after 'equal', the sign can be ommited for a '+' sign in front of a number or variable
else if (tokens[i - 1].type == TOKEN_EQUAL)
{
// if first token after 'equal', the sign can be ommited for a '+' sign in front of a number or variable
*token_count = 0;
return TERM_PLUS;
}

View File

@@ -11,6 +11,7 @@ void reduce(s_term *terms, double *polynom)
i = 0;
while (terms[i].position != TERM_END)
{
// get coefficient with left sign
tmp = terms[i].coefficient;
if (terms[i].position == TERM_RIGHT)

View File

@@ -61,9 +61,9 @@ static void print_context_polynom()
int i;
i = 0;
while (i < polynom_len_g_err)
while (i <= polynom_len_g_err)
{
dprintf(STDERR_FILENO, "polynom[%i]: %10g\n", i, polynom_g_err[i]);
dprintf(STDERR_FILENO, "polynom[%i]: %c -> %10g\n", i, i + 'a', polynom_g_err[i]);
i++;
}
ft_putchar_fd('\n', STDERR_FILENO);
@@ -72,11 +72,11 @@ static void print_context_polynom()
static void print_context_solution()
{
dprintf(STDERR_FILENO, "delta_sign : %25s\n", delta_sign_to_str(solution_g_err->delta_sign));
dprintf(STDERR_FILENO, "delta_absolute : %25g\n", solution_g_err->delta_absolute);
dprintf(STDERR_FILENO, "delta_absolute : %25g (|b² - 4ac|)\n", solution_g_err->delta_absolute);
dprintf(STDERR_FILENO, "first_term_gcd : %25i\n", solution_g_err->first_term_gcd);
dprintf(STDERR_FILENO, "first_term_numerator : %25i\n", solution_g_err->first_term_numerator);
dprintf(STDERR_FILENO, "first_term_denominator : %25i\n", solution_g_err->first_term_denominator);
dprintf(STDERR_FILENO, "first_term : %25g\n", solution_g_err->first_term);
dprintf(STDERR_FILENO, "first_term_numerator : %25i (-b / gcd)\n", solution_g_err->first_term_numerator);
dprintf(STDERR_FILENO, "first_term_denominator : %25i (2a / gcd)\n", solution_g_err->first_term_denominator);
dprintf(STDERR_FILENO, "first_term : %25g (-b / 2a)\n", solution_g_err->first_term);
// dprintf(STDERR_FILENO, "second_term_gcd : %25g\n", solution_g_err->second_term_gcd);
// dprintf(STDERR_FILENO, "second_term_numerator : %25g\n", solution_g_err->second_term_numerator);
// dprintf(STDERR_FILENO, "second_term_denominator: %25g\n", solution_g_err->second_term_denominator);
@@ -106,10 +106,17 @@ void print_state()
void stop_errors(const char *description, ...)
{
// print context (if debug mode)
print_state();
// print context
if (flag_debug_mode)
{
print_state();
}
ft_putstr_fd("\e[1;31mERROR:\e[0m ", STDERR_FILENO);
// print red ERROR
if (flag_debug_mode)
{
ft_putstr_fd("\e[1;31mERROR:\e[0m ", STDERR_FILENO);
}
// print the formatted description
va_list args;
@@ -118,7 +125,11 @@ void stop_errors(const char *description, ...)
va_end(args);
// print the base message
ft_dprintf(STDERR_FILENO, " (errno[%d] : %s)\n", errno, strerror(errno));
if (flag_debug_mode)
{
ft_dprintf(STDERR_FILENO, " (errno[%d] : %s)\n", errno, strerror(errno));
}
// stop program
exit(EXIT_FAILURE);
}

View File

@@ -10,7 +10,46 @@ void print_debug(const char *description, ...)
// print the formatted description
va_list args;
va_start(args, description);
// ft_vdprintf(STDOUT_FILENO, description, args); // it's not handling floats for the moment
vdprintf(STDOUT_FILENO, description, args);
// ft_vdprintf(STDERR_FILENO, description, args); // it's not handling floats for the moment
vdprintf(STDERR_FILENO, description, args);
va_end(args);
}
void print_before_solution(double *polynom, int max_exponent)
{
int i;
// reduced form
ft_putstr("Reduced form: ");
i = max_exponent;
printf("%g * x^%i ", ft_fabs(polynom[i]), i);
fflush(stdout);
i--;
while (i >= 0)
{
if (polynom[i] >= 0)
{
ft_putchar('+');
}
else
{
ft_putchar('-');
}
printf(" %g * x^%i ", ft_fabs(polynom[i]), i);
fflush(stdout);
i--;
}
ft_putstr("= 0\n");
// // check errors
// ft_printf(": ");
// ft_printf(": ");
// ft_printf(": ");
// degree
ft_printf("Polynomial degree: %i\n", max_exponent);
if (max_exponent > 2)
{
stop_errors("The polynomial degree is strictly greater than 2, I can't solve.\n");
}
}