145 lines
3.2 KiB
C
145 lines
3.2 KiB
C
/* launcher.c */
|
|
|
|
#include "computorv1.h"
|
|
|
|
static void remove_spaces(char *s)
|
|
{
|
|
char *read = s;
|
|
char *write = s;
|
|
|
|
// copy all non-space chars
|
|
while (*read)
|
|
{
|
|
if (!ft_isspace(*read))
|
|
{
|
|
*write++ = *read;
|
|
}
|
|
read++;
|
|
}
|
|
*write = '\0';
|
|
|
|
// zero the rest of the buffer
|
|
while (write != read)
|
|
{
|
|
*write++ = '\0';
|
|
}
|
|
}
|
|
|
|
static size_t count_any_of(const char *s, const char *set)
|
|
{
|
|
size_t count = 0;
|
|
for (; *s != '\0'; s++)
|
|
{
|
|
if (ft_strchr(set, *s) != NULL)
|
|
{
|
|
count++;
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
|
|
static int get_max_exponent(s_term *terms)
|
|
{
|
|
int i;
|
|
int max_exponent;
|
|
|
|
i = 0;
|
|
max_exponent = 0;
|
|
while (terms[i].position != TERM_POS_END)
|
|
{
|
|
if (terms[i].exponent > max_exponent)
|
|
{
|
|
max_exponent = terms[i].exponent;
|
|
}
|
|
i++;
|
|
}
|
|
|
|
return max_exponent;
|
|
}
|
|
|
|
static int get_number_of_exponents(s_term *terms, int max_exponent)
|
|
{
|
|
int i;
|
|
int nbr_of_exponent;
|
|
|
|
int exponent_present[max_exponent];
|
|
ft_bzero(exponent_present, sizeof(exponent_present));
|
|
|
|
// mark exponents as present
|
|
i = 0;
|
|
while (terms[i].position != TERM_POS_END)
|
|
{
|
|
exponent_present[terms[i].exponent] = 1;
|
|
i++;
|
|
}
|
|
|
|
// Count unique exponents
|
|
nbr_of_exponent = 0;
|
|
i = 0;
|
|
while (i < max_exponent)
|
|
{
|
|
if (exponent_present[i] == 1)
|
|
nbr_of_exponent++;
|
|
i++;
|
|
}
|
|
|
|
return nbr_of_exponent;
|
|
}
|
|
|
|
void launch_computorv1(char *input)
|
|
{
|
|
int max_exponent;
|
|
int nbr_of_exponents;
|
|
int degree;
|
|
size_t arg_len;
|
|
size_t terms_count_prediction;
|
|
|
|
// init
|
|
input_g_err = input;
|
|
remove_spaces(input);
|
|
|
|
// lexerize
|
|
arg_len = ft_strlen(input) + 1; // +1 for last END token
|
|
print_debug("\n-> tokens[%i]\n", arg_len);
|
|
s_token tokens[arg_len];
|
|
tokens_g_err = tokens;
|
|
ft_bzero(tokens, sizeof(tokens));
|
|
lexerize(input, tokens);
|
|
|
|
// parse
|
|
terms_count_prediction = count_any_of(input, "-+=") + 2; // +1 for first term that can have no leading '+', +1 for last term == NULL
|
|
print_debug("-> terms[%i]\n\n", terms_count_prediction);
|
|
s_term terms[terms_count_prediction];
|
|
terms_g_err = terms;
|
|
ft_bzero(terms, sizeof(terms));
|
|
parse(tokens, terms, terms_count_prediction);
|
|
|
|
// reduce
|
|
max_exponent = get_max_exponent(terms);
|
|
print_debug("-> max_exponent: %i\n\n", max_exponent);
|
|
nbr_of_exponents = get_number_of_exponents(terms, max_exponent);
|
|
print_debug("-> nbr_of_exponents: %i\n\n", nbr_of_exponents);
|
|
s_polynom polynom[nbr_of_exponents + 2]; // +1 for last term, +1 for the degree (eg. degree 2 means 3 terms)
|
|
polynom_g_err = polynom;
|
|
ft_bzero(polynom, sizeof(polynom));
|
|
degree = reduce(terms, polynom, max_exponent);
|
|
print_debug("-> degree: %i\n\n", degree);
|
|
|
|
// print before solution
|
|
print_reduced_form(polynom);
|
|
print_degree(polynom, degree);
|
|
|
|
// solve
|
|
s_solution solution[1];
|
|
solution_g_err = solution;
|
|
ft_bzero(solution, sizeof(solution));
|
|
solution[0].degree = degree;
|
|
solve(polynom, solution);
|
|
|
|
// print solution
|
|
print_solution(solution);
|
|
|
|
// debug
|
|
print_state();
|
|
}
|