From f62a6fe94f66a00dd6d6fc5262140c06541ed1ed Mon Sep 17 00:00:00 2001 From: hugogogo Date: Thu, 7 May 2026 16:05:00 +0200 Subject: [PATCH] fix output beautify error --- Makefile | 30 ++++++++++-- libft | 2 +- src/main.c | 3 +- src/utils/printer.c | 112 ++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 134 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 2d7713e..f62ea2c 100644 --- a/Makefile +++ b/Makefile @@ -118,10 +118,34 @@ run: $(NAME) -./$(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" - @echo $(B_PURPLE)"\n---------------------------------------------\n10. run normal \n"$(RESET) - -./$(NAME) -d "3x² + 2x -7 = x" - @echo $(B_PURPLE)"\n---------------------------------------------\n11. run \n"$(RESET) + @echo $(B_PURPLE)"\n---------------------------------------------\n10. run \n"$(RESET) -./$(NAME) "3x² + 2x -7 = x" + @echo $(B_PURPLE)"\n---------------------------------------------\n11. run \n"$(RESET) + -./$(NAME) "-3x² + 2x -7 = x" + @echo $(B_PURPLE)"\n---------------------------------------------\n12. run \n"$(RESET) + -./$(NAME) "+3x² + 2x -7 = x" + @echo $(B_PURPLE)"\n---------------------------------------------\n13. run \n"$(RESET) + -./$(NAME) "3x² + 0x -7 = x" + @echo $(B_PURPLE)"\n---------------------------------------------\n14. run \n"$(RESET) + -./$(NAME) "3x² + 0x -0 = x" + @echo $(B_PURPLE)"\n---------------------------------------------\n15. run \n"$(RESET) + -./$(NAME) "3x² + 2x -0 = x" + @echo $(B_PURPLE)"\n---------------------------------------------\n16. run -b \n"$(RESET) + -./$(NAME) -b "3x² + 2x -7 = x" + @echo $(B_PURPLE)"\n---------------------------------------------\n17. run -b \n"$(RESET) + -./$(NAME) -b "-3x² + 2x -7 = x" + @echo $(B_PURPLE)"\n---------------------------------------------\n18. run -b \n"$(RESET) + -./$(NAME) -b "+3x² + 2x -7 = x" + @echo $(B_PURPLE)"\n---------------------------------------------\n19. run -b \n"$(RESET) + -./$(NAME) -b "3x² + 0x -7 = x" + @echo $(B_PURPLE)"\n---------------------------------------------\n20. run -b \n"$(RESET) + -./$(NAME) -b "3x² + 2x -0 = x" + @echo $(B_PURPLE)"\n---------------------------------------------\n21. run -b \n"$(RESET) + -./$(NAME) -b "3x² + 0x -0 = x" + @echo $(B_PURPLE)"\n---------------------------------------------\n22. run -b \n"$(RESET) + -./$(NAME) -b "3x² + x -0 = x" + @echo $(B_PURPLE)"\n---------------------------------------------\n22. run -b \n"$(RESET) + -./$(NAME) -b "0x² + x -0 = x" clean: $(RM_OBJS) diff --git a/libft b/libft index aa35d29..3cf0b6e 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit aa35d294f2ff1bb28cc6c20e89ed2e0deedf11e1 +Subproject commit 3cf0b6ecb9f40a962c4ad1971e5bf21c9aa16fdd diff --git a/src/main.c b/src/main.c index fb74b2c..d6360fc 100644 --- a/src/main.c +++ b/src/main.c @@ -120,6 +120,7 @@ int main(int ac, char **av) // init flags flag_debug_mode = false; flag_loop_mode = false; + flag_beautify_mode = false; // check arguments program_mode = MODE_ARGV; @@ -145,7 +146,7 @@ int main(int ac, char **av) flag_loop_mode = true; program_mode = MODE_LOOP; } - else if ((ft_strcmp(av[i], "-l") == 0)) + else if ((ft_strcmp(av[i], "-b") == 0)) { // flag -b : beautify output flag_beautify_mode = true; diff --git a/src/utils/printer.c b/src/utils/printer.c index 93e1d57..94b6d02 100644 --- a/src/utils/printer.c +++ b/src/utils/printer.c @@ -15,29 +15,125 @@ void print_debug(const char *description, ...) va_end(args); } -void print_reduced_form(double *polynom, int max_exponent) +/** +- [x] : ./computorv1 -b "3x² + 0x -0 = x" + Reduced form: 3x² - = 0 +- [x] : ./computorv1 -b "3x² + 0x -7 = x" + Reduced form: 3x² - - 7 = 0 +- [x] : ./computorv1 -b "-3x² + 2x -7 = x" + Reduced form: -+ x - 7 = 0 + */ +static void print_reduced_form_beautify(double *polynom, int max_exponent) { int i; + bool is_first_term; + double value; + int sign; - // reduced form ft_putstr("Reduced form: "); + i = max_exponent; - printf("%g * x^%i ", ft_fabs(polynom[i]), i); - fflush(stdout); - i--; + is_first_term = true; while (i >= 0) { - if (polynom[i] >= 0) + value = ft_fabs(polynom[i]); + sign = ft_sign_f(polynom[i]); + // dprintf(STDERR_FILENO, "\nDEBUG: polynom[%i]: %g, sign: %i\n", i, value, sign); // debug + + // if term is zero dont output + if (value == 0) { - ft_putchar('+'); + // ft_dprintf(STDERR_FILENO, "DEBUG: is 0\n"); // debug + i--; + continue; + } + // ft_dprintf(STDERR_FILENO, "DEBUG: is not 0\n"); // debug + + // print sign + if (sign > 0) + { + // don't output '+' if is first term + if (!is_first_term) + ft_putchar('+'); } else { ft_putchar('-'); } - printf(" %g * x^%i ", ft_fabs(polynom[i]), i); + + // print term + if (!is_first_term) + { + ft_putchar(' '); + } + + // for x⁰ + if (i == 0) + { + printf("%g ", value); + } + + // for x¹ + if (i == 1) + { + if (value == 1) + printf("x "); + if (value > 1) + printf("%gx ", value); + } + + // for x² + if (i >= 2) + { + if (value == 1) + printf("x%s ", ft_superscript(i + '0')); + if (value > 1) + printf("%gx%s ", value, ft_superscript(i + '0')); + } fflush(stdout); + i--; + is_first_term = false; + } + ft_putstr("= 0\n"); +} + +void print_reduced_form(double *polynom, int max_exponent) +{ + int i; + bool is_first_term; + + if (flag_beautify_mode) + return print_reduced_form_beautify(polynom, max_exponent); + + // reduced form + ft_putstr("Reduced form: "); + i = max_exponent; + is_first_term = true; + while (i >= 0) + { + // print sign + if (polynom[i] > 0) + { + // don't output '+' if is first term + if (!is_first_term) + ft_putchar('+'); + } + else + { + ft_putchar('-'); + } + + // print term + if (!is_first_term) + { + ft_putchar(' '); + } + printf("%g * x^%i ", ft_fabs(polynom[i]), i); + fflush(stdout); + + i--; + is_first_term = false; } ft_putstr("= 0\n"); }