fix superscript 1

This commit is contained in:
hugogogo
2026-05-07 14:28:58 +02:00
parent f373fef4ac
commit 09701f8884
9 changed files with 75 additions and 23 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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

2
libft

Submodule libft updated: 413e149003...aa35d294f2

View File

@@ -110,7 +110,7 @@ void launch_computorv1(char *input)
// lexerize
arg_len = ft_strlen(input) + 1; // +1 for last END token
print_debug("\n-> tokens[%i]\n", arg_len); // debug
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];

View File

@@ -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;
}

View File

@@ -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

View File

@@ -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)

View File

@@ -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");
}
}