From 43b4def6ce4b30af57c7a02ec9a97252b4922c94 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Fri, 8 May 2026 01:00:03 +0200 Subject: [PATCH] wip implemented new union for solve --- Makefile | 64 ++++++++++++++++++------------------- README.md | 47 +++++++++++++++++---------- headers/computorv1.h | 26 ++++++++++++--- libft | 2 +- src/launcher.c | 9 +++--- src/reducer.c | 2 +- src/solver.c | 70 +++++++++++++++++++++++++++++------------ src/utils/errors.c | 33 ++++++++++++------- src/utils/print_enums.c | 28 ++++++++--------- 9 files changed, 176 insertions(+), 105 deletions(-) diff --git a/Makefile b/Makefile index d15f485..26a38bc 100644 --- a/Makefile +++ b/Makefile @@ -118,38 +118,38 @@ run: $(NAME) -./$(NAME) -d "3x^2 + 2x -7x^4 = x^4" @echo $(B_PURPLE)"\n---------------------------------------------\n9. run with utf8 \n"$(RESET) -./$(NAME) -d "3x² + 2x -7x¹ = x" - @echo $(B_PURPLE)"\n---------------------------------------------\n10. run \n"$(RESET) - -./$(NAME) "3x² + 2x -7 = x" - @echo $(B_PURPLE)"\n---------------------------------------------\n11. run \n"$(RESET) - -./$(NAME) "-3x² + 2x -7 = x" - @echo $(B_PURPLE)"\n---------------------------------------------\n12. run \n"$(RESET) - -./$(NAME) "+3x² + 2x -7 = x" - @echo $(B_PURPLE)"\n---------------------------------------------\n13. run \n"$(RESET) - -./$(NAME) "3x² + 0x -7 = x" - @echo $(B_PURPLE)"\n---------------------------------------------\n14. run \n"$(RESET) - -./$(NAME) "3x² + 0x -0 = x" - @echo $(B_PURPLE)"\n---------------------------------------------\n15. run \n"$(RESET) - -./$(NAME) "3x² + 2x -0 = x" - @echo $(B_PURPLE)"\n---------------------------------------------\n16. run -b \n"$(RESET) - -./$(NAME) -b "3x² + 2x -7 = x" - @echo $(B_PURPLE)"\n---------------------------------------------\n17. run -b \n"$(RESET) - -./$(NAME) -b "-3x² + 2x -7 = x" - @echo $(B_PURPLE)"\n---------------------------------------------\n18. run -b \n"$(RESET) - -./$(NAME) -b "+3x² + 2x -7 = x" - @echo $(B_PURPLE)"\n---------------------------------------------\n19. run -b \n"$(RESET) - -./$(NAME) -b "3x² + 0x -7 = x" - @echo $(B_PURPLE)"\n---------------------------------------------\n20. run -b \n"$(RESET) - -./$(NAME) -b "3x² + 2x -0 = x" - @echo $(B_PURPLE)"\n---------------------------------------------\n21. run -b \n"$(RESET) - -./$(NAME) -b "3x² + 0x -0 = x" - @echo $(B_PURPLE)"\n---------------------------------------------\n22. run -b \n"$(RESET) - -./$(NAME) -b "3x² + x -0 = x" - @echo $(B_PURPLE)"\n---------------------------------------------\n23. run -b \n"$(RESET) - -./$(NAME) -b "0x² + x -0 = x" - @echo $(B_PURPLE)"\n---------------------------------------------\n24. run -b \n"$(RESET) - -./$(NAME) -b "2x⁵ + x -0 = -7x^5" - @echo $(B_PURPLE)"\n---------------------------------------------\n25. run -b \n"$(RESET) - -./$(NAME) -b "2x + x -0 = -7x" +# @echo $(B_PURPLE)"\n---------------------------------------------\n10. run \n"$(RESET) +# -./$(NAME) "3x² + 2x -7 = x" +# @echo $(B_PURPLE)"\n---------------------------------------------\n11. run \n"$(RESET) +# -./$(NAME) "-3x² + 2x -7 = x" +# @echo $(B_PURPLE)"\n---------------------------------------------\n12. run \n"$(RESET) +# -./$(NAME) "+3x² + 2x -7 = x" +# @echo $(B_PURPLE)"\n---------------------------------------------\n13. run \n"$(RESET) +# -./$(NAME) "3x² + 0x -7 = x" +# @echo $(B_PURPLE)"\n---------------------------------------------\n14. run \n"$(RESET) +# -./$(NAME) "3x² + 0x -0 = x" +# @echo $(B_PURPLE)"\n---------------------------------------------\n15. run \n"$(RESET) +# -./$(NAME) "3x² + 2x -0 = x" +# @echo $(B_PURPLE)"\n---------------------------------------------\n16. run -b \n"$(RESET) +# -./$(NAME) -b "3x² + 2x -7 = x" +# @echo $(B_PURPLE)"\n---------------------------------------------\n17. run -b \n"$(RESET) +# -./$(NAME) -b "-3x² + 2x -7 = x" +# @echo $(B_PURPLE)"\n---------------------------------------------\n18. run -b \n"$(RESET) +# -./$(NAME) -b "+3x² + 2x -7 = x" +# @echo $(B_PURPLE)"\n---------------------------------------------\n19. run -b \n"$(RESET) +# -./$(NAME) -b "3x² + 0x -7 = x" +# @echo $(B_PURPLE)"\n---------------------------------------------\n20. run -b \n"$(RESET) +# -./$(NAME) -b "3x² + 2x -0 = x" +# @echo $(B_PURPLE)"\n---------------------------------------------\n21. run -b \n"$(RESET) +# -./$(NAME) -b "3x² + 0x -0 = x" +# @echo $(B_PURPLE)"\n---------------------------------------------\n22. run -b \n"$(RESET) +# -./$(NAME) -b "3x² + x -0 = x" +# @echo $(B_PURPLE)"\n---------------------------------------------\n23. run -b \n"$(RESET) +# -./$(NAME) -b "0x² + x -0 = x" +# @echo $(B_PURPLE)"\n---------------------------------------------\n24. run -b \n"$(RESET) +# -./$(NAME) -b "2x⁵ + x -0 = -7x^5" +# @echo $(B_PURPLE)"\n---------------------------------------------\n25. run -b \n"$(RESET) +# -./$(NAME) -b "2x + x -0 = -7x" clean: $(RM_OBJS) diff --git a/README.md b/README.md index 1e734dc..c00f452 100644 --- a/README.md +++ b/README.md @@ -48,21 +48,34 @@ this project uses submodules (maybe recursively), so either : -> if degree 3 : - print degree 5. solve - -> discriminant : Δ = b² - 4ac - -> Δ > 0 -> 2 solutions : x = ( -b / 2a ) +- ( √|Δ| / 2a ) - -> Δ == 0 -> : x = ( -b / 2a ) - -> Δ < 0 -> 2 solutions : x = ( -b / 2a ) +- i( √|Δ| / 2a ) - -> solution : - - delta_sign; // + or - - - delta_absolute; // |Δ| - - first_term_gcd; // gcd(b, 2a) - - first_term_numerator; // -b / gcd - - first_term_denominator; // 2a / gcd - - first_term; // double (-b / 2a) - - second_term_gcd; // gcd(√|Δ|, 2a) - - second_term_numerator; // √|Δ| / gcd - - second_term_denominator; // 2a / gcd - - second_term; // double (√|Δ| / 2a) - - double solution1; // first_term + second_term - - double solution2; // first_term - second_term + -> degree 1 : + -> ax + b = 0 <=> ax = -b <=> x = -b / a + -> solution : + - a; // double + - b; // double + - solution_gcd; // gcd(b, a) + - solution_numerator; // -b / gcd + - solution_denominator; // a / gcd + - solution; // double (-b / a) + -> degree 2 : + -> discriminant : Δ = b² - 4ac + -> Δ > 0 -> 2 solutions : x = ( -b / 2a ) +- ( √|Δ| / 2a ) + -> Δ == 0 -> 1 solution : x = ( -b / 2a ) + -> Δ < 0 -> 2 solutions : x = ( -b / 2a ) +- i( √|Δ| / 2a ) + -> solution : + - a; // double + - b; // double + - c; // double + - delta_sign; // + or - + - delta_absolute; // |Δ| + - first_term_gcd; // gcd(b, 2a) + - first_term_numerator; // -b / gcd + - first_term_denominator; // 2a / gcd + - first_term; // double (-b / 2a) + - second_term_gcd; // gcd(√|Δ|, 2a) + - second_term_numerator; // √|Δ| / gcd + - second_term_denominator; // 2a / gcd + - second_term; // double (√|Δ| / 2a) + - double solution1; // first_term + second_term + - double solution2; // first_term - second_term 6. print solution diff --git a/headers/computorv1.h b/headers/computorv1.h index c2651f4..baebff8 100644 --- a/headers/computorv1.h +++ b/headers/computorv1.h @@ -115,11 +115,19 @@ int reduce(s_term *terms, s_polynom *polynom, int max_exponent); typedef enum { - DELTA_PLUS, // + - DELTA_MINUS, // - - DELTA_ZERO, // 0 + DELTA_PLUS = 1, + DELTA_MINUS = -1, + DELTA_ZERO = 0, } e_delta_sign; +typedef struct +{ + int solution_gcd; // gcd(b, a) + int solution_numerator; // -b / gcd + int solution_denominator; // a / gcd + double solution; // double (-b / a) +} s_solution_degree_1; + typedef struct { e_delta_sign delta_sign; // DELTA_PLUS or DELTA_MINUS or DELTA_ZERO @@ -133,7 +141,17 @@ typedef struct int second_term_denominator; // 2a / gcd double second_term; // double (√|Δ| / 2a) double solution1; // first_term + second_term - double solution2; // first_term - second_term + double solution2; // first_term - second_term (not if DELTA_ZERO) +} s_solution_degree_2; + +typedef struct +{ + int degree; + union + { + s_solution_degree_1 solution_degree_1; + s_solution_degree_2 solution_degree_2; + }; } s_solution; void solve(const s_polynom *polynom, s_solution *solution); diff --git a/libft b/libft index 3cf0b6e..8edf428 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 3cf0b6ecb9f40a962c4ad1971e5bf21c9aa16fdd +Subproject commit 8edf428fed1e5e292b4539a0b3e1bfab1c3bd392 diff --git a/src/launcher.c b/src/launcher.c index c29d30a..d5cb108 100644 --- a/src/launcher.c +++ b/src/launcher.c @@ -172,10 +172,11 @@ void launch_computorv1(char *input) print_reduced_form(polynom); print_degree(polynom, degree); - // // solve - // s_solution solution[1]; - // solution_g_err = solution; - // solve(polynom, solution); + // solve + s_solution solution[1]; + solution_g_err = solution; + solution[0].degree = degree; + solve(polynom, solution); // print solution diff --git a/src/reducer.c b/src/reducer.c index b7bbc9c..8b69663 100644 --- a/src/reducer.c +++ b/src/reducer.c @@ -58,7 +58,7 @@ int reduce(s_term *terms, s_polynom *polynom, int max_exponent) // fill polynom polynom[i].coefficient = tmp_coefficient; polynom[i].exponent = max_exponent; - polynom[i].sign = ft_sign_f(tmp_coefficient) == 1 ? TERM_PLUS : TERM_MINUS; + polynom[i].sign = ft_fsign(tmp_coefficient) == 1 ? TERM_PLUS : TERM_MINUS; max_exponent--; i++; diff --git a/src/solver.c b/src/solver.c index 10e4284..f155b88 100644 --- a/src/solver.c +++ b/src/solver.c @@ -69,32 +69,43 @@ static int find_gcd(double numerator, double denominator) } /* - e_delta_sign delta_sign; // DELTA_PLUS or DELTA_MINUS or DELTA_ZERO - double delta_absolute; // |Δ| == |b² - 4ac| - int first_term_gcd; // gcd(b, 2a) - int first_term_numerator; // -b / gcd - int first_term_denominator; // 2a / gcd - double first_term; // double (-b / 2a) - int second_term_gcd; // gcd(√|Δ|, 2a) - int second_term_numerator; // √|Δ| / gcd - int second_term_denominator; // 2a / gcd - double second_term; // double (√|Δ| / 2a) - double solution1; // first_term + second_term - double solution2; // first_term - second_term +typedef struct +{ + double a; // a in "ax + b" + double b; // b in "ax + b" + int solution_gcd; // gcd(b, a) + int solution_numerator; // -b / gcd + int solution_denominator; // a / gcd + double solution; // double (-b / a) +} s_solution_degree_1; +*/ +/* +typedef struct +{ + double a; // a in "ax² + bx + c" + double b; // b in "ax² + bx + c" + double c; // c in "ax² + bx + c" + e_delta_sign delta_sign; // DELTA_PLUS or DELTA_MINUS or DELTA_ZERO + double delta_absolute; // |Δ| == |b² - 4ac| + int first_term_gcd; // gcd(b, 2a) + int first_term_numerator; // -b / gcd + int first_term_denominator; // 2a / gcd + double first_term; // double (-b / 2a) + int second_term_gcd; // gcd(√|Δ|, 2a) + int second_term_numerator; // √|Δ| / gcd + int second_term_denominator; // 2a / gcd + double second_term; // double (√|Δ| / 2a) + double solution1; // first_term + second_term + double solution2; // first_term - second_term (not if DELTA_ZERO) +} s_solution_degree_2; */ -void solve(const s_polynom *polynom, s_solution *solution) +static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, double c) { double delta; - double a; - double b; - double c; - a = polynom[2].coefficient; - b = polynom[1].coefficient; - c = polynom[0].coefficient; delta = b * b - 4 * a * c; - solution->delta_sign = ft_sign_f(delta); + solution->delta_sign = ft_fsign(delta); solution->delta_absolute = ft_fabs(delta); solution->first_term_gcd = find_gcd(b, 2 * a); solution->first_term_numerator = -b / solution->first_term_gcd; @@ -114,3 +125,22 @@ void solve(const s_polynom *polynom, s_solution *solution) // solution->solution2 = 0.0; // } } + +void solve(const s_polynom *polynom, s_solution *solution) +{ + double a; + double b; + double c; + + if (solution->degree == 1) + { + } + else if (solution->degree == 2) + { + a = polynom[0].coefficient; + b = polynom[1].coefficient; + c = polynom[2].coefficient; + + solve_degree_2(&solution->solution_degree_2, a, b, c); + } +} diff --git a/src/utils/errors.c b/src/utils/errors.c index 5c15530..6db650d 100644 --- a/src/utils/errors.c +++ b/src/utils/errors.c @@ -71,18 +71,27 @@ static void print_context_polynom() static void print_context_solution() { - dprintf(STDERR_FILENO, "delta_sign : %25s\n", delta_sign_to_str(solution_g_err->delta_sign)); - dprintf(STDERR_FILENO, "delta_absolute : %25g (|b² - 4ac|)\n", solution_g_err->delta_absolute); - dprintf(STDERR_FILENO, "first_term_gcd : %25i\n", solution_g_err->first_term_gcd); - dprintf(STDERR_FILENO, "first_term_numerator : %25i (-b / gcd)\n", solution_g_err->first_term_numerator); - dprintf(STDERR_FILENO, "first_term_denominator : %25i (2a / gcd)\n", solution_g_err->first_term_denominator); - dprintf(STDERR_FILENO, "first_term : %25g (-b / 2a)\n", solution_g_err->first_term); - // dprintf(STDERR_FILENO, "second_term_gcd : %25g\n", solution_g_err->second_term_gcd); - // dprintf(STDERR_FILENO, "second_term_numerator : %25g\n", solution_g_err->second_term_numerator); - // dprintf(STDERR_FILENO, "second_term_denominator: %25g\n", solution_g_err->second_term_denominator); - // dprintf(STDERR_FILENO, "second_term : %25g\n", solution_g_err->second_term); - // dprintf(STDERR_FILENO, "solution1 : %25g\n", solution_g_err->solution1); - // dprintf(STDERR_FILENO, "solution2 : %25g\n", solution_g_err->solution2); + s_solution_degree_2 solution_2; + + if (solution_g_err->degree == 2) + { + solution_2 = solution_g_err->solution_degree_2; + dprintf(STDERR_FILENO, "degree 2 : delta > 0 ( -b/2a +- √Δ/2a )\n"); + dprintf(STDERR_FILENO, " delta == 0 ( -b/2a )\n"); + dprintf(STDERR_FILENO, " delta < 0 ( -b/2a +- i√|Δ|/2a )\n"); + dprintf(STDERR_FILENO, "delta_sign : %25s\n", delta_sign_to_str(solution_2.delta_sign)); + dprintf(STDERR_FILENO, "delta_absolute : %25g (|b² - 4ac|)\n", solution_2.delta_absolute); + dprintf(STDERR_FILENO, "first_term_gcd : %25i\n", solution_2.first_term_gcd); + dprintf(STDERR_FILENO, "first_term_numerator : %25i (-b / gcd)\n", solution_2.first_term_numerator); + dprintf(STDERR_FILENO, "first_term_denominator : %25i (2a / gcd)\n", solution_2.first_term_denominator); + dprintf(STDERR_FILENO, "first_term : %25g (-b / 2a)\n", solution_2.first_term); + // dprintf(STDERR_FILENO, "second_term_gcd : %25g\n", solution_2.second_term_gcd); + // dprintf(STDERR_FILENO, "second_term_numerator : %25g\n", solution_2.second_term_numerator); + // dprintf(STDERR_FILENO, "second_term_denominator: %25g\n", solution_2.second_term_denominator); + // dprintf(STDERR_FILENO, "second_term : %25g\n", solution_2.second_term); + // dprintf(STDERR_FILENO, "solution1 : %25g\n", solution_2.solution1); + // dprintf(STDERR_FILENO, "solution2 : %25g\n", solution_2.solution2); + } ft_putchar_fd('\n', STDERR_FILENO); } diff --git a/src/utils/print_enums.c b/src/utils/print_enums.c index d567315..624de96 100644 --- a/src/utils/print_enums.c +++ b/src/utils/print_enums.c @@ -49,24 +49,24 @@ const char *term_position_to_str(e_term_position enum_value) const char *term_sign_to_str(e_term_sign enum_value) { - if (enum_value > TERM_SIGN_END || enum_value < TERM_PLUS) + if (enum_value == TERM_PLUS) + return "TERM_PLUS"; + else if (enum_value == TERM_MINUS) + return "TERM_MINUS"; + else if (enum_value == TERM_SIGN_END) + return "TERM_SIGN_END"; + else return "invalid enum value"; - - static const char *const term_sign_str[TERM_SIGN_END + 1] = { - [TERM_PLUS] = "TERM_PLUS", - [TERM_MINUS] = "TERM_MINUS", - [TERM_SIGN_END] = "TERM_SIGN_END"}; - return term_sign_str[enum_value]; } const char *delta_sign_to_str(e_delta_sign enum_value) { - if (enum_value > DELTA_ZERO || enum_value < DELTA_PLUS) + if (enum_value == DELTA_PLUS) + return "DELTA_PLUS"; + else if (enum_value == DELTA_MINUS) + return "DELTA_MINUS"; + else if (enum_value == DELTA_ZERO) + return "DELTA_ZERO"; + else return "invalid enum value"; - - static const char *const delta_sign_str[DELTA_ZERO + 1] = { - [DELTA_PLUS] = "DELTA_PLUS", - [DELTA_MINUS] = "DELTA_MINUS", - [DELTA_ZERO] = "DELTA_ZERO"}; - return delta_sign_str[enum_value]; } \ No newline at end of file