fix output beautify error
This commit is contained in:
@@ -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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user