diff --git a/Makefile b/Makefile index 92d8a25..73861e6 100644 --- a/Makefile +++ b/Makefile @@ -108,7 +108,7 @@ run: $(NAME) test: $(NAME) @echo $(B_PURPLE)"\n---------------------------------------------\nlaunch tests\n"$(RESET) - -@bash tester.sh --skip-reduced + -@bash tester.sh clean: $(RM_OBJS) diff --git a/headers/computorv1.h b/headers/computorv1.h index ab93229..5034f3e 100644 --- a/headers/computorv1.h +++ b/headers/computorv1.h @@ -186,12 +186,26 @@ typedef struct 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 */ bool is_nearly_equal(double num, int compare); 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 any_has_decimal_part(double *num, size_t len); 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, ...); -/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * 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 */ diff --git a/src/launcher.c b/src/launcher.c index e099dfa..6f946db 100644 --- a/src/launcher.c +++ b/src/launcher.c @@ -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 diff --git a/src/printer_equation.c b/src/printer_equation.c index b21609e..6b9cce5 100644 --- a/src/printer_equation.c +++ b/src/printer_equation.c @@ -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"); } diff --git a/src/solver.c b/src/solver.c index f70337f..5953dc5 100644 --- a/src/solver.c +++ b/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) { diff --git a/src/utils/math.c b/src/utils/math.c index 56476b7..9d5debd 100644 --- a/src/utils/math.c +++ b/src/utils/math.c @@ -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)); diff --git a/tester.sh b/tester.sh index 087210e..2fce261 100644 --- a/tester.sh +++ b/tester.sh @@ -356,7 +356,7 @@ Radicant is strictly positive, the two solutions are: run_test \ "29. degree 2 pure" \ "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 Radicant is strictly positive, the two solutions are: 1/2 @@ -366,7 +366,7 @@ Radicant is strictly positive, the two solutions are: run_test \ "30. degree 2 pure" \ "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 Radicant is strictly positive, the two solutions are: 2