wip exo ouput
This commit is contained in:
@@ -137,6 +137,7 @@ void launch_computorv1(char *input)
|
||||
solve(polynom, solution);
|
||||
|
||||
// print solution
|
||||
print_solution(solution);
|
||||
|
||||
// debug
|
||||
print_state();
|
||||
|
||||
50
src/solver.c
50
src/solver.c
@@ -68,36 +68,21 @@ static int find_gcd(double numerator, double denominator)
|
||||
return gcd_int(abs(num_scaled), ft_abs(den_scaled));
|
||||
}
|
||||
|
||||
/*
|
||||
typedef struct
|
||||
static void solve_degree_1(s_solution_degree_1 *solution, double a, double b)
|
||||
{
|
||||
int solution_gcd; // gcd(b, a)
|
||||
int solution_numerator; // -b / gcd
|
||||
int solution_denominator; // a / gcd
|
||||
double solution; // double (-b / a)
|
||||
} s_solution_degree_1;
|
||||
*/
|
||||
/*
|
||||
typedef struct
|
||||
{
|
||||
e_delta_sign delta_sign; // DELTA_PLUS or DELTA_MINUS or DELTA_ZERO
|
||||
double delta_absolute; // |Δ| == |b² - 4ac|
|
||||
double delta_sqrt; // √|Δ|
|
||||
double value;
|
||||
|
||||
int first_term_gcd; // gcd(b, 2a)
|
||||
int first_term_numerator; // -b / gcd
|
||||
int first_term_denominator; // 2a / gcd
|
||||
double first_term; // double (-b / 2a)
|
||||
solution->a = a;
|
||||
solution->b = b;
|
||||
|
||||
int second_term_gcd; // gcd(√|Δ|, 2a)
|
||||
int second_term_numerator; // √|Δ| / gcd
|
||||
int second_term_denominator; // 2a / gcd
|
||||
double second_term; // double (√|Δ| / 2a)
|
||||
|
||||
double solution1; // first_term + second_term
|
||||
double solution2; // first_term - second_term (not if DELTA_ZERO)
|
||||
} s_solution_degree_2;
|
||||
*/
|
||||
solution->solution_gcd = find_gcd(b, a); // gcd(b, a)
|
||||
solution->solution_numerator = -b / solution->solution_gcd; // -b / gcd
|
||||
solution->solution_denominator = a / solution->solution_gcd; // a / gcd
|
||||
value = -b / a;
|
||||
if (value == 0)
|
||||
value = ft_fabs(value);
|
||||
solution->solution = value;
|
||||
}
|
||||
|
||||
static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, double c)
|
||||
{
|
||||
@@ -127,11 +112,6 @@ static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, do
|
||||
solution->second_term = delta_sqrt / 2 * a; // √|Δ| / 2a
|
||||
|
||||
// solution
|
||||
/*
|
||||
degree 2 : delta > 0 ( -b / 2a +- √Δ / 2a )
|
||||
delta == 0 ( -b / 2a )
|
||||
delta < 0 ( -b / 2a +- i√|Δ| / 2a )
|
||||
*/
|
||||
solution->solution1 = solution->first_term; // first_term + second_term
|
||||
if (solution->delta_sign == DELTA_ZERO)
|
||||
{
|
||||
@@ -146,6 +126,8 @@ static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, do
|
||||
else if (solution->delta_sign == DELTA_PLUS)
|
||||
{
|
||||
// no real solution, but can output irreal solution with 'i'
|
||||
solution->solution1 = NAN;
|
||||
solution->solution2 = NAN;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,6 +139,10 @@ void solve(const s_polynom *polynom, s_solution *solution)
|
||||
|
||||
if (solution->degree == 1)
|
||||
{
|
||||
a = polynom[0].coefficient;
|
||||
b = polynom[1].coefficient;
|
||||
|
||||
solve_degree_1(&solution->solution_degree_1, a, b);
|
||||
}
|
||||
else if (solution->degree == 2)
|
||||
{
|
||||
|
||||
@@ -71,9 +71,23 @@ static void print_context_polynom()
|
||||
|
||||
static void print_context_solution()
|
||||
{
|
||||
s_solution_degree_1 solution_1;
|
||||
s_solution_degree_2 solution_2;
|
||||
|
||||
if (solution_g_err->degree == 2)
|
||||
if (solution_g_err->degree == 1)
|
||||
{
|
||||
solution_1 = solution_g_err->solution_degree_1;
|
||||
dprintf(STDERR_FILENO, "degree 1 : x^1 ( ax + b )\n");
|
||||
|
||||
dprintf(STDERR_FILENO, "a : %10g ( a )\n", solution_1.a);
|
||||
dprintf(STDERR_FILENO, "b : %10g ( b )\n", solution_1.b);
|
||||
|
||||
dprintf(STDERR_FILENO, "gcd : %10i ( gcd( b, a ) )\n", solution_1.solution_gcd);
|
||||
dprintf(STDERR_FILENO, "numerator : %10i ( -b / gcd )\n", solution_1.solution_numerator);
|
||||
dprintf(STDERR_FILENO, "denominator : %10i ( a / gcd )\n", solution_1.solution_denominator);
|
||||
dprintf(STDERR_FILENO, "solution : %10g ( -b / a )\n", solution_1.solution);
|
||||
}
|
||||
else if (solution_g_err->degree == 2)
|
||||
{
|
||||
solution_2 = solution_g_err->solution_degree_2;
|
||||
dprintf(STDERR_FILENO, "degree 2 : delta > 0 ( -b / 2a +- √Δ / 2a )\n");
|
||||
|
||||
@@ -156,4 +156,16 @@ void print_degree(s_polynom *polynom, int degree)
|
||||
ft_printf("Polynomial degree: %i\n", degree);
|
||||
stop_errors("The polynomial degree is strictly greater than 2, I can't solve.\n");
|
||||
}
|
||||
}
|
||||
|
||||
void print_solution(s_solution *solution)
|
||||
{
|
||||
if (solution->degree == 1)
|
||||
{
|
||||
ft_printf("The solution is:\n");
|
||||
printf("%g\n", solution->solution_degree_1.solution);
|
||||
}
|
||||
else if (solution->degree == 2)
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user