diff --git a/Makefile b/Makefile index da035dc..ec9a0e4 100644 --- a/Makefile +++ b/Makefile @@ -44,8 +44,10 @@ SRCS = main.c \ reducer.c \ solver.c \ errors.c \ - printer.c \ - print_enums.c + printer_equation.c \ + printer_solutions.c \ + print_enums.c \ + print_debug.c # COMPILATION CONFIG : CC = gcc @@ -149,6 +151,8 @@ run: $(NAME) -./$(NAME) $(RUN_FLAGS) "3x^2" @echo $(B_PURPLE)"\n---------------------------------------------\n23. degree 2 \n"$(RESET) -./$(NAME) $(RUN_FLAGS) "3x^2 = 0" + @echo $(B_PURPLE)"\n---------------------------------------------\n23. degree 2 \n"$(RESET) + -./$(NAME) $(RUN_FLAGS) "3x^2 + 2 = 0" clean: $(RM_OBJS) diff --git a/README.md b/README.md index bc5ad56..0e5c36e 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,10 @@ ## todo -- change "compputorv1" to "computor" +- fix error if b is 0 (b takes the value of c instead) - doing gcd for int values, and not for double values - double is nearly_equal_0 +- if no "=" sign return error ## ressources @@ -75,8 +76,6 @@ this project uses submodules (maybe recursively), so either : - delta_sqrt; // √|Δ| - first_term; // double (-b / 2a) - second_term; // double (√|Δ| / 2a) - - double solution1; // first_term + second_term || -b / 2a + √|Δ| * i / 2a - - double solution2; // first_term - second_term || -b / 2a - √|Δ| * i / 2a 6. print solution --- diff --git a/headers/computorv1.h b/headers/computorv1.h index 2e1b035..bcb9766 100644 --- a/headers/computorv1.h +++ b/headers/computorv1.h @@ -138,8 +138,6 @@ typedef struct double delta_sqrt; // √|Δ| double first_term; // double (-b / 2a) double second_term; // double (√|Δ| / 2a) - double solution1; // || first_term + second_term || first_term + i * second_term - double solution2; // (not if DELTA_ZERO) || first_term - second_term || first_term - i * second_term } s_solution_degree_2; typedef struct @@ -172,10 +170,15 @@ const char *term_sign_to_str(e_term_sign sign); const char *delta_sign_to_str(e_delta_sign sign); /** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * UTILS/PRINTER.C + * UTILS/PRINT_DEBUG.C */ 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); diff --git a/src/utils/printer.c b/src/printer_equation.c similarity index 65% rename from src/utils/printer.c rename to src/printer_equation.c index c251d77..555339a 100644 --- a/src/utils/printer.c +++ b/src/printer_equation.c @@ -2,19 +2,6 @@ #include "computorv1.h" -void print_debug(const char *description, ...) -{ - if (!flag_debug_mode) - return; - - // print the formatted description - va_list args; - va_start(args, description); - // ft_vdprintf(STDERR_FILENO, description, args); // it's not handling floats for the moment - vdprintf(STDERR_FILENO, description, args); - va_end(args); -} - /** - [x] : ./computorv1 -b "3x² + 0x -0 = x" Reduced form: 3x² - = 0 @@ -157,45 +144,3 @@ void print_degree(s_polynom *polynom, int degree) stop_errors("The polynomial degree is strictly greater than 2, I can't solve.\n"); } } - -void print_solution(s_solution *solution) -{ - s_solution_degree_1 solution_d1; - s_solution_degree_2 solution_d2; - - if (solution->degree == 1) - { - solution_d1 = solution->solution_degree_1; - ft_printf("The solution is:\n"); - printf("%g\n", solution_d1.solution); - return; - } - else if (solution->degree != 2) - { - stop_errors("The degree is '%i', it should already have been handled.", solution->degree); - } - - // degree 2 - - solution_d2 = solution->solution_degree_2; - if (solution_d2.delta_sign == 0) - { - ft_printf("Discriminant is equal to zero, the solution is:\n"); - printf("%g\n", solution_d2.solution1); - } - else if (solution_d2.delta_sign > 0) - { - ft_printf("Discriminant is strictly positive, the two solutions are:\n"); - printf("%g\n%g\n", solution_d2.solution1, solution_d2.solution2); - } - else if (solution_d2.delta_sign < 0) - { - ft_printf("Discriminant is strictly negative, the two complex solutions are:\n"); - printf("%g/%g + %gi/%g\n", solution_d2.b * -1, solution_d2.a * 2, solution_d2.delta_sqrt, solution_d2.a * 2); - printf("%g/%g - %gi/%g\n", solution_d2.b * -1, solution_d2.a * 2, solution_d2.delta_sqrt, solution_d2.a * 2); - } - else - { - stop_errors("delta sign is wrong : '%i' , delta : '%g'", solution_d2.delta_sign, solution_d2.delta); - } -} \ No newline at end of file diff --git a/src/printer_solutions.c b/src/printer_solutions.c new file mode 100644 index 0000000..6fb925a --- /dev/null +++ b/src/printer_solutions.c @@ -0,0 +1,69 @@ +/* printer.c */ + +#include "computorv1.h" + +static void print_solution_degree_1(s_solution_degree_1 solution) +{ + ft_printf("The solution is:\n"); + printf("%g\n", solution.solution); +} + +static void print_solution_delta_zero(s_solution_degree_2 solution) +{ + ft_printf("Discriminant is equal to zero, the solution is:\n"); + printf("%g\n", solution.first_term); +} + +static void print_solution_delta_positiv(s_solution_degree_2 solution) +{ + ft_printf("Discriminant is strictly positive, the two solutions are:\n"); + printf("%g\n", solution.first_term + solution.second_term); + printf("%g\n", solution.first_term - solution.second_term); +} + +static void print_solution_delta_negativ(s_solution_degree_2 solution) +{ + ft_printf("Discriminant is strictly negative, the two complex solutions are:\n"); + + // solution 1 + if (solution.b) + printf("%g/%g + ", solution.b * -1, solution.a * 2); + printf("%gi/%g\n", solution.delta_sqrt, solution.a * 2); + + // solution 2 + if (solution.b) + printf("%g/%g - ", solution.b * -1, solution.a * 2); + else + printf("-"); + printf("%gi/%g\n", solution.delta_sqrt, solution.a * 2); +} + +void print_solution(s_solution *solution) +{ + s_solution_degree_2 solution_d2; + e_delta_sign delta_sign; + + // degree 1 + + if (solution->degree == 1) + { + return print_solution_degree_1(solution->solution_degree_1); + } + else if (solution->degree == 2) + { + solution_d2 = solution->solution_degree_2; + delta_sign = solution_d2.delta_sign; + if (delta_sign == 0) + print_solution_delta_zero(solution_d2); + else if (delta_sign > 0) + print_solution_delta_positiv(solution_d2); + else if (delta_sign < 0) + print_solution_delta_negativ(solution_d2); + else + stop_errors("delta sign is wrong : '%i' , delta : '%g'", solution_d2.delta_sign, solution_d2.delta); + } + else + { + stop_errors("The degree is '%i', it should already have been handled.", solution->degree); + } +} \ No newline at end of file diff --git a/src/solver.c b/src/solver.c index 9bf8c78..1cc3304 100644 --- a/src/solver.c +++ b/src/solver.c @@ -34,27 +34,10 @@ static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, do delta_sqrt = solution->delta_sqrt; // terms - solution->first_term = positiv_zero(-b / (2 * a)); // -b / 2a - solution->second_term = positiv_zero(delta_sqrt / 2 * a); // √|Δ| / 2a - - // solution - solution->solution1 = solution->first_term; // first_term + second_term - if (solution->delta_sign == DELTA_ZERO) - { - solution->solution1 = positiv_zero(solution->first_term); // -b / 2a - solution->solution2 = NAN; // NAN (no solution) - } - else if (solution->delta_sign == DELTA_PLUS) - { - solution->solution1 = positiv_zero(solution->first_term + solution->second_term); // -b / 2a + √Δ / 2a - solution->solution2 = positiv_zero(solution->first_term - solution->second_term); // -b / 2a - √Δ / 2a - } - else if (solution->delta_sign == DELTA_MINUS) - { - // no real solution, but can output irreal solution with 'i' - solution->solution1 = NAN; - solution->solution2 = NAN; - } + if (b) + solution->first_term = positiv_zero(-b / (2 * a)); // -b / 2a + if (delta) + solution->second_term = positiv_zero(delta_sqrt / 2 * a); // √|Δ| / 2a } void solve(const s_polynom *polynom, s_solution *solution) diff --git a/src/utils/errors.c b/src/utils/errors.c index 64ea62f..ca6f88f 100644 --- a/src/utils/errors.c +++ b/src/utils/errors.c @@ -101,9 +101,6 @@ static void print_context_solution() dprintf(STDERR_FILENO, "first_term : %15g ( -b / 2a )\n", solution_d2.first_term); dprintf(STDERR_FILENO, "second_term : %15g ( √|Δ| / 2a )\n", solution_d2.second_term); - - dprintf(STDERR_FILENO, "solution1 : %15g ( (-b / 2a) + (√Δ / 2a) )\n", solution_d2.solution1); - dprintf(STDERR_FILENO, "solution2 : %15g ( (-b / 2a) - (√Δ / 2a) )\n", solution_d2.solution2); } ft_putchar_fd('\n', STDERR_FILENO); diff --git a/src/utils/print_debug.c b/src/utils/print_debug.c new file mode 100644 index 0000000..7d6dc7f --- /dev/null +++ b/src/utils/print_debug.c @@ -0,0 +1,16 @@ +/* printer.c */ + +#include "computorv1.h" + +void print_debug(const char *description, ...) +{ + if (!flag_debug_mode) + return; + + // print the formatted description + va_list args; + va_start(args, description); + // ft_vdprintf(STDERR_FILENO, description, args); // it's not handling floats for the moment + vdprintf(STDERR_FILENO, description, args); + va_end(args); +}