From 09701f8884d5d93f0f773431dd775f8c147ecf6e Mon Sep 17 00:00:00 2001 From: hugogogo Date: Thu, 7 May 2026 14:28:58 +0200 Subject: [PATCH] fix superscript 1 --- Makefile | 4 +++- README.md | 14 ++++++++++---- headers/computorv1.h | 6 ++++-- libft | 2 +- src/launcher.c | 11 ++++++----- src/lexer.c | 4 ++-- src/main.c | 23 +++++++++++++++++++++++ src/utils/errors.c | 1 + src/utils/printer.c | 33 +++++++++++++++++++++++++-------- 9 files changed, 75 insertions(+), 23 deletions(-) diff --git a/Makefile b/Makefile index 130d8ba..2d7713e 100644 --- a/Makefile +++ b/Makefile @@ -118,8 +118,10 @@ run: $(NAME) -./$(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---------------------------------------------\n9. run normal \n"$(RESET) + @echo $(B_PURPLE)"\n---------------------------------------------\n10. run normal \n"$(RESET) -./$(NAME) -d "3x² + 2x -7 = x" + @echo $(B_PURPLE)"\n---------------------------------------------\n11. run \n"$(RESET) + -./$(NAME) "3x² + 2x -7 = x" clean: $(RM_OBJS) diff --git a/README.md b/README.md index 276628e..f595256 100644 --- a/README.md +++ b/README.md @@ -41,9 +41,15 @@ this project uses submodules (maybe recursively), so either : - 3 - ... 4. print reduced form -5. find degree -6. print degree -7. solve + -> print reduced form + -> if degree 1 : + - if c = 0 -> print "any real is a solution" + - if c != 0 -> print "no solution" + -> if degree 2 : + - print degree + -> if degree 3 : + - print degree +5. solve -> discriminant : Δ = b² - 4ac -> Δ > 0 -> 2 solutions : x = ( -b / 2a ) +- ( √|Δ| / 2a ) -> Δ == 0 -> : x = ( -b / 2a ) @@ -61,4 +67,4 @@ this project uses submodules (maybe recursively), so either : - second_term; // double (√|Δ| / 2a) - double solution1; // first_term + second_term - double solution2; // first_term - second_term -8. print solution +6. print solution diff --git a/headers/computorv1.h b/headers/computorv1.h index eb2666e..c727933 100644 --- a/headers/computorv1.h +++ b/headers/computorv1.h @@ -155,7 +155,8 @@ const char *delta_sign_to_str(e_delta_sign sign); */ void print_debug(const char *description, ...); -void print_before_solution(double *polynom, int max_exponent); +void print_reduced_form(double *polynom, int max_exponent); +void print_degree(double *polynom, int max_exponent); /** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * GLOBALS @@ -166,8 +167,9 @@ extern s_token *tokens_g_err; extern s_term *terms_g_err; extern double *polynom_g_err; extern int polynom_len_g_err; +extern s_solution *solution_g_err; extern bool flag_debug_mode; extern bool flag_loop_mode; -extern s_solution *solution_g_err; +extern bool flag_beautify_mode; #endif \ No newline at end of file diff --git a/libft b/libft index 413e149..aa35d29 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 413e1490036a59d58a1072275ddaaf3421abbbe0 +Subproject commit aa35d294f2ff1bb28cc6c20e89ed2e0deedf11e1 diff --git a/src/launcher.c b/src/launcher.c index d899e6c..9a7dccb 100644 --- a/src/launcher.c +++ b/src/launcher.c @@ -109,8 +109,8 @@ void launch_computorv1(char *input) remove_spaces(input); // lexerize - arg_len = ft_strlen(input) + 1; // +1 for last END token - print_debug("\n-> tokens[%i]\n", arg_len); // debug + arg_len = ft_strlen(input) + 1; // +1 for last END token + print_debug("\n-> tokens[%i]\n", arg_len); s_token tokens[arg_len]; tokens_g_err = tokens; tokens_fill_null(tokens, arg_len); @@ -118,7 +118,7 @@ void launch_computorv1(char *input) // parse terms_count_prediction = count_any_of(input, "-+=") + 2; // +1 for first term that can have no leading '+', +1 for last term == NULL - print_debug("-> terms[%i]\n\n", terms_count_prediction); // debug + print_debug("-> terms[%i]\n\n", terms_count_prediction); s_term terms[terms_count_prediction]; terms_g_err = terms; terms_fill_null(terms, terms_count_prediction); @@ -126,7 +126,7 @@ void launch_computorv1(char *input) // reduce max_exponent = get_max_exponent(terms); - print_debug("-> max_exponent: %i\n\n", max_exponent); // debug + print_debug("-> max_exponent: %i\n\n", max_exponent); double polynom[max_exponent + 1]; polynom_g_err = polynom; polynom_len_g_err = max_exponent; @@ -134,7 +134,8 @@ void launch_computorv1(char *input) reduce(terms, polynom); // print before solution - print_before_solution(polynom, max_exponent); + print_reduced_form(polynom, max_exponent); + print_degree(polynom, max_exponent); // solve s_solution solution[1]; diff --git a/src/lexer.c b/src/lexer.c index f3ddd67..73ea0ba 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -294,13 +294,13 @@ void lexerize(const char *input, s_token *tokens) } else { - stop_errors(&input[input_pos]); + stop_errors("input[input_pos] is not any token: %s\n", &input[input_pos]); } tokens_count++; if (token_size == 0) { - stop_errors(&input[input_pos]); + stop_errors("token_size is 0 : %s\n", &input[input_pos]); } input_pos += token_size; } diff --git a/src/main.c b/src/main.c index 82f9f3a..fb74b2c 100644 --- a/src/main.c +++ b/src/main.c @@ -8,6 +8,7 @@ bool flag_debug_mode; bool flag_loop_mode; +bool flag_beautify_mode; char *input_g_err; s_token *tokens_g_err; s_term *terms_g_err; @@ -19,6 +20,19 @@ s_solution *solution_g_err; * PROGRAM */ +static void print_usage() +{ + ft_printf("USAGE :\n\n"); + ft_printf("| ./computorv1 [flags] [polynom]\n"); + ft_printf("| ./computorv1 [polynom] [flags]\n"); + ft_printf("| ./computorv1 [flags] -> interactiv mode\n\n"); + ft_printf("[flags] :\n"); + ft_printf("-d : print debug\n"); + ft_printf("-l : interactive loop\n"); + ft_printf("-b : beautify output\n"); + ft_putchar('\n'); +} + // trim spaces and quotes and newlines static void clean_copy_input(char *input, char *line) { @@ -122,15 +136,24 @@ int main(int ac, char **av) { if ((ft_strcmp(av[i], "-d") == 0)) { + // flag -d : debug flag_debug_mode = true; } else if ((ft_strcmp(av[i], "-l") == 0)) { + // flag -l : interactiv loop flag_loop_mode = true; program_mode = MODE_LOOP; } + else if ((ft_strcmp(av[i], "-l") == 0)) + { + // flag -b : beautify output + flag_beautify_mode = true; + } else if (ft_strlen(av[i]) == 2 && av[i][0] == '-') { + // unknown flag + print_usage(); stop_errors("unknwon flag '%s'", av[i]); } else diff --git a/src/utils/errors.c b/src/utils/errors.c index d836e4f..12534cb 100644 --- a/src/utils/errors.c +++ b/src/utils/errors.c @@ -123,6 +123,7 @@ void stop_errors(const char *description, ...) va_start(args, description); ft_vdprintf(STDERR_FILENO, description, args); va_end(args); + ft_putchar_fd('\n', STDERR_FILENO); // print the base message if (flag_debug_mode) diff --git a/src/utils/printer.c b/src/utils/printer.c index a367ab2..93e1d57 100644 --- a/src/utils/printer.c +++ b/src/utils/printer.c @@ -15,7 +15,7 @@ void print_debug(const char *description, ...) va_end(args); } -void print_before_solution(double *polynom, int max_exponent) +void print_reduced_form(double *polynom, int max_exponent) { int i; @@ -40,16 +40,33 @@ void print_before_solution(double *polynom, int max_exponent) i--; } ft_putstr("= 0\n"); +} - // // check errors - // ft_printf(": "); - // ft_printf(": "); - // ft_printf(": "); +void print_degree(double *polynom, int max_exponent) +{ - // degree - ft_printf("Polynomial degree: %i\n", max_exponent); - if (max_exponent > 2) + if (max_exponent == 0) { + if (polynom[0] == 0) + { + stop_errors("Any real number is a solution.\n"); + } + else if (polynom[0] != 0) + { + stop_errors("No solution.\n"); + } + } + else if (max_exponent == 1) + { + ft_printf("Polynomial degree: %i\n", max_exponent); + } + else if (max_exponent == 2) + { + ft_printf("Polynomial degree: %i\n", max_exponent); + } + else + { + ft_printf("Polynomial degree: %i\n", max_exponent); stop_errors("The polynomial degree is strictly greater than 2, I can't solve.\n"); } } \ No newline at end of file