reduced form calculated
This commit is contained in:
1
Makefile
1
Makefile
@@ -40,6 +40,7 @@ D_SRCS = ./src \
|
|||||||
SRCS = computorv1.c \
|
SRCS = computorv1.c \
|
||||||
lexer.c \
|
lexer.c \
|
||||||
parser.c \
|
parser.c \
|
||||||
|
reduce.c \
|
||||||
errors.c \
|
errors.c \
|
||||||
print_enums.c
|
print_enums.c
|
||||||
|
|
||||||
|
|||||||
@@ -125,6 +125,6 @@ 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_term *polynom_g_err;
|
extern s_polynom *polynom_g_err;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -9,6 +9,7 @@
|
|||||||
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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PROGRAM
|
* PROGRAM
|
||||||
@@ -121,7 +122,8 @@ int main(int ac, char **av)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// reduce
|
// reduce
|
||||||
s_polynom *polynom;
|
s_polynom polynom[1];
|
||||||
|
polynom_g_err = polynom;
|
||||||
reduce(terms, polynom);
|
reduce(terms, polynom);
|
||||||
|
|
||||||
// debug
|
// 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;
|
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;
|
double coefficient;
|
||||||
|
|
||||||
@@ -227,6 +227,7 @@ int parse(s_token *tokens, s_term *terms, int terms_count_max)
|
|||||||
int i;
|
int i;
|
||||||
int terms_count;
|
int terms_count;
|
||||||
int token_count;
|
int token_count;
|
||||||
|
int sign;
|
||||||
e_term_position term_position;
|
e_term_position term_position;
|
||||||
|
|
||||||
check_variables(tokens);
|
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;
|
terms[terms_count].position = term_position;
|
||||||
|
|
||||||
// sign
|
// sign
|
||||||
|
sign = 1;
|
||||||
e_term_sign ret_sign = get_sign(tokens, i, &token_count);
|
e_term_sign ret_sign = get_sign(tokens, i, &token_count);
|
||||||
terms[terms_count].sign = ret_sign;
|
terms[terms_count].sign = ret_sign;
|
||||||
|
if (ret_sign == TERM_MINUS)
|
||||||
|
{
|
||||||
|
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(tokens, i, &token_count);
|
double ret_coefficient = get_coefficient_absolute(tokens, i, &token_count);
|
||||||
terms[terms_count].coefficient = ret_coefficient;
|
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
|
||||||
|
|
||||||
|
|||||||
25
src/reduce.c
25
src/reduce.c
@@ -4,4 +4,29 @@
|
|||||||
|
|
||||||
void reduce(s_term *terms, s_polynom *polynom)
|
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;
|
int i;
|
||||||
|
|
||||||
ft_putchar('\n');
|
ft_putchar_fd('\n', STDERR_FILENO);
|
||||||
i = 0;
|
i = 0;
|
||||||
while (tokens_g_err[i].type != TOKEN_END)
|
while (tokens_g_err[i].type != TOKEN_END)
|
||||||
{
|
{
|
||||||
@@ -54,14 +54,14 @@ static void print_context_tokens()
|
|||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
ft_putchar('\n');
|
ft_putchar_fd('\n', STDERR_FILENO);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void print_context_terms()
|
static void print_context_terms()
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
ft_putchar('\n');
|
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)
|
||||||
{
|
{
|
||||||
@@ -92,7 +92,14 @@ static void print_context_terms()
|
|||||||
ft_dprintf(STDERR_FILENO, " | %8s : %d\n", "exponent", terms_g_err[i].exponent);
|
ft_dprintf(STDERR_FILENO, " | %8s : %d\n", "exponent", terms_g_err[i].exponent);
|
||||||
i++;
|
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()
|
void print_state()
|
||||||
@@ -104,6 +111,8 @@ void print_state()
|
|||||||
print_context_tokens();
|
print_context_tokens();
|
||||||
if (terms_g_err)
|
if (terms_g_err)
|
||||||
print_context_terms();
|
print_context_terms();
|
||||||
|
if (polynom_g_err)
|
||||||
|
print_context_polynom();
|
||||||
}
|
}
|
||||||
|
|
||||||
int stop_errors(e_program_error err, const char *details, ...)
|
int stop_errors(e_program_error err, const char *details, ...)
|
||||||
|
|||||||
Reference in New Issue
Block a user