Compare commits
3 Commits
858d4f3ac8
...
1962b87895
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1962b87895 | ||
|
|
3732c0b777 | ||
|
|
2393c1eea4 |
105
.vscode/launch.json
vendored
105
.vscode/launch.json
vendored
File diff suppressed because one or more lines are too long
23
.vscode/tasks.json
vendored
Normal file
23
.vscode/tasks.json
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"version": "2.0.0",
|
||||
"tasks": [
|
||||
{
|
||||
"type": "cppbuild",
|
||||
"label": "C/C++: gcc build active file",
|
||||
"command": "/usr/bin/gcc",
|
||||
"args": [
|
||||
"-fdiagnostics-color=always",
|
||||
"-g",
|
||||
"${file}",
|
||||
"-o",
|
||||
"${fileDirname}/${fileBasenameNoExtension}"
|
||||
],
|
||||
"options": {
|
||||
"cwd": "${fileDirname}"
|
||||
},
|
||||
"problemMatcher": ["$gcc"],
|
||||
"group": "build",
|
||||
"detail": "compiler: /usr/bin/gcc"
|
||||
}
|
||||
]
|
||||
}
|
||||
2
Makefile
2
Makefile
@@ -53,7 +53,7 @@ SRCS = main.c \
|
||||
# COMPILATION CONFIG :
|
||||
CC = gcc
|
||||
EXT = c
|
||||
CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -g3
|
||||
CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -g
|
||||
LFLAGS = -L$(D_LIB) -lft
|
||||
LFLAGS += -lm
|
||||
|
||||
|
||||
@@ -8,8 +8,9 @@
|
||||
- [ ] check why number stored in int or double
|
||||
- [x] get rid of lib math (check in libft)
|
||||
- [x] use ft_abs() instead of abs() -> actually abs() is in stdlib
|
||||
- [x] handling sign '-' : "+ -2"
|
||||
- [x] max exponent len :
|
||||
- [x] solve expressions without '=' ?
|
||||
- [x] solve when max exponent is 0
|
||||
- [x] check reduced form in subject
|
||||
|
||||
## ressources
|
||||
|
||||
|
||||
@@ -101,7 +101,7 @@ typedef struct
|
||||
int exponent;
|
||||
} s_term;
|
||||
|
||||
void parse(s_token *tokens, s_term *terms, int terms_count_max);
|
||||
void parse(s_token *tokens, s_term *terms);
|
||||
|
||||
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
* REDUCE.C
|
||||
|
||||
2
libft
2
libft
Submodule libft updated: 71806cb923...9ac98cb1f5
@@ -54,16 +54,17 @@ static int get_max_exponent(s_term *terms)
|
||||
i++;
|
||||
}
|
||||
|
||||
print_debug("-> max_exponent: %i\n\n", max_exponent);
|
||||
return max_exponent;
|
||||
}
|
||||
|
||||
static int get_number_of_exponents(s_term *terms, int max_exponent)
|
||||
{
|
||||
int i;
|
||||
int nbr_of_exponent;
|
||||
int nbr_of_exponents;
|
||||
|
||||
if (max_exponent <= 0 || max_exponent > MAX_VLA_SIZE)
|
||||
stop_errors("max_exponent should be between 1 and %d, but got : %d\n", MAX_VLA_SIZE, max_exponent);
|
||||
if (max_exponent < 0 || max_exponent > MAX_VLA_SIZE)
|
||||
stop_errors("max_exponent should be between 0 and %d, but got : %d\n", MAX_VLA_SIZE, max_exponent);
|
||||
int exponent_present[max_exponent];
|
||||
ft_bzero(exponent_present, sizeof(exponent_present));
|
||||
|
||||
@@ -76,16 +77,17 @@ static int get_number_of_exponents(s_term *terms, int max_exponent)
|
||||
}
|
||||
|
||||
// Count unique exponents
|
||||
nbr_of_exponent = 0;
|
||||
nbr_of_exponents = 0;
|
||||
i = 0;
|
||||
while (i < max_exponent)
|
||||
{
|
||||
if (exponent_present[i] == 1)
|
||||
nbr_of_exponent++;
|
||||
nbr_of_exponents++;
|
||||
i++;
|
||||
}
|
||||
|
||||
return nbr_of_exponent;
|
||||
print_debug("-> nbr_of_exponents: %i\n\n", nbr_of_exponents);
|
||||
return nbr_of_exponents;
|
||||
}
|
||||
|
||||
void launch_computorv1(char *input)
|
||||
@@ -118,15 +120,11 @@ void launch_computorv1(char *input)
|
||||
s_term terms[terms_count_prediction];
|
||||
terms_g_err = terms;
|
||||
ft_bzero(terms, sizeof(terms));
|
||||
parse(tokens, terms, terms_count_prediction);
|
||||
parse(tokens, terms);
|
||||
|
||||
// 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);
|
||||
if (nbr_of_exponents <= 0 || nbr_of_exponents > MAX_VLA_SIZE)
|
||||
stop_errors("nbr_of_exponents should be between 1 and %d, but got : %d\n", MAX_VLA_SIZE, 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));
|
||||
|
||||
10
src/lexer.c
10
src/lexer.c
@@ -256,9 +256,9 @@ void lexerize(const char *input, s_token *tokens)
|
||||
int tokens_count;
|
||||
int input_pos;
|
||||
int token_size;
|
||||
bool has_equal;
|
||||
// bool has_equal;
|
||||
|
||||
has_equal = false;
|
||||
// has_equal = false;
|
||||
tokens_count = 0;
|
||||
input_pos = 0;
|
||||
while (input[input_pos])
|
||||
@@ -326,7 +326,7 @@ void lexerize(const char *input, s_token *tokens)
|
||||
}
|
||||
else if (token_is_equal(input, input_pos, &token_size))
|
||||
{
|
||||
has_equal = true;
|
||||
// has_equal = true;
|
||||
tokens[tokens_count].type = TOKEN_EQUAL;
|
||||
tokens[tokens_count].tag = TOKEN_NO_TAG;
|
||||
tokens[tokens_count].value_char = '=';
|
||||
@@ -344,8 +344,8 @@ void lexerize(const char *input, s_token *tokens)
|
||||
input_pos += token_size;
|
||||
}
|
||||
|
||||
if (!has_equal)
|
||||
stop_errors("the input polynomial does not contains an equal sign, it's an expression not an equation.\n");
|
||||
// if (!has_equal)
|
||||
// stop_errors("the input polynomial does not contains an equal sign, it's an expression not an equation.\n");
|
||||
|
||||
tokens[tokens_count].type = TOKEN_END;
|
||||
tokens[tokens_count].tag = TOKEN_NO_TAG;
|
||||
|
||||
21
src/parser.c
21
src/parser.c
@@ -222,7 +222,7 @@ static void check_variables(s_token *tokens)
|
||||
}
|
||||
}
|
||||
|
||||
void parse(s_token *tokens, s_term *terms, int terms_count_max)
|
||||
void parse(s_token *tokens, s_term *terms)
|
||||
{
|
||||
int i;
|
||||
int terms_count;
|
||||
@@ -238,7 +238,7 @@ void parse(s_token *tokens, s_term *terms, int terms_count_max)
|
||||
token_count = 0;
|
||||
i = 0;
|
||||
term_position = TERM_LEFT;
|
||||
while (tokens[i].type != TOKEN_END && terms_count < terms_count_max)
|
||||
while (tokens[i].type != TOKEN_END)
|
||||
{
|
||||
print_debug("- token[%i]\n", i);
|
||||
|
||||
@@ -277,16 +277,9 @@ void parse(s_token *tokens, s_term *terms, int terms_count_max)
|
||||
terms_count++;
|
||||
}
|
||||
|
||||
// last token is TOKEN_END, and terms[] should have at least one more spot for the END term
|
||||
if (tokens[i].type == TOKEN_END && terms_count < terms_count_max)
|
||||
{
|
||||
terms[terms_count].position = TERM_POS_END;
|
||||
terms[terms_count].sign = TERM_SIGN_END;
|
||||
terms[terms_count].coefficient = 0;
|
||||
terms[terms_count].exponent = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
stop_errors("terms_count: %i, terms_count_max: %i, tokens[%i].type: %s", terms_count, terms_count_max, i, token_type_to_str(tokens[i].type));
|
||||
}
|
||||
// last token is TOKEN_END
|
||||
terms[terms_count].position = TERM_POS_END;
|
||||
terms[terms_count].sign = TERM_SIGN_END;
|
||||
terms[terms_count].coefficient = 0;
|
||||
terms[terms_count].exponent = 0;
|
||||
}
|
||||
@@ -61,18 +61,18 @@ static void print_reduced_form_beautify(s_polynom *polynom)
|
||||
if (exponent == 1)
|
||||
{
|
||||
if (coefficient == 1)
|
||||
printf("x ");
|
||||
printf("X ");
|
||||
if (coefficient > 1)
|
||||
printf("%gx ", coefficient);
|
||||
printf("%gX ", coefficient);
|
||||
}
|
||||
|
||||
// for x²
|
||||
if (exponent >= 2)
|
||||
{
|
||||
if (coefficient == 1)
|
||||
printf("x%s ", ft_superscript(exponent + '0'));
|
||||
printf("X%s ", ft_superscript(exponent + '0'));
|
||||
if (coefficient > 1)
|
||||
printf("%gx%s ", coefficient, ft_superscript(exponent + '0'));
|
||||
printf("%gX%s ", coefficient, ft_superscript(exponent + '0'));
|
||||
}
|
||||
fflush(stdout);
|
||||
|
||||
@@ -116,7 +116,7 @@ void print_reduced_form(s_polynom *polynom, int degree)
|
||||
// print term
|
||||
if (!is_first_term)
|
||||
ft_putchar(' ');
|
||||
printf("%g * x^%i ", ft_fabs(found_term->coefficient), i);
|
||||
printf("%g * X^%i ", ft_fabs(found_term->coefficient), i);
|
||||
fflush(stdout);
|
||||
|
||||
is_first_term = false;
|
||||
|
||||
@@ -135,14 +135,13 @@ static void print_solution_delta_zero(s_solution_degree_2 solution)
|
||||
static void print_solution_delta_positiv(s_solution_degree_2 solution)
|
||||
{
|
||||
ft_printf("Discriminant is strictly positive, the two solutions are:\n");
|
||||
printf("%g\n", positiv_zero(solution.left_term + solution.right_term));
|
||||
printf("%g\n", positiv_zero(solution.left_term - solution.right_term));
|
||||
printf("%g\n", positiv_zero(solution.left_term + solution.right_term));
|
||||
}
|
||||
|
||||
static void print_solution_delta_negativ(s_solution_degree_2 solution)
|
||||
{
|
||||
double denominator;
|
||||
double denominator_abs;
|
||||
int denominator_sign;
|
||||
double right_term_abs;
|
||||
int right_term_sign;
|
||||
@@ -154,32 +153,45 @@ static void print_solution_delta_negativ(s_solution_degree_2 solution)
|
||||
|
||||
if (solution.all_int)
|
||||
{
|
||||
double first_term_numerator;
|
||||
double first_term_denominator_abs;
|
||||
double second_term_numerator;
|
||||
double second_term_denominator_abs;
|
||||
|
||||
denominator = positiv_zero(solution.a * 2);
|
||||
denominator_abs = ft_fabs(denominator);
|
||||
denominator_sign = ft_fsign(denominator);
|
||||
|
||||
first_term_denominator_abs = ft_fabs(denominator);
|
||||
first_term_numerator = solution.b;
|
||||
reduce_fraction(&first_term_numerator, &first_term_denominator_abs);
|
||||
|
||||
second_term_denominator_abs = ft_fabs(denominator);
|
||||
second_term_numerator = positiv_zero(solution.delta_sqrt);
|
||||
reduce_fraction(&second_term_numerator, &second_term_denominator_abs);
|
||||
|
||||
// solution 1
|
||||
if (!is_nearly_equal_zero(solution.b))
|
||||
if (!is_nearly_equal_zero(first_term_numerator))
|
||||
{
|
||||
has_first_term = true;
|
||||
printf("%g/%g ", solution.b * -1 * denominator_sign, denominator_abs);
|
||||
printf("%g/%g ", first_term_numerator * -1 * denominator_sign, first_term_denominator_abs);
|
||||
}
|
||||
if (denominator_sign == -1)
|
||||
printf("- ");
|
||||
else if (has_first_term)
|
||||
printf("+ "); // dont print '+' if it's first term
|
||||
printf("%gi/%g\n", positiv_zero(solution.delta_sqrt), denominator_abs);
|
||||
printf("%gi/%g\n", second_term_numerator, second_term_denominator_abs);
|
||||
|
||||
// solution 2
|
||||
if (!is_nearly_equal_zero(solution.b))
|
||||
if (!is_nearly_equal_zero(first_term_numerator))
|
||||
{
|
||||
has_first_term = true;
|
||||
printf("%g/%g ", solution.b * -1 * denominator_sign, denominator_abs);
|
||||
printf("%g/%g ", first_term_numerator * -1 * denominator_sign, first_term_denominator_abs);
|
||||
}
|
||||
if (denominator_sign == 1)
|
||||
printf("- ");
|
||||
else if (has_first_term)
|
||||
printf("+ "); // dont print '+' if it's first term
|
||||
printf("%gi/%g\n", positiv_zero(solution.delta_sqrt), denominator_abs);
|
||||
printf("%gi/%g\n", second_term_numerator, second_term_denominator_abs);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -192,7 +204,7 @@ static void print_solution_delta_negativ(s_solution_degree_2 solution)
|
||||
has_first_term = true;
|
||||
printf("%g ", solution.left_term);
|
||||
}
|
||||
if (right_term_sign == -1)
|
||||
if (right_term_sign == 1)
|
||||
printf("- ");
|
||||
else if (has_first_term)
|
||||
printf("+ "); // dont print '+' if it's first term
|
||||
@@ -205,7 +217,7 @@ static void print_solution_delta_negativ(s_solution_degree_2 solution)
|
||||
has_first_term = true;
|
||||
printf("%g ", solution.left_term);
|
||||
}
|
||||
if (right_term_sign == 1)
|
||||
if (right_term_sign == -1)
|
||||
printf("- ");
|
||||
else if (has_first_term)
|
||||
printf("+ "); // dont print '+' if it's first term
|
||||
|
||||
Reference in New Issue
Block a user