fix output beautify error

This commit is contained in:
hugogogo
2026-05-07 16:05:00 +02:00
parent 09701f8884
commit f62a6fe94f
4 changed files with 134 additions and 13 deletions

View File

@@ -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)

2
libft

Submodule libft updated: aa35d294f2...3cf0b6ecb9

View File

@@ -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;

View File

@@ -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_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('-');
}
// 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");
}