diff --git a/Makefile b/Makefile index bfbcf14..835ca44 100644 --- a/Makefile +++ b/Makefile @@ -40,6 +40,7 @@ D_SRCS = ./src \ SRCS = computorv1.c \ lexer.c \ parser.c \ + reduce.c \ errors.c \ print_enums.c diff --git a/headers/computorv1.h b/headers/computorv1.h index 80d22bf..7583197 100644 --- a/headers/computorv1.h +++ b/headers/computorv1.h @@ -125,6 +125,6 @@ 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_term *polynom_g_err; +extern s_polynom *polynom_g_err; #endif \ No newline at end of file diff --git a/src/computorv1.c b/src/computorv1.c index e15fa87..239fb42 100644 --- a/src/computorv1.c +++ b/src/computorv1.c @@ -9,6 +9,7 @@ char *input_g_err; s_token *tokens_g_err; s_term *terms_g_err; +s_polynom *polynom_g_err; /** * PROGRAM @@ -121,7 +122,8 @@ int main(int ac, char **av) } // reduce - s_polynom *polynom; + s_polynom polynom[1]; + polynom_g_err = polynom; reduce(terms, polynom); // debug diff --git a/src/parser.c b/src/parser.c index e595eab..0bfcb0b 100644 --- a/src/parser.c +++ b/src/parser.c @@ -66,7 +66,7 @@ static double get_double_value(s_token token) return token.value_int; } -static double get_coefficient(s_token *tokens, int i, int *token_count) +static double get_coefficient_absolute(s_token *tokens, int i, int *token_count) { double coefficient; @@ -227,6 +227,7 @@ int parse(s_token *tokens, s_term *terms, int terms_count_max) int i; int terms_count; int token_count; + int sign; e_term_position term_position; check_variables(tokens); @@ -251,14 +252,19 @@ int parse(s_token *tokens, s_term *terms, int terms_count_max) terms[terms_count].position = term_position; // sign + sign = 1; e_term_sign ret_sign = get_sign(tokens, i, &token_count); terms[terms_count].sign = ret_sign; + if (ret_sign == TERM_MINUS) + { + 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 // coefficient - double ret_coefficient = get_coefficient(tokens, i, &token_count); - terms[terms_count].coefficient = ret_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 diff --git a/src/reduce.c b/src/reduce.c index f667e16..c8da944 100644 --- a/src/reduce.c +++ b/src/reduce.c @@ -4,4 +4,29 @@ void reduce(s_term *terms, s_polynom *polynom) { + int i; + double tmp; + + i = 0; + while (terms[i].position != TERM_END) + { + 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; + } + i++; + } } \ No newline at end of file diff --git a/src/utils/errors.c b/src/utils/errors.c index 7c392b8..8310c06 100644 --- a/src/utils/errors.c +++ b/src/utils/errors.c @@ -11,7 +11,7 @@ static void print_context_tokens() { int i; - ft_putchar('\n'); + ft_putchar_fd('\n', STDERR_FILENO); i = 0; while (tokens_g_err[i].type != TOKEN_END) { @@ -54,14 +54,14 @@ static void print_context_tokens() } i++; } - ft_putchar('\n'); + ft_putchar_fd('\n', STDERR_FILENO); } static void print_context_terms() { int i; - ft_putchar('\n'); + ft_putchar_fd('\n', STDERR_FILENO); i = 0; while (terms_g_err[i].position != TERM_END) { @@ -92,7 +92,14 @@ static void print_context_terms() ft_dprintf(STDERR_FILENO, " | %8s : %d\n", "exponent", terms_g_err[i].exponent); i++; } - ft_putchar('\n'); + ft_putchar_fd('\n', STDERR_FILENO); +} + +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); } void print_state() @@ -104,6 +111,8 @@ void print_state() print_context_tokens(); if (terms_g_err) print_context_terms(); + if (polynom_g_err) + print_context_polynom(); } int stop_errors(e_program_error err, const char *details, ...)