fix printer reduced form order
This commit is contained in:
2
Makefile
2
Makefile
@@ -108,7 +108,7 @@ run: $(NAME)
|
|||||||
|
|
||||||
test: $(NAME)
|
test: $(NAME)
|
||||||
@echo $(B_PURPLE)"\n---------------------------------------------\nlaunch tests\n"$(RESET)
|
@echo $(B_PURPLE)"\n---------------------------------------------\nlaunch tests\n"$(RESET)
|
||||||
-@bash tester.sh --skip-reduced
|
-@bash tester.sh
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
$(RM_OBJS)
|
$(RM_OBJS)
|
||||||
|
|||||||
@@ -186,12 +186,26 @@ typedef struct
|
|||||||
|
|
||||||
void solve(const s_polynom *polynom, s_solution *solution);
|
void solve(const s_polynom *polynom, s_solution *solution);
|
||||||
|
|
||||||
|
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
|
* PRINTER_EQUATION.C
|
||||||
|
*/
|
||||||
|
|
||||||
|
void print_reduced_form(s_polynom *polynom, int degree);
|
||||||
|
void print_degree(s_polynom *polynom, int max_exponent);
|
||||||
|
|
||||||
|
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
|
* PRINTER_SOLUTIONS.C
|
||||||
|
*/
|
||||||
|
|
||||||
|
void print_solution(s_solution *solution);
|
||||||
|
|
||||||
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
* UTILS/MATH.C
|
* UTILS/MATH.C
|
||||||
*/
|
*/
|
||||||
|
|
||||||
bool is_nearly_equal(double num, int compare);
|
bool is_nearly_equal(double num, int compare);
|
||||||
bool is_nearly_equal_zero(double num);
|
bool is_nearly_equal_zero(double num);
|
||||||
|
const s_polynom *get_term_of_power(const s_polynom *polynom, int power);
|
||||||
bool has_decimal_part(double num);
|
bool has_decimal_part(double num);
|
||||||
bool any_has_decimal_part(double *num, size_t len);
|
bool any_has_decimal_part(double *num, size_t len);
|
||||||
int gcd_int(int a, int b);
|
int gcd_int(int a, int b);
|
||||||
@@ -221,14 +235,6 @@ const char *radicand_sign_to_str(e_radicand_sign enum_value);
|
|||||||
|
|
||||||
void print_debug(const char *description, ...);
|
void print_debug(const char *description, ...);
|
||||||
|
|
||||||
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
||||||
* UTILS/PRINTER.C
|
|
||||||
*/
|
|
||||||
|
|
||||||
void print_reduced_form(s_polynom *polynom);
|
|
||||||
void print_degree(s_polynom *polynom, int max_exponent);
|
|
||||||
void print_solution(s_solution *solution);
|
|
||||||
|
|
||||||
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
* GLOBALS
|
* GLOBALS
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -126,7 +126,7 @@ void launch_computorv1(char *input)
|
|||||||
print_debug("-> degree: %i\n\n", degree);
|
print_debug("-> degree: %i\n\n", degree);
|
||||||
|
|
||||||
// print before solution
|
// print before solution
|
||||||
print_reduced_form(polynom);
|
print_reduced_form(polynom, degree);
|
||||||
print_degree(polynom, degree);
|
print_degree(polynom, degree);
|
||||||
|
|
||||||
// solve
|
// solve
|
||||||
|
|||||||
@@ -82,10 +82,12 @@ static void print_reduced_form_beautify(s_polynom *polynom)
|
|||||||
ft_printf("%s= 0\n", default_form);
|
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;
|
int i;
|
||||||
bool is_first_term;
|
bool is_first_term;
|
||||||
|
const s_polynom *found_term;
|
||||||
|
char sign;
|
||||||
|
|
||||||
if (flag_beautify_mode)
|
if (flag_beautify_mode)
|
||||||
return print_reduced_form_beautify(polynom);
|
return print_reduced_form_beautify(polynom);
|
||||||
@@ -94,25 +96,31 @@ void print_reduced_form(s_polynom *polynom)
|
|||||||
ft_putstr("Reduced form: ");
|
ft_putstr("Reduced form: ");
|
||||||
is_first_term = true;
|
is_first_term = true;
|
||||||
i = 0;
|
i = 0;
|
||||||
while (polynom[i].sign != TERM_SIGN_END)
|
while (i <= degree)
|
||||||
{
|
{
|
||||||
|
found_term = get_term_of_power(polynom, i);
|
||||||
|
|
||||||
// print sign
|
// print sign
|
||||||
// don't output '+' if is first term
|
// 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
|
// print term
|
||||||
if (!is_first_term)
|
if (!is_first_term)
|
||||||
{
|
|
||||||
ft_putchar(' ');
|
ft_putchar(' ');
|
||||||
}
|
printf("%g * x^%i ", ft_fabs(found_term->coefficient), i);
|
||||||
printf("%g * x^%i ", ft_fabs(polynom[i].coefficient), polynom[i].exponent);
|
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
|
|
||||||
i++;
|
|
||||||
is_first_term = false;
|
is_first_term = false;
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
ft_putstr("= 0\n");
|
ft_putstr("= 0\n");
|
||||||
}
|
}
|
||||||
|
|||||||
29
src/solver.c
29
src/solver.c
@@ -2,29 +2,6 @@
|
|||||||
|
|
||||||
#include "computorv1.h"
|
#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)
|
static double positiv_zero(double num)
|
||||||
{
|
{
|
||||||
if (num == 0)
|
if (num == 0)
|
||||||
@@ -120,9 +97,9 @@ void solve(const s_polynom *polynom, s_solution *solution)
|
|||||||
double power1;
|
double power1;
|
||||||
double power0;
|
double power0;
|
||||||
|
|
||||||
power2 = get_coefficient_of_power(2, polynom);
|
power2 = get_term_of_power(polynom, 2)->coefficient;
|
||||||
power1 = get_coefficient_of_power(1, polynom);
|
power1 = get_term_of_power(polynom, 1)->coefficient;
|
||||||
power0 = get_coefficient_of_power(0, polynom);
|
power0 = get_term_of_power(polynom, 0)->coefficient;
|
||||||
|
|
||||||
if (solution->degree == 1)
|
if (solution->degree == 1)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -16,6 +16,21 @@ bool is_nearly_equal_zero(double num)
|
|||||||
return true;
|
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)
|
bool has_decimal_part(double num)
|
||||||
{
|
{
|
||||||
return (!is_nearly_equal(num, (int)num));
|
return (!is_nearly_equal(num, (int)num));
|
||||||
|
|||||||
@@ -356,7 +356,7 @@ Radicant is strictly positive, the two solutions are:
|
|||||||
run_test \
|
run_test \
|
||||||
"29. degree 2 pure" \
|
"29. degree 2 pure" \
|
||||||
"16 * x^2 + 5 * x^1 - 4 * x^0 = 5 * x" "\
|
"16 * x^2 + 5 * x^1 - 4 * x^0 = 5 * x" "\
|
||||||
Reduced form: -4 * x^0 + 0 * x^1 + 3 * x^2 = 0
|
Reduced form: -4 * x^0 + 0 * x^1 + 16 * x^2 = 0
|
||||||
Polynomial degree: 2
|
Polynomial degree: 2
|
||||||
Radicant is strictly positive, the two solutions are:
|
Radicant is strictly positive, the two solutions are:
|
||||||
1/2
|
1/2
|
||||||
@@ -366,7 +366,7 @@ Radicant is strictly positive, the two solutions are:
|
|||||||
run_test \
|
run_test \
|
||||||
"30. degree 2 pure" \
|
"30. degree 2 pure" \
|
||||||
"4 * x^2 + 5 * x^1 - 16 * x^0 = 5 * x" "\
|
"4 * x^2 + 5 * x^1 - 16 * x^0 = 5 * x" "\
|
||||||
Reduced form: -4 * x^0 + 0 * x^1 + 3 * x^2 = 0
|
Reduced form: -16 * x^0 + 0 * x^1 + 4 * x^2 = 0
|
||||||
Polynomial degree: 2
|
Polynomial degree: 2
|
||||||
Radicant is strictly positive, the two solutions are:
|
Radicant is strictly positive, the two solutions are:
|
||||||
2
|
2
|
||||||
|
|||||||
Reference in New Issue
Block a user