new struct for polynom

This commit is contained in:
hugogogo
2026-05-07 19:38:43 +02:00
parent f62a6fe94f
commit 23c788d0c2
10 changed files with 191 additions and 121 deletions

View File

@@ -23,42 +23,42 @@ void print_debug(const char *description, ...)
- [x] : ./computorv1 -b "-3x² + 2x -7 = x"
Reduced form: -+ x - 7 = 0
*/
static void print_reduced_form_beautify(double *polynom, int max_exponent)
static void print_reduced_form_beautify(s_polynom *polynom)
{
int i;
bool is_first_term;
double value;
int sign;
double coefficient;
int exponent;
char *default_form;
default_form = "0 ";
ft_putstr("Reduced form: ");
i = max_exponent;
i = 0;
is_first_term = true;
while (i >= 0)
while (polynom[i].sign != TERM_SIGN_END)
{
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
coefficient = ft_fabs(polynom[i].coefficient);
exponent = polynom[i].exponent;
// dprintf(STDERR_FILENO, "\nDEBUG: polynom[0].coefficient: %g, polynom[0].exponent: %i, polynom[0].sign: %c\n", polynom[0].coefficient, polynom[0].exponent, polynom[0].sign); // tmpdebug
// if term is zero dont output
if (value == 0)
if (coefficient == 0.0)
{
// ft_dprintf(STDERR_FILENO, "DEBUG: is 0\n"); // debug
i--;
// ft_dprintf(STDERR_FILENO, "DEBUG: is 0\n"); // tmpdebug
i++;
continue;
}
// ft_dprintf(STDERR_FILENO, "DEBUG: is not 0\n"); // debug
// ft_dprintf(STDERR_FILENO, "DEBUG: is not 0\n"); // tmpdebug
// default_form is not nedded anymore since something will be printed
default_form = "";
// print sign
if (sign > 0)
// don't output '+' if is first term
if (!(polynom[i].sign == TERM_PLUS && is_first_term))
{
// don't output '+' if is first term
if (!is_first_term)
ft_putchar('+');
}
else
{
ft_putchar('-');
ft_putchar(polynom[i].sign);
}
// print term
@@ -68,60 +68,55 @@ static void print_reduced_form_beautify(double *polynom, int max_exponent)
}
// for x⁰
if (i == 0)
if (exponent == 0)
{
printf("%g ", value);
printf("%g ", coefficient);
}
// for x¹
if (i == 1)
if (exponent == 1)
{
if (value == 1)
if (coefficient == 1)
printf("x ");
if (value > 1)
printf("%gx ", value);
if (coefficient > 1)
printf("%gx ", coefficient);
}
// for x²
if (i >= 2)
if (exponent >= 2)
{
if (value == 1)
printf("x%s ", ft_superscript(i + '0'));
if (value > 1)
printf("%gx%s ", value, ft_superscript(i + '0'));
if (coefficient == 1)
printf("x%s ", ft_superscript(exponent + '0'));
if (coefficient > 1)
printf("%gx%s ", coefficient, ft_superscript(exponent + '0'));
}
fflush(stdout);
i--;
i++;
is_first_term = false;
}
ft_putstr("= 0\n");
ft_printf("%s= 0\n", default_form);
}
void print_reduced_form(double *polynom, int max_exponent)
void print_reduced_form(s_polynom *polynom)
{
int i;
bool is_first_term;
if (flag_beautify_mode)
return print_reduced_form_beautify(polynom, max_exponent);
return print_reduced_form_beautify(polynom);
// reduced form
ft_putstr("Reduced form: ");
i = max_exponent;
is_first_term = true;
while (i >= 0)
i = 0;
while (polynom[i].sign != TERM_SIGN_END)
{
// print sign
if (polynom[i] > 0)
// don't output '+' if is first term
if (!(polynom[i].sign == TERM_PLUS && is_first_term))
{
// don't output '+' if is first term
if (!is_first_term)
ft_putchar('+');
}
else
{
ft_putchar('-');
ft_putchar(polynom[i].sign);
}
// print term
@@ -129,40 +124,39 @@ void print_reduced_form(double *polynom, int max_exponent)
{
ft_putchar(' ');
}
printf("%g * x^%i ", ft_fabs(polynom[i]), i);
printf("%g * x^%i ", ft_fabs(polynom[i].coefficient), polynom[i].exponent);
fflush(stdout);
i--;
i++;
is_first_term = false;
}
ft_putstr("= 0\n");
}
void print_degree(double *polynom, int max_exponent)
void print_degree(s_polynom *polynom, int degree)
{
if (max_exponent == 0)
if (degree == 0)
{
if (polynom[0] == 0)
if (polynom[0].coefficient == 0)
{
stop_errors("Any real number is a solution.\n");
}
else if (polynom[0] != 0)
else if (polynom[0].coefficient != 0)
{
stop_errors("No solution.\n");
}
}
else if (max_exponent == 1)
else if (degree == 1)
{
ft_printf("Polynomial degree: %i\n", max_exponent);
ft_printf("Polynomial degree: %i\n", degree);
}
else if (max_exponent == 2)
else if (degree == 2)
{
ft_printf("Polynomial degree: %i\n", max_exponent);
ft_printf("Polynomial degree: %i\n", degree);
}
else
{
ft_printf("Polynomial degree: %i\n", max_exponent);
ft_printf("Polynomial degree: %i\n", degree);
stop_errors("The polynomial degree is strictly greater than 2, I can't solve.\n");
}
}