improve printer
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
69
src/printer_solutions.c
Normal file
69
src/printer_solutions.c
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
25
src/solver.c
25
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)
|
||||
|
||||
@@ -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);
|
||||
|
||||
16
src/utils/print_debug.c
Normal file
16
src/utils/print_debug.c
Normal file
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user