fix polynomial to accept any exponents

This commit is contained in:
hugogogo
2026-05-04 22:14:12 +02:00
parent bc48f52c2c
commit b178951936
7 changed files with 88 additions and 61 deletions

View File

@@ -102,6 +102,10 @@ run: $(NAME)
./$(NAME) -d "3 * x^2 + 5 * x^1 - 2 * x^0 = 5 * x^1"
@echo $(BLUE)"\n -> run \n"$(RESET)
./$(NAME) -d "3*x^2 + 2*x = 0"
@echo $(BLUE)"\n -> run \n"$(RESET)
./$(NAME) -d "3*x^2 + 2*x -7x^3 = x^3"
@echo $(BLUE)"\n -> run \n"$(RESET)
./$(NAME) -d "3*x^2 + 2*x -7x^4 = x^4"
clean:
$(RM_OBJS)

View File

@@ -16,31 +16,30 @@ this project uses submodules (maybe recursively), so either :
## steps
1. lexer
-> tokens[] :
{
PLUS -> +
MINUS -> -
VARIABLE -> x
NUMBER -> int or double
POWER -> ^
MULTIPLICATION -> *
DIVISION -> /
END -> null
}[]
-> tokens types :
- TOKEN_VARIABLE // x, y, etc.
- TOKEN_NUMBER_INT // int
- TOKEN_NUMBER_DOUBLE // double
- TOKEN_POWER // ^ or **
- TOKEN_SIGN_PLUS // +
- TOKEN_SIGN_MINUS // -
- TOKEN_FACTOR_MULT // *
- TOKEN_FACTOR_DIV // / or :
- TOKEN_EQUAL // =
- END // null
2. parser
-> terms[] :
{
SIGN -> + or -
COEFFICIENT -> double
EXPONENT -> double
}[]
-> terms :
- POSITION // left or righ from =
- SIGN // + or -
- COEFFICIENT // double
- EXPONENT // double
3. reduce
-> polynom :
{
a -> double
b -> double
c -> double
}[]
-> polynom :
- 0
- 1
- 2
- 3
- ...
4. print reduced form
5. find degree
6. print degree

View File

@@ -90,14 +90,7 @@ int parse(s_token *tokens, s_term *terms, int terms_count_max);
* REDUCE.C
*/
typedef struct
{
double a;
double b;
double c;
} s_polynom;
void reduce(s_term *terms, s_polynom *polynom);
void reduce(s_term *terms, double *polynom);
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* UTILS/ERRORS.C
@@ -136,7 +129,8 @@ const char *term_sign_to_str(e_term_sign sign);
extern char *input_g_err;
extern s_token *tokens_g_err;
extern s_term *terms_g_err;
extern s_polynom *polynom_g_err;
extern double *polynom_g_err;
extern int polynom_len_g_err;
extern bool debug_mode;
#endif

View File

@@ -6,11 +6,12 @@
* GLOBALS
*/
bool debug_mode;
char *input_g_err;
s_token *tokens_g_err;
s_term *terms_g_err;
s_polynom *polynom_g_err;
bool debug_mode;
double *polynom_g_err;
int polynom_len_g_err;
/**
* PROGRAM
@@ -81,16 +82,41 @@ static void terms_fill_null(s_term *terms, size_t terms_count_prediction)
}
}
static void polynom_fill_null(s_polynom *polynom)
static int get_max_exponent(s_term *terms)
{
polynom->a = 0.0;
polynom->b = 0.0;
polynom->c = 0.0;
int i;
int max_exponent;
i = 0;
max_exponent = 0;
while (terms[i].position != TERM_END)
{
if (terms[i].exponent > max_exponent)
{
max_exponent = terms[i].exponent;
}
i++;
}
return max_exponent;
}
static void polynom_fill_null(double *polynom, int len)
{
int i;
i = 0;
while (i < len)
{
polynom[i] = 0.0;
i++;
}
}
static void launch_argv(char *input)
{
int ret;
int max_exponent;
size_t arg_len;
size_t terms_count_prediction;
@@ -123,15 +149,18 @@ static void launch_argv(char *input)
}
// reduce
s_polynom polynom[1];
max_exponent = get_max_exponent(terms) + 1;
double polynom[max_exponent];
polynom_g_err = polynom;
polynom_fill_null(polynom);
polynom_len_g_err = max_exponent;
polynom_fill_null(polynom, max_exponent);
reduce(terms, polynom);
// debug
print_state();
}
// trim spaces and quotes and newlines
static void clean_copy_input(char *input, char *line)
{
size_t i;

View File

@@ -207,7 +207,7 @@ int parse(s_token *tokens, s_term *terms, int terms_count_max)
term_position = TERM_LEFT;
while (tokens[i].type != TOKEN_END && terms_count < terms_count_max)
{
ft_printf("- token[%i]\n", i); // debug
// ft_printf("- token[%i]\n", i); // debug
// equal
if (tokens[i].type == TOKEN_EQUAL)
@@ -229,19 +229,19 @@ int parse(s_token *tokens, s_term *terms, int terms_count_max)
sign = -1;
}
i += token_count;
ft_printf("term[%i] get_sign: (%i)[%s], token_count: [%d]\n", terms_count, ret_sign, term_sign_to_str(ret_sign), token_count); // debug
// ft_printf("term[%i] get_sign: (%i)[%s], token_count: [%d]\n", terms_count, ret_sign, term_sign_to_str(ret_sign), token_count); // debug
// coefficient
double ret_coefficient = get_coefficient_absolute(tokens, i, &token_count);
terms[terms_count].coefficient = ret_coefficient * sign;
i += token_count;
printf("term[%i] get_coefficient: [%g], token_count: [%d]\n", terms_count, ret_coefficient, token_count); // debug
// printf("term[%i] get_coefficient: [%g], token_count: [%d]\n", terms_count, ret_coefficient, token_count); // debug
// exponent
int ret_exponent = get_exponent(tokens, i, &token_count);
terms[terms_count].exponent = ret_exponent;
i += token_count;
ft_printf("term[%i] get_exponent: [%i], token_count: [%d]\n", terms_count, ret_exponent, token_count); // debug
// ft_printf("term[%i] get_exponent: [%i], token_count: [%d]\n", terms_count, ret_exponent, token_count); // debug
terms_count++;
}

View File

@@ -2,31 +2,26 @@
#include "computorv1.h"
void reduce(s_term *terms, s_polynom *polynom)
void reduce(s_term *terms, double *polynom)
{
int i;
int exponent;
double tmp;
i = 0;
while (terms[i].position != TERM_END)
{
// get coefficient with left sign
tmp = terms[i].coefficient;
if (terms[i].position == TERM_RIGHT)
{
tmp *= -1;
}
if (terms[i].exponent == 2)
{
polynom->a += tmp;
}
if (terms[i].exponent == 1)
{
polynom->b += tmp;
}
if (terms[i].exponent == 0)
{
polynom->c += tmp;
}
// add coefficient to exponent
exponent = terms[i].exponent;
polynom[exponent] += tmp;
i++;
}
}

View File

@@ -61,7 +61,6 @@ static void print_context_terms()
{
int i;
ft_putchar_fd('\n', STDERR_FILENO);
i = 0;
while (terms_g_err[i].position != TERM_END)
{
@@ -97,9 +96,16 @@ static void print_context_terms()
static void print_context_polynom()
{
dprintf(STDERR_FILENO, "\npolynom[a]: %10g\n", polynom_g_err->a);
dprintf(STDERR_FILENO, "polynom[b]: %10g\n", polynom_g_err->b);
dprintf(STDERR_FILENO, "polynom[c]: %10g\n", polynom_g_err->c);
int i;
ft_putnbr_fd(polynom_len_g_err, STDERR_FILENO);
ft_putchar_fd('\n', STDERR_FILENO);
i = 0;
while (i < polynom_len_g_err)
{
dprintf(STDERR_FILENO, "polynom[%i]: %10g\n", i, polynom_g_err[i]);
i++;
}
}
void print_state()