reduced form calculated
This commit is contained in:
1
Makefile
1
Makefile
@@ -40,6 +40,7 @@ D_SRCS = ./src \
|
||||
SRCS = computorv1.c \
|
||||
lexer.c \
|
||||
parser.c \
|
||||
reduce.c \
|
||||
errors.c \
|
||||
print_enums.c
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
12
src/parser.c
12
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
|
||||
|
||||
|
||||
25
src/reduce.c
25
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++;
|
||||
}
|
||||
}
|
||||
@@ -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, ...)
|
||||
|
||||
Reference in New Issue
Block a user