diff --git a/2025.05.02/08Ex/main.c b/2025.05.02/08Ex/main.c index 8904670..86253e2 100644 --- a/2025.05.02/08Ex/main.c +++ b/2025.05.02/08Ex/main.c @@ -3,15 +3,13 @@ #include #include "init_f.h" -#include "status.h" #include "solve.h" /* ./a.out a b eps M k */ int main(int argc, char *argv[]) { double t, a, b, eps, x = 0; - int m, k, cl, it = 0, task = 8; - status ret; + int m, k, it, cl, task = 8; double (*f) (double); double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6, sin}; // TODO: Remove sin @@ -27,38 +25,25 @@ int main(int argc, char *argv[]) ((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f)))) ) { fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]); - return 1; + return -1; } f = f_lst[k]; t = clock(); - ret = t8_solve(f, a, b, eps, m, &x, &it); + it = t8_solve(f, a, b, eps, m, &x); t = (clock() - t) / CLOCKS_PER_SEC; cl = get_call_count(); -// printf("x = %e\tf(x) = %e\n", x, f(x)); - - do { - switch (ret) - { - case SUCCESS: - continue; - case RUN_TIME: - fprintf(stderr, "Error: with code %d - Not enough iterations!\n", ret); - break; - case EQUAL: - fprintf(stderr, "Error: with code %d - The dots degenerated into one!\n", ret); - break; - } - + if (it >= m) + { fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t); - return ret; - } while (0); - - printf("%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, f(x), it, cl, t); - - return 0; + return -2; + } else + { + printf("%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, f(x), it, cl, t); + return 0; + } } diff --git a/2025.05.02/08Ex/solve.c b/2025.05.02/08Ex/solve.c index 113047b..763bb2d 100644 --- a/2025.05.02/08Ex/solve.c +++ b/2025.05.02/08Ex/solve.c @@ -1,8 +1,8 @@ #include "solve.h" -#include "status.h" #include #include +#include int t8_solve ( @@ -11,54 +11,46 @@ int t8_solve ( double eps, int m, double *x ) { int it = 0; - status ret = SUCCESS; - double c = DBL_MAX, y, y_a = f(a), y_b = f(b); + double x_l = 0, y_l = 0, c = a, y = f(a); - if (fabs(y_a) - eps < DBL_EPSILON) + if (fabs(b - a) < DBL_EPSILON) { *x = a; - return SUCCESS; - } if (fabs(y_b) - eps < DBL_EPSILON) - { - *x = b; - return SUCCESS; - } if (fabs(fabs(y_b) - fabs(y_a)) < DBL_EPSILON) - { - *x = a; - return EQUAL; + return 1; } - for (it = 0; it < m; ++it) + for (double h = (b - a) * 0.1; fabs(h) > DBL_EPSILON; h *= -0.1) { - double h = (b - a) * 0.1; - c = a + h; - y = f(c); + do { + if (it >= m) + break; + + it++; + x_l = c; + y_l = y; + + c += h; + y = f(c); + } while (((y - y_l) - eps) > DBL_EPSILON); - if (fabs(y) - eps < DBL_EPSILON) - { - ret = SUCCESS; + if (it >= m) break; - } else if (fabs(y_a) - fabs(y_b) > DBL_EPSILON) + + if ((c - b) > DBL_EPSILON) { - a = c; - y_a = y; - } else if (fabs(y_b) - fabs(y_a) > DBL_EPSILON) + x_l = b; + y_l = f(b); + break; + } else if ((a - c) > DBL_EPSILON) { - b = c; - y_b = y; - } else - { - ret = EQUAL; + x_l = a; + y_l = f(b); break; } } - if (it >= m) - ret = RUN_TIME; - - *x = c; - *m_it = it; + *x = x_l; - return ret; + return it; } diff --git a/2025.05.02/08Ex/solve.h b/2025.05.02/08Ex/solve.h index e27e04f..f028ddf 100644 --- a/2025.05.02/08Ex/solve.h +++ b/2025.05.02/08Ex/solve.h @@ -1,12 +1,10 @@ #ifndef SOLVE_H #define SOLVE_H -#include "status.h" - -status t8_solve ( +int t8_solve ( double (*f) (double), double a, double b, - double eps, int m, double *x, int *m_it + double eps, int m, double *x ); #endif