improve printer
This commit is contained in:
8
Makefile
8
Makefile
@@ -44,8 +44,10 @@ SRCS = main.c \
|
|||||||
reducer.c \
|
reducer.c \
|
||||||
solver.c \
|
solver.c \
|
||||||
errors.c \
|
errors.c \
|
||||||
printer.c \
|
printer_equation.c \
|
||||||
print_enums.c
|
printer_solutions.c \
|
||||||
|
print_enums.c \
|
||||||
|
print_debug.c
|
||||||
|
|
||||||
# COMPILATION CONFIG :
|
# COMPILATION CONFIG :
|
||||||
CC = gcc
|
CC = gcc
|
||||||
@@ -149,6 +151,8 @@ run: $(NAME)
|
|||||||
-./$(NAME) $(RUN_FLAGS) "3x^2"
|
-./$(NAME) $(RUN_FLAGS) "3x^2"
|
||||||
@echo $(B_PURPLE)"\n---------------------------------------------\n23. degree 2 \n"$(RESET)
|
@echo $(B_PURPLE)"\n---------------------------------------------\n23. degree 2 \n"$(RESET)
|
||||||
-./$(NAME) $(RUN_FLAGS) "3x^2 = 0"
|
-./$(NAME) $(RUN_FLAGS) "3x^2 = 0"
|
||||||
|
@echo $(B_PURPLE)"\n---------------------------------------------\n23. degree 2 \n"$(RESET)
|
||||||
|
-./$(NAME) $(RUN_FLAGS) "3x^2 + 2 = 0"
|
||||||
clean:
|
clean:
|
||||||
$(RM_OBJS)
|
$(RM_OBJS)
|
||||||
|
|
||||||
|
|||||||
@@ -2,9 +2,10 @@
|
|||||||
|
|
||||||
## todo
|
## 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
|
- doing gcd for int values, and not for double values
|
||||||
- double is nearly_equal_0
|
- double is nearly_equal_0
|
||||||
|
- if no "=" sign return error
|
||||||
|
|
||||||
|
|
||||||
## ressources
|
## ressources
|
||||||
@@ -75,8 +76,6 @@ this project uses submodules (maybe recursively), so either :
|
|||||||
- delta_sqrt; // √|Δ|
|
- delta_sqrt; // √|Δ|
|
||||||
- first_term; // double (-b / 2a)
|
- first_term; // double (-b / 2a)
|
||||||
- second_term; // double (√|Δ| / 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
|
6. print solution
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|||||||
@@ -138,8 +138,6 @@ typedef struct
|
|||||||
double delta_sqrt; // √|Δ|
|
double delta_sqrt; // √|Δ|
|
||||||
double first_term; // double (-b / 2a)
|
double first_term; // double (-b / 2a)
|
||||||
double second_term; // double (√|Δ| / 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;
|
} s_solution_degree_2;
|
||||||
|
|
||||||
typedef struct
|
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);
|
const char *delta_sign_to_str(e_delta_sign sign);
|
||||||
|
|
||||||
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
* UTILS/PRINTER.C
|
* UTILS/PRINT_DEBUG.C
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void print_debug(const char *description, ...);
|
void print_debug(const char *description, ...);
|
||||||
|
|
||||||
|
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
|
* UTILS/PRINTER.C
|
||||||
|
*/
|
||||||
|
|
||||||
void print_reduced_form(s_polynom *polynom);
|
void print_reduced_form(s_polynom *polynom);
|
||||||
void print_degree(s_polynom *polynom, int max_exponent);
|
void print_degree(s_polynom *polynom, int max_exponent);
|
||||||
void print_solution(s_solution *solution);
|
void print_solution(s_solution *solution);
|
||||||
|
|||||||
@@ -2,19 +2,6 @@
|
|||||||
|
|
||||||
#include "computorv1.h"
|
#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"
|
- [x] : ./computorv1 -b "3x² + 0x -0 = x"
|
||||||
Reduced form: 3x² - = 0
|
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");
|
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;
|
delta_sqrt = solution->delta_sqrt;
|
||||||
|
|
||||||
// terms
|
// terms
|
||||||
solution->first_term = positiv_zero(-b / (2 * a)); // -b / 2a
|
if (b)
|
||||||
solution->second_term = positiv_zero(delta_sqrt / 2 * a); // √|Δ| / 2a
|
solution->first_term = positiv_zero(-b / (2 * a)); // -b / 2a
|
||||||
|
if (delta)
|
||||||
// solution
|
solution->second_term = positiv_zero(delta_sqrt / 2 * a); // √|Δ| / 2a
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void solve(const s_polynom *polynom, s_solution *solution)
|
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, "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, "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);
|
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