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" ./$(NAME) -d "3 * x^2 + 5 * x^1 - 2 * x^0 = 5 * x^1"
@echo $(BLUE)"\n -> run \n"$(RESET) @echo $(BLUE)"\n -> run \n"$(RESET)
./$(NAME) -d "3*x^2 + 2*x = 0" ./$(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: clean:
$(RM_OBJS) $(RM_OBJS)

View File

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

View File

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

View File

@@ -6,11 +6,12 @@
* GLOBALS * GLOBALS
*/ */
bool debug_mode;
char *input_g_err; char *input_g_err;
s_token *tokens_g_err; s_token *tokens_g_err;
s_term *terms_g_err; s_term *terms_g_err;
s_polynom *polynom_g_err; double *polynom_g_err;
bool debug_mode; int polynom_len_g_err;
/** /**
* PROGRAM * 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; int i;
polynom->b = 0.0; int max_exponent;
polynom->c = 0.0;
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) static void launch_argv(char *input)
{ {
int ret; int ret;
int max_exponent;
size_t arg_len; size_t arg_len;
size_t terms_count_prediction; size_t terms_count_prediction;
@@ -123,15 +149,18 @@ static void launch_argv(char *input)
} }
// reduce // reduce
s_polynom polynom[1]; max_exponent = get_max_exponent(terms) + 1;
double polynom[max_exponent];
polynom_g_err = polynom; polynom_g_err = polynom;
polynom_fill_null(polynom); polynom_len_g_err = max_exponent;
polynom_fill_null(polynom, max_exponent);
reduce(terms, polynom); reduce(terms, polynom);
// debug // debug
print_state(); print_state();
} }
// trim spaces and quotes and newlines
static void clean_copy_input(char *input, char *line) static void clean_copy_input(char *input, char *line)
{ {
size_t i; 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; term_position = TERM_LEFT;
while (tokens[i].type != TOKEN_END && terms_count < terms_count_max) 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 // equal
if (tokens[i].type == TOKEN_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; sign = -1;
} }
i += token_count; 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 // coefficient
double ret_coefficient = get_coefficient_absolute(tokens, i, &token_count); double ret_coefficient = get_coefficient_absolute(tokens, i, &token_count);
terms[terms_count].coefficient = ret_coefficient * sign; terms[terms_count].coefficient = ret_coefficient * sign;
i += token_count; 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 // exponent
int ret_exponent = get_exponent(tokens, i, &token_count); int ret_exponent = get_exponent(tokens, i, &token_count);
terms[terms_count].exponent = ret_exponent; terms[terms_count].exponent = ret_exponent;
i += token_count; 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++; terms_count++;
} }

View File

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

View File

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