wip exo ouput

This commit is contained in:
hugogogo
2026-05-10 20:19:24 +02:00
parent cd6e2327e9
commit 500b6fd932
7 changed files with 93 additions and 85 deletions

View File

@@ -66,6 +66,9 @@ else
RM_OBJS = rm -rf $(D_OBJS) RM_OBJS = rm -rf $(D_OBJS)
endif endif
# EXECUTION FLAGS
RUN_FLAGS = -b
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# . target: prerequisites . $@ : target # # . target: prerequisites . $@ : target #
@@ -100,57 +103,48 @@ $(NAME): $(OBJS)
$(CC) $(OBJS) -o $@ $(LFLAGS) $(CC) $(OBJS) -o $@ $(LFLAGS)
run: $(NAME) run: $(NAME)
@echo $(B_PURPLE)"\n---------------------------------------------\n1. run without flag \n"$(RESET) @echo $(B_PURPLE)"\n---------------------------------------------\n1. \n"$(RESET)
-./$(NAME) "3 * x^2 + 5 * x^1 - 2 * x^0 = 5 * x^1" -./$(NAME) $(RUN_FLAGS) "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) @echo $(B_PURPLE)"\n---------------------------------------------\n2. \n"$(RESET)
-./$(NAME) "3.4 * x^2 + 1 * x^1 - 2.0 * x^0 = 5 * x^1" -d -./$(NAME) $(RUN_FLAGS) "3.4 * x^2 + 1 * x^1 - 2.0 * x^0 = 5 * x^1"
@echo $(B_PURPLE)"\n---------------------------------------------\n3. run with flag '-d' as first \n"$(RESET) @echo $(B_PURPLE)"\n---------------------------------------------\n3. \n"$(RESET)
-./$(NAME) -d "3 * x^2 + 2 * x^2 = 5 * x^1" -./$(NAME) $(RUN_FLAGS) "3 * x^2 + 2 * x^2 = 5 * x^1"
@echo $(B_PURPLE)"\n---------------------------------------------\n4. run with wrong flag '-e' SHOULD FAIL \n"$(RESET) @echo $(B_PURPLE)"\n---------------------------------------------\n4. \n"$(RESET)
-./$(NAME) -d -e "3 * x^2 + 2 * x - 7 * x^4 = 1 * x^4" -./$(NAME) $(RUN_FLAGS) -e "3 * x^2 + 2 * x - 7 * x^4 = 1 * x^4"
@echo $(B_PURPLE)"\n---------------------------------------------\n5. run with free form \n"$(RESET) @echo $(B_PURPLE)"\n---------------------------------------------\n5. \n"$(RESET)
-./$(NAME) -d "3*x^2 + 2x = 0" -./$(NAME) $(RUN_FLAGS) "3*x^2 + 2x = 0"
@echo $(B_PURPLE)"\n---------------------------------------------\n6. run with float coefficient \n"$(RESET) @echo $(B_PURPLE)"\n---------------------------------------------\n6. float coefficient \n"$(RESET)
-./$(NAME) -d "3.4 * x^2 + 1 * x^1 - 2.0 * x^0 = 5.123 * x^1" -./$(NAME) $(RUN_FLAGS) "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) @echo $(B_PURPLE)"\n---------------------------------------------\n7. float exponent SHOULD FAILS \n"$(RESET)
-./$(NAME) -d "3.4 * x^2 + 1 * x^1 - 2.0 * x^0 = 5 * x^1.2" -./$(NAME) $(RUN_FLAGS) "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) @echo $(B_PURPLE)"\n---------------------------------------------\n8. power 4 \n"$(RESET)
-./$(NAME) -d "3x^2 + 2x -7x^4 = x^4" -./$(NAME) $(RUN_FLAGS) "3x^2 + 2x -7x^4 = x^4"
@echo $(B_PURPLE)"\n---------------------------------------------\n9. run with utf8 \n"$(RESET) @echo $(B_PURPLE)"\n---------------------------------------------\n9. utf8 \n"$(RESET)
-./$(NAME) -d "3x² + 2x -7x¹ = x" -./$(NAME) $(RUN_FLAGS) "3x² + 2x -7x¹ = x"
# @echo $(B_PURPLE)"\n---------------------------------------------\n10. run \n"$(RESET) @echo $(B_PURPLE)"\n---------------------------------------------\n10. \n"$(RESET)
# -./$(NAME) "3x² + 2x -7 = x" -./$(NAME) $(RUN_FLAGS) "3x² + 2x -7 = x"
# @echo $(B_PURPLE)"\n---------------------------------------------\n11. run \n"$(RESET) @echo $(B_PURPLE)"\n---------------------------------------------\n11. \n"$(RESET)
# -./$(NAME) "-3x² + 2x -7 = x" -./$(NAME) $(RUN_FLAGS) "-3x² + 2x -7 = x"
# @echo $(B_PURPLE)"\n---------------------------------------------\n12. run \n"$(RESET) @echo $(B_PURPLE)"\n---------------------------------------------\n12. \n"$(RESET)
# -./$(NAME) "+3x² + 2x -7 = x" -./$(NAME) $(RUN_FLAGS) "+3x² + 2x -7 = x"
# @echo $(B_PURPLE)"\n---------------------------------------------\n13. run \n"$(RESET) @echo $(B_PURPLE)"\n---------------------------------------------\n13. \n"$(RESET)
# -./$(NAME) "3x² + 0x -7 = x" -./$(NAME) $(RUN_FLAGS) "3x² + 0x -7 = x"
# @echo $(B_PURPLE)"\n---------------------------------------------\n14. run \n"$(RESET) @echo $(B_PURPLE)"\n---------------------------------------------\n14. \n"$(RESET)
# -./$(NAME) "3x² + 0x -0 = x" -./$(NAME) $(RUN_FLAGS) "3x² + 0x -0 = x"
# @echo $(B_PURPLE)"\n---------------------------------------------\n15. run \n"$(RESET) @echo $(B_PURPLE)"\n---------------------------------------------\n15. \n"$(RESET)
# -./$(NAME) "3x² + 2x -0 = x" -./$(NAME) $(RUN_FLAGS) "3x² + 2x -0 = x"
# @echo $(B_PURPLE)"\n---------------------------------------------\n16. run -b \n"$(RESET) @echo $(B_PURPLE)"\n---------------------------------------------\n16. degree 1 \n"$(RESET)
# -./$(NAME) -b "3x² + 2x -7 = x" -./$(NAME) $(RUN_FLAGS) "3x + 2x -0 = x"
# @echo $(B_PURPLE)"\n---------------------------------------------\n17. run -b \n"$(RESET) @echo $(B_PURPLE)"\n---------------------------------------------\n17. \n"$(RESET)
# -./$(NAME) -b "-3x² + 2x -7 = x" -./$(NAME) $(RUN_FLAGS) "3x² + x -0 = x"
# @echo $(B_PURPLE)"\n---------------------------------------------\n18. run -b \n"$(RESET) @echo $(B_PURPLE)"\n---------------------------------------------\n18. \n"$(RESET)
# -./$(NAME) -b "+3x² + 2x -7 = x" -./$(NAME) $(RUN_FLAGS) "0x² + x -0 = x"
# @echo $(B_PURPLE)"\n---------------------------------------------\n19. run -b \n"$(RESET) @echo $(B_PURPLE)"\n---------------------------------------------\n19. \n"$(RESET)
# -./$(NAME) -b "3x² + 0x -7 = x" -./$(NAME) $(RUN_FLAGS) "2x⁵ + x -0 = -7x^5"
# @echo $(B_PURPLE)"\n---------------------------------------------\n20. run -b \n"$(RESET) @echo $(B_PURPLE)"\n---------------------------------------------\n20. degree 1 \n"$(RESET)
# -./$(NAME) -b "3x² + 2x -0 = x" -./$(NAME) $(RUN_FLAGS) "2x + x -0 = -7x"
# @echo $(B_PURPLE)"\n---------------------------------------------\n21. run -b \n"$(RESET) @echo $(B_PURPLE)"\n---------------------------------------------\n20. degree 1 \n"$(RESET)
# -./$(NAME) -b "3x² + 0x -0 = x" -./$(NAME) $(RUN_FLAGS) "2x + x -3 = -7x"
# @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: clean:
$(RM_OBJS) $(RM_OBJS)

View File

@@ -189,6 +189,7 @@ const char *delta_sign_to_str(e_delta_sign sign);
void print_debug(const char *description, ...); void print_debug(const char *description, ...);
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);
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* GLOBALS * GLOBALS

2
libft

Submodule libft updated: 60c72d7560...45cc208f44

View File

@@ -137,6 +137,7 @@ void launch_computorv1(char *input)
solve(polynom, solution); solve(polynom, solution);
// print solution // print solution
print_solution(solution);
// debug // debug
print_state(); print_state();

View File

@@ -68,36 +68,21 @@ static int find_gcd(double numerator, double denominator)
return gcd_int(abs(num_scaled), ft_abs(den_scaled)); return gcd_int(abs(num_scaled), ft_abs(den_scaled));
} }
/* static void solve_degree_1(s_solution_degree_1 *solution, double a, double b)
typedef struct
{ {
int solution_gcd; // gcd(b, a) double value;
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; // √|Δ|
int first_term_gcd; // gcd(b, 2a) solution->a = a;
int first_term_numerator; // -b / gcd solution->b = b;
int first_term_denominator; // 2a / gcd
double first_term; // double (-b / 2a)
int second_term_gcd; // gcd(√|Δ|, 2a) solution->solution_gcd = find_gcd(b, a); // gcd(b, a)
int second_term_numerator; // √|Δ| / gcd solution->solution_numerator = -b / solution->solution_gcd; // -b / gcd
int second_term_denominator; // 2a / gcd solution->solution_denominator = a / solution->solution_gcd; // a / gcd
double second_term; // double (√|Δ| / 2a) value = -b / a;
if (value == 0)
double solution1; // first_term + second_term value = ft_fabs(value);
double solution2; // first_term - second_term (not if DELTA_ZERO) solution->solution = value;
} s_solution_degree_2; }
*/
static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, double c) 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->second_term = delta_sqrt / 2 * a; // √|Δ| / 2a
// solution // 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 solution->solution1 = solution->first_term; // first_term + second_term
if (solution->delta_sign == DELTA_ZERO) 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) else if (solution->delta_sign == DELTA_PLUS)
{ {
// no real solution, but can output irreal solution with 'i' // 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) 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) else if (solution->degree == 2)
{ {

View File

@@ -71,9 +71,23 @@ static void print_context_polynom()
static void print_context_solution() static void print_context_solution()
{ {
s_solution_degree_1 solution_1;
s_solution_degree_2 solution_2; 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; solution_2 = solution_g_err->solution_degree_2;
dprintf(STDERR_FILENO, "degree 2 : delta > 0 ( -b / 2a +- √Δ / 2a )\n"); dprintf(STDERR_FILENO, "degree 2 : delta > 0 ( -b / 2a +- √Δ / 2a )\n");

View File

@@ -156,4 +156,16 @@ void print_degree(s_polynom *polynom, int degree)
ft_printf("Polynomial degree: %i\n", degree); ft_printf("Polynomial degree: %i\n", 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)
{
if (solution->degree == 1)
{
ft_printf("The solution is:\n");
printf("%g\n", solution->solution_degree_1.solution);
}
else if (solution->degree == 2)
{
}
} }