From f373fef4ac6621d5acafc800e9a3fe8041e2aa8a Mon Sep 17 00:00:00 2001 From: hugogogo Date: Thu, 7 May 2026 13:18:28 +0200 Subject: [PATCH] wip print before solution --- Makefile | 4 +++- headers/computorv1.h | 1 + libft | 2 +- src/launcher.c | 8 +++++--- src/parser.c | 6 ++++-- src/reducer.c | 1 + src/utils/errors.c | 31 +++++++++++++++++++++---------- src/utils/printer.c | 43 +++++++++++++++++++++++++++++++++++++++++-- 8 files changed, 77 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index c53c086..130d8ba 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/headers/computorv1.h b/headers/computorv1.h index f14dede..eb2666e 100644 --- a/headers/computorv1.h +++ b/headers/computorv1.h @@ -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 diff --git a/libft b/libft index b768ac1..413e149 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit b768ac1a141f853889660dfabdce0ad6e47be034 +Subproject commit 413e1490036a59d58a1072275ddaaf3421abbbe0 diff --git a/src/launcher.c b/src/launcher.c index 359e2d3..d899e6c 100644 --- a/src/launcher.c +++ b/src/launcher.c @@ -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]; diff --git a/src/parser.c b/src/parser.c index f7597b9..8ca2911 100644 --- a/src/parser.c +++ b/src/parser.c @@ -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; } diff --git a/src/reducer.c b/src/reducer.c index 41f81fa..453efb7 100644 --- a/src/reducer.c +++ b/src/reducer.c @@ -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) diff --git a/src/utils/errors.c b/src/utils/errors.c index 6e3292c..d836e4f 100644 --- a/src/utils/errors.c +++ b/src/utils/errors.c @@ -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); } \ No newline at end of file diff --git a/src/utils/printer.c b/src/utils/printer.c index 4867341..a367ab2 100644 --- a/src/utils/printer.c +++ b/src/utils/printer.c @@ -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"); + } } \ No newline at end of file