fix printer reduced form order
This commit is contained in:
@@ -126,7 +126,7 @@ void launch_computorv1(char *input)
|
||||
print_debug("-> degree: %i\n\n", degree);
|
||||
|
||||
// print before solution
|
||||
print_reduced_form(polynom);
|
||||
print_reduced_form(polynom, degree);
|
||||
print_degree(polynom, degree);
|
||||
|
||||
// solve
|
||||
|
||||
@@ -82,10 +82,12 @@ static void print_reduced_form_beautify(s_polynom *polynom)
|
||||
ft_printf("%s= 0\n", default_form);
|
||||
}
|
||||
|
||||
void print_reduced_form(s_polynom *polynom)
|
||||
void print_reduced_form(s_polynom *polynom, int degree)
|
||||
{
|
||||
int i;
|
||||
bool is_first_term;
|
||||
const s_polynom *found_term;
|
||||
char sign;
|
||||
|
||||
if (flag_beautify_mode)
|
||||
return print_reduced_form_beautify(polynom);
|
||||
@@ -94,25 +96,31 @@ void print_reduced_form(s_polynom *polynom)
|
||||
ft_putstr("Reduced form: ");
|
||||
is_first_term = true;
|
||||
i = 0;
|
||||
while (polynom[i].sign != TERM_SIGN_END)
|
||||
while (i <= degree)
|
||||
{
|
||||
found_term = get_term_of_power(polynom, i);
|
||||
|
||||
// print sign
|
||||
// don't output '+' if is first term
|
||||
if (!(polynom[i].sign == TERM_PLUS && is_first_term))
|
||||
sign = found_term->sign == TERM_SIGN_END ? TERM_PLUS : found_term->sign;
|
||||
if (is_first_term)
|
||||
{
|
||||
ft_putchar(polynom[i].sign);
|
||||
if (sign == TERM_MINUS)
|
||||
ft_putchar(sign);
|
||||
}
|
||||
else
|
||||
{
|
||||
ft_putchar(sign);
|
||||
}
|
||||
|
||||
// print term
|
||||
if (!is_first_term)
|
||||
{
|
||||
ft_putchar(' ');
|
||||
}
|
||||
printf("%g * x^%i ", ft_fabs(polynom[i].coefficient), polynom[i].exponent);
|
||||
printf("%g * x^%i ", ft_fabs(found_term->coefficient), i);
|
||||
fflush(stdout);
|
||||
|
||||
i++;
|
||||
is_first_term = false;
|
||||
i++;
|
||||
}
|
||||
ft_putstr("= 0\n");
|
||||
}
|
||||
|
||||
29
src/solver.c
29
src/solver.c
@@ -2,29 +2,6 @@
|
||||
|
||||
#include "computorv1.h"
|
||||
|
||||
static double get_coefficient_of_power(int power, const s_polynom *polynom)
|
||||
{
|
||||
int i;
|
||||
double coefficient;
|
||||
|
||||
// if no term found with exponent, default coefficient is 0
|
||||
coefficient = 0;
|
||||
|
||||
i = 0;
|
||||
while (polynom[i].sign != TERM_SIGN_END)
|
||||
{
|
||||
if (polynom[i].exponent == power)
|
||||
{
|
||||
coefficient = polynom[i].coefficient;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
print_debug("- coefficient of power '%i' : %g (polynom[%i])\n", power, coefficient, i);
|
||||
return coefficient;
|
||||
}
|
||||
|
||||
static double positiv_zero(double num)
|
||||
{
|
||||
if (num == 0)
|
||||
@@ -120,9 +97,9 @@ void solve(const s_polynom *polynom, s_solution *solution)
|
||||
double power1;
|
||||
double power0;
|
||||
|
||||
power2 = get_coefficient_of_power(2, polynom);
|
||||
power1 = get_coefficient_of_power(1, polynom);
|
||||
power0 = get_coefficient_of_power(0, polynom);
|
||||
power2 = get_term_of_power(polynom, 2)->coefficient;
|
||||
power1 = get_term_of_power(polynom, 1)->coefficient;
|
||||
power0 = get_term_of_power(polynom, 0)->coefficient;
|
||||
|
||||
if (solution->degree == 1)
|
||||
{
|
||||
|
||||
@@ -16,6 +16,21 @@ bool is_nearly_equal_zero(double num)
|
||||
return true;
|
||||
}
|
||||
|
||||
const s_polynom *get_term_of_power(const s_polynom *polynom, int power)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (polynom[i].sign != TERM_SIGN_END)
|
||||
{
|
||||
if (polynom[i].exponent == power)
|
||||
break;
|
||||
i++;
|
||||
}
|
||||
|
||||
return &polynom[i];
|
||||
}
|
||||
|
||||
bool has_decimal_part(double num)
|
||||
{
|
||||
return (!is_nearly_equal(num, (int)num));
|
||||
|
||||
Reference in New Issue
Block a user