diff --git a/Makefile b/Makefile index 26a38bc..43f368c 100644 --- a/Makefile +++ b/Makefile @@ -66,6 +66,9 @@ else RM_OBJS = rm -rf $(D_OBJS) endif +# EXECUTION FLAGS +RUN_FLAGS = -b + # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # . target: prerequisites . $@ : target # @@ -100,57 +103,48 @@ $(NAME): $(OBJS) $(CC) $(OBJS) -o $@ $(LFLAGS) run: $(NAME) - @echo $(B_PURPLE)"\n---------------------------------------------\n1. run without flag \n"$(RESET) - -./$(NAME) "3 * x^2 + 5 * x^1 - 2 * x^0 = 5 * x^1" - @echo $(B_PURPLE)"\n---------------------------------------------\n2. run with flag '-d' as last \n"$(RESET) - -./$(NAME) "3.4 * x^2 + 1 * x^1 - 2.0 * x^0 = 5 * x^1" -d - @echo $(B_PURPLE)"\n---------------------------------------------\n3. run with flag '-d' as first \n"$(RESET) - -./$(NAME) -d "3 * x^2 + 2 * x^2 = 5 * x^1" - @echo $(B_PURPLE)"\n---------------------------------------------\n4. run with wrong flag '-e' SHOULD FAIL \n"$(RESET) - -./$(NAME) -d -e "3 * x^2 + 2 * x - 7 * x^4 = 1 * x^4" - @echo $(B_PURPLE)"\n---------------------------------------------\n5. run with free form \n"$(RESET) - -./$(NAME) -d "3*x^2 + 2x = 0" - @echo $(B_PURPLE)"\n---------------------------------------------\n6. run with float coefficient \n"$(RESET) - -./$(NAME) -d "3.4 * x^2 + 1 * x^1 - 2.0 * x^0 = 5.123 * x^1" - @echo $(B_PURPLE)"\n---------------------------------------------\n7. run with float exponent SHOULD FAILS \n"$(RESET) - -./$(NAME) -d "3.4 * x^2 + 1 * x^1 - 2.0 * x^0 = 5 * x^1.2" - @echo $(B_PURPLE)"\n---------------------------------------------\n8. run with power 4 \n"$(RESET) - -./$(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---------------------------------------------\n1. \n"$(RESET) + -./$(NAME) $(RUN_FLAGS) "3 * x^2 + 5 * x^1 - 2 * x^0 = 5 * x^1" + @echo $(B_PURPLE)"\n---------------------------------------------\n2. \n"$(RESET) + -./$(NAME) $(RUN_FLAGS) "3.4 * x^2 + 1 * x^1 - 2.0 * x^0 = 5 * x^1" + @echo $(B_PURPLE)"\n---------------------------------------------\n3. \n"$(RESET) + -./$(NAME) $(RUN_FLAGS) "3 * x^2 + 2 * x^2 = 5 * x^1" + @echo $(B_PURPLE)"\n---------------------------------------------\n4. \n"$(RESET) + -./$(NAME) $(RUN_FLAGS) -e "3 * x^2 + 2 * x - 7 * x^4 = 1 * x^4" + @echo $(B_PURPLE)"\n---------------------------------------------\n5. \n"$(RESET) + -./$(NAME) $(RUN_FLAGS) "3*x^2 + 2x = 0" + @echo $(B_PURPLE)"\n---------------------------------------------\n6. float coefficient \n"$(RESET) + -./$(NAME) $(RUN_FLAGS) "3.4 * x^2 + 1 * x^1 - 2.0 * x^0 = 5.123 * x^1" + @echo $(B_PURPLE)"\n---------------------------------------------\n7. float exponent SHOULD FAILS \n"$(RESET) + -./$(NAME) $(RUN_FLAGS) "3.4 * x^2 + 1 * x^1 - 2.0 * x^0 = 5 * x^1.2" + @echo $(B_PURPLE)"\n---------------------------------------------\n8. power 4 \n"$(RESET) + -./$(NAME) $(RUN_FLAGS) "3x^2 + 2x -7x^4 = x^4" + @echo $(B_PURPLE)"\n---------------------------------------------\n9. utf8 \n"$(RESET) + -./$(NAME) $(RUN_FLAGS) "3x² + 2x -7x¹ = x" + @echo $(B_PURPLE)"\n---------------------------------------------\n10. \n"$(RESET) + -./$(NAME) $(RUN_FLAGS) "3x² + 2x -7 = x" + @echo $(B_PURPLE)"\n---------------------------------------------\n11. \n"$(RESET) + -./$(NAME) $(RUN_FLAGS) "-3x² + 2x -7 = x" + @echo $(B_PURPLE)"\n---------------------------------------------\n12. \n"$(RESET) + -./$(NAME) $(RUN_FLAGS) "+3x² + 2x -7 = x" + @echo $(B_PURPLE)"\n---------------------------------------------\n13. \n"$(RESET) + -./$(NAME) $(RUN_FLAGS) "3x² + 0x -7 = x" + @echo $(B_PURPLE)"\n---------------------------------------------\n14. \n"$(RESET) + -./$(NAME) $(RUN_FLAGS) "3x² + 0x -0 = x" + @echo $(B_PURPLE)"\n---------------------------------------------\n15. \n"$(RESET) + -./$(NAME) $(RUN_FLAGS) "3x² + 2x -0 = x" + @echo $(B_PURPLE)"\n---------------------------------------------\n16. degree 1 \n"$(RESET) + -./$(NAME) $(RUN_FLAGS) "3x + 2x -0 = x" + @echo $(B_PURPLE)"\n---------------------------------------------\n17. \n"$(RESET) + -./$(NAME) $(RUN_FLAGS) "3x² + x -0 = x" + @echo $(B_PURPLE)"\n---------------------------------------------\n18. \n"$(RESET) + -./$(NAME) $(RUN_FLAGS) "0x² + x -0 = x" + @echo $(B_PURPLE)"\n---------------------------------------------\n19. \n"$(RESET) + -./$(NAME) $(RUN_FLAGS) "2x⁵ + x -0 = -7x^5" + @echo $(B_PURPLE)"\n---------------------------------------------\n20. degree 1 \n"$(RESET) + -./$(NAME) $(RUN_FLAGS) "2x + x -0 = -7x" + @echo $(B_PURPLE)"\n---------------------------------------------\n20. degree 1 \n"$(RESET) + -./$(NAME) $(RUN_FLAGS) "2x + x -3 = -7x" clean: $(RM_OBJS) diff --git a/headers/computorv1.h b/headers/computorv1.h index 3c2bf44..8563e46 100644 --- a/headers/computorv1.h +++ b/headers/computorv1.h @@ -189,6 +189,7 @@ const char *delta_sign_to_str(e_delta_sign sign); void print_debug(const char *description, ...); 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/libft b/libft index 60c72d7..45cc208 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 60c72d7560f47ac24074cae1fdb347e364e8d624 +Subproject commit 45cc208f4474d2d880f926f88c5baafb8852d50f diff --git a/src/launcher.c b/src/launcher.c index 69b70fa..e099dfa 100644 --- a/src/launcher.c +++ b/src/launcher.c @@ -137,6 +137,7 @@ void launch_computorv1(char *input) solve(polynom, solution); // print solution + print_solution(solution); // debug print_state(); diff --git a/src/solver.c b/src/solver.c index dba7e5b..0cb1415 100644 --- a/src/solver.c +++ b/src/solver.c @@ -68,36 +68,21 @@ static int find_gcd(double numerator, double denominator) return gcd_int(abs(num_scaled), ft_abs(den_scaled)); } -/* -typedef struct +static void solve_degree_1(s_solution_degree_1 *solution, double a, double 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 -{ - e_delta_sign delta_sign; // DELTA_PLUS or DELTA_MINUS or DELTA_ZERO - double delta_absolute; // |Δ| == |b² - 4ac| - double delta_sqrt; // √|Δ| + double value; - 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) + solution->a = a; + solution->b = b; - 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; - */ + solution->solution_gcd = find_gcd(b, a); // gcd(b, a) + solution->solution_numerator = -b / solution->solution_gcd; // -b / gcd + solution->solution_denominator = a / solution->solution_gcd; // a / gcd + value = -b / a; + if (value == 0) + value = ft_fabs(value); + solution->solution = value; +} static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, double c) { @@ -127,11 +112,6 @@ static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, do solution->second_term = delta_sqrt / 2 * a; // √|Δ| / 2a // solution - /* - degree 2 : delta > 0 ( -b / 2a +- √Δ / 2a ) - delta == 0 ( -b / 2a ) - delta < 0 ( -b / 2a +- i√|Δ| / 2a ) - */ solution->solution1 = solution->first_term; // first_term + second_term if (solution->delta_sign == DELTA_ZERO) { @@ -146,6 +126,8 @@ static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, do else if (solution->delta_sign == DELTA_PLUS) { // no real solution, but can output irreal solution with 'i' + solution->solution1 = NAN; + solution->solution2 = NAN; } } @@ -157,6 +139,10 @@ void solve(const s_polynom *polynom, s_solution *solution) if (solution->degree == 1) { + a = polynom[0].coefficient; + b = polynom[1].coefficient; + + solve_degree_1(&solution->solution_degree_1, a, b); } else if (solution->degree == 2) { diff --git a/src/utils/errors.c b/src/utils/errors.c index fe321e8..36b953b 100644 --- a/src/utils/errors.c +++ b/src/utils/errors.c @@ -71,9 +71,23 @@ static void print_context_polynom() static void print_context_solution() { + s_solution_degree_1 solution_1; s_solution_degree_2 solution_2; - if (solution_g_err->degree == 2) + if (solution_g_err->degree == 1) + { + solution_1 = solution_g_err->solution_degree_1; + dprintf(STDERR_FILENO, "degree 1 : x^1 ( ax + b )\n"); + + dprintf(STDERR_FILENO, "a : %10g ( a )\n", solution_1.a); + dprintf(STDERR_FILENO, "b : %10g ( b )\n", solution_1.b); + + dprintf(STDERR_FILENO, "gcd : %10i ( gcd( b, a ) )\n", solution_1.solution_gcd); + dprintf(STDERR_FILENO, "numerator : %10i ( -b / gcd )\n", solution_1.solution_numerator); + dprintf(STDERR_FILENO, "denominator : %10i ( a / gcd )\n", solution_1.solution_denominator); + dprintf(STDERR_FILENO, "solution : %10g ( -b / a )\n", solution_1.solution); + } + else 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"); diff --git a/src/utils/printer.c b/src/utils/printer.c index 54e7f96..c480167 100644 --- a/src/utils/printer.c +++ b/src/utils/printer.c @@ -156,4 +156,16 @@ void print_degree(s_polynom *polynom, int degree) ft_printf("Polynomial degree: %i\n", degree); stop_errors("The polynomial degree is strictly greater than 2, I can't solve.\n"); } +} + +void print_solution(s_solution *solution) +{ + if (solution->degree == 1) + { + ft_printf("The solution is:\n"); + printf("%g\n", solution->solution_degree_1.solution); + } + else if (solution->degree == 2) + { + } } \ No newline at end of file