From 8e114b5c29b5e9431387ddaeaa3e2e6e0ea7d14e Mon Sep 17 00:00:00 2001 From: AZEN-SGG Date: Wed, 30 Apr 2025 12:01:55 +0300 Subject: [PATCH] Programm is compiling --- 2025.05.02/01Ex/Makefile | 42 +++++++++++++++++++++++ 2025.05.02/01Ex/init_f.c | 61 +++++++++++++++++++++++++++++++++ 2025.05.02/01Ex/init_f.h | 13 +++++++ 2025.05.02/01Ex/io_status.h | 16 +++++++++ 2025.05.02/01Ex/main.c | 64 +++++++++++++++++++++++++++++++++++ 2025.05.02/01Ex/solve.c | 55 ++++++++++++++++++++++++++++++ 2025.05.02/01Ex/solve.h | 12 +++++++ 2025.05.02/01Ex/status.h | 11 ++++++ 2025.05.02/Example/Makefile | 2 +- 2025.05.02/Example/array_io.c | 37 -------------------- 2025.05.02/Example/array_io.h | 20 ----------- 2025.05.02/Example/init_f.c | 15 ++++++++ 2025.05.02/Example/init_f.h | 1 + 2025.05.02/Example/main.c | 30 ++++++++-------- 2025.05.02/Example/solve.c | 36 ++++++++++---------- 2025.05.02/Example/solve.h | 2 +- 2025.05.02/Example/status.h | 1 + 17 files changed, 328 insertions(+), 90 deletions(-) create mode 100644 2025.05.02/01Ex/Makefile create mode 100644 2025.05.02/01Ex/init_f.c create mode 100644 2025.05.02/01Ex/init_f.h create mode 100644 2025.05.02/01Ex/io_status.h create mode 100644 2025.05.02/01Ex/main.c create mode 100644 2025.05.02/01Ex/solve.c create mode 100644 2025.05.02/01Ex/solve.h create mode 100644 2025.05.02/01Ex/status.h delete mode 100644 2025.05.02/Example/array_io.c delete mode 100644 2025.05.02/Example/array_io.h diff --git a/2025.05.02/01Ex/Makefile b/2025.05.02/01Ex/Makefile new file mode 100644 index 0000000..2eb337d --- /dev/null +++ b/2025.05.02/01Ex/Makefile @@ -0,0 +1,42 @@ +WFLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused \ +-Wempty-body -Wlogical-op -Wold-style-declaration -Wmissing-parameter-type \ +-Wignored-qualifiers -Winit-self -Wshadow -Wtype-limits \ +-Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 \ +-Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs \ +-Wmissing-prototypes -Wmissing-declarations -Wold-style-definition \ +-Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal \ +-Wwrite-strings -Wno-long-long -std=gnu99 -Wstrict-prototypes \ +-Wmissing-field-initializers -Wpointer-sign + +LDFLAGS = -std=gnu99 -mfpmath=sse -O3 +LDLIBS = -lm + +ifeq ($(OS),Windows_NT) + EXE = exe + CLEAN = del + LDLIBS += -lssp +else + EXE = out + CLEAN = rm -f +endif + +TARGET = a01.$(EXE) +OBJ = main.o solve.o init_f.o + +%.o: %.c + gcc $(WFLAGS) $(LDFLAGS) -c $< -o $@ + +$(TARGET): $(OBJ) + gcc $^ -o $@ $(LDLIBS) + +# Отладочная сборка (gdb) +gdb: LDFLAGS = -std=gnu99 -mfpmath=sse -g -O0 +gdb: clean $(TARGET) + +# Профилировочная сборка (gprof) +prof: LDFLAGS += -pg +prof: LDLIBS += -pg +prof: clean $(TARGET) + +clean: + $(CLEAN) *.o *$(EXE) diff --git a/2025.05.02/01Ex/init_f.c b/2025.05.02/01Ex/init_f.c new file mode 100644 index 0000000..22eb5ea --- /dev/null +++ b/2025.05.02/01Ex/init_f.c @@ -0,0 +1,61 @@ +#include "init_f.h" + +#include + + +static int cl = 0; + +int get_call_count (void) +{ + return cl; +} + +double f0 (double x) +{ + cl++; + (void)x; + + return 1; +} + +double f1 (double x) +{ + cl++; + return (x * 1e-100) - 1.0; +} + +double f2 (double x) +{ + cl++; + return 4 - x * x; +} + +double f3 (double x) +{ + double x_2 = x * x; + cl++; + + return x * x_2 + 3 * x_2 + 16; +} + +double f4 (double x) +{ + double x_2 = x * x; + cl++; + + return 3 - 2 * x_2 - x_2 * x_2; +} + +double f5 (double x) +{ + cl++; + return sqrt(fabs(x) + 1) - 2; +} + +double f6 (double x) +{ + cl++; + return sqrt(sqrt(fabs(x) + 1) + 1) - 2; +} + + diff --git a/2025.05.02/01Ex/init_f.h b/2025.05.02/01Ex/init_f.h new file mode 100644 index 0000000..ee5e8e8 --- /dev/null +++ b/2025.05.02/01Ex/init_f.h @@ -0,0 +1,13 @@ +#ifndef INIT_F_H +#define INIT_F_H + +int get_call_count (void); +double f0 (double x); +double f1 (double x); +double f2 (double x); +double f3 (double x); +double f4 (double x); +double f5 (double x); +double f6 (double x); + +#endif diff --git a/2025.05.02/01Ex/io_status.h b/2025.05.02/01Ex/io_status.h new file mode 100644 index 0000000..8867bee --- /dev/null +++ b/2025.05.02/01Ex/io_status.h @@ -0,0 +1,16 @@ +#ifndef IO_STATUS_H +#define IO_STATUS_H + +#define ERR_MEM "Error: Not enough memory!" +#define ERR_OPEN "Error: Cannot open file" +#define ERR_READ "Error: Cannot read file" +#define ERR_FUNC "Error: Algorithm is not applicable!" + +typedef enum _io_status +{ + SUCCESS, + ERROR_OPEN, + ERROR_READ +} io_status; + +#endif diff --git a/2025.05.02/01Ex/main.c b/2025.05.02/01Ex/main.c new file mode 100644 index 0000000..ee1d50d --- /dev/null +++ b/2025.05.02/01Ex/main.c @@ -0,0 +1,64 @@ +#include +#include +#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 = 1; + status ret; + + double (*f) (double); + double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6, sin}; + int len_f = sizeof(f_lst) / sizeof(f_lst[0]); + + if ( + !((argc == 6) && + sscanf(argv[1], "%lf", &a) == 1 && + sscanf(argv[2], "%lf", &b) == 1 && + sscanf(argv[3], "%lf", &eps) == 1 && + ((sscanf(argv[4], "%d", &m) == 1) && m > 0) && + ((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; + } + + f = f_lst[k]; + + t = clock(); + ret = t1_solve(f, a, b, eps, m, &x, &it); + t = (clock() - t) / CLOCKS_PER_SEC; + + cl = get_call_count(); + + do { + switch (ret) + { + case SUCCESS: + continue; + case RUN_TIME: + fprintf(stderr, "Error: with code %d - Not enough iterations!\n", ret); + break; + case MORE_ONE_ROOT: + fprintf(stderr, "Error: with code %d - The same signs on the boundaries of the segment!\n", ret); + break; + case HIGH_ERROR: + fprintf(stderr, "Error: with code %d - The solution was found with a high error rate!\n", ret); + break; + } + + 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; +} + diff --git a/2025.05.02/01Ex/solve.c b/2025.05.02/01Ex/solve.c new file mode 100644 index 0000000..e639342 --- /dev/null +++ b/2025.05.02/01Ex/solve.c @@ -0,0 +1,55 @@ +#include "solve.h" +#include "status.h" + +#include +#include + +status t1_solve ( + double (*f) (double), + double a, double b, + double eps, int m, double *x, int *m_it +) { + int it = 0; + status ret = SUCCESS; + double c = DBL_MAX, y, y_a = f(a), y_b = f(b); + + + if (y_a * y_b >= -DBL_EPSILON) + { + *x = DBL_MAX; + return MORE_ONE_ROOT; + } + + for (it = 0; it < m; ++it) + { + c = (a + b) * 0.5; + y = f(c); + + if (fabs(y) < eps) + { + ret = SUCCESS; + break; + } else if ((fabs(c - a) < DBL_EPSILON) || (fabs(c - b) < DBL_EPSILON)) + { + ret = HIGH_ERROR; + break; + } else if (y * y_a > DBL_EPSILON) + { + a = c; + y_a = y; + } else if (y * y_b > DBL_EPSILON) + { + b = c; + y_b = y; + } + } + + if (it >= m) + ret = RUN_TIME; + + *x = c; + *m_it = it; + + return ret; +} + diff --git a/2025.05.02/01Ex/solve.h b/2025.05.02/01Ex/solve.h new file mode 100644 index 0000000..d57ce24 --- /dev/null +++ b/2025.05.02/01Ex/solve.h @@ -0,0 +1,12 @@ +#ifndef SOLVE_H +#define SOLVE_H + +#include "status.h" + +status t1_solve ( + double (*f) (double), + double a, double b, + double eps, int m, double *x, int *m_it + ); + +#endif diff --git a/2025.05.02/01Ex/status.h b/2025.05.02/01Ex/status.h new file mode 100644 index 0000000..e085043 --- /dev/null +++ b/2025.05.02/01Ex/status.h @@ -0,0 +1,11 @@ +#ifndef STATUS_H +#define STATUS_H + +typedef enum _status { + SUCCESS, + MORE_ONE_ROOT, + RUN_TIME, + HIGH_ERROR, +} status; + +#endif diff --git a/2025.05.02/Example/Makefile b/2025.05.02/Example/Makefile index ec9e73b..2eb337d 100644 --- a/2025.05.02/Example/Makefile +++ b/2025.05.02/Example/Makefile @@ -21,7 +21,7 @@ else endif TARGET = a01.$(EXE) -OBJ = main.o solve.o array_io.o +OBJ = main.o solve.o init_f.o %.o: %.c gcc $(WFLAGS) $(LDFLAGS) -c $< -o $@ diff --git a/2025.05.02/Example/array_io.c b/2025.05.02/Example/array_io.c deleted file mode 100644 index 9182088..0000000 --- a/2025.05.02/Example/array_io.c +++ /dev/null @@ -1,37 +0,0 @@ -#include -#include -#include "array_io.h" - -io_status read_values_and_derivatives ( - double * restrict X, - double * restrict Y, - double * restrict D, - const int n, - const char * restrict name - ) -{ - FILE *fp; - if (!(fp = fopen(name, "r"))) - return ERROR_OPEN; - - for (int i = 0; i < n; ++i) - if (fscanf(fp, "%lf\t%lf\t%lf", X + i, Y + i, D + i) != 3) - { fclose(fp); return ERROR_READ; } - - fclose(fp); - return SUCCESS; -} - -void print_values_and_derivatives ( - const double * restrict X, - const double * restrict Y, - const double * restrict D, - const int n, const int p - ) -{ - int np = (n > p ? p : n); - - for (int i = 0; i < np; i++) - printf("f(%lf) = %lf, f`(%lf) = %lf\n", X[i], Y[i], X[i], D[i]); -} - diff --git a/2025.05.02/Example/array_io.h b/2025.05.02/Example/array_io.h deleted file mode 100644 index 2847b31..0000000 --- a/2025.05.02/Example/array_io.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef ARRAY_IO_H -#define ARRAY_IO_H - -#include "io_status.h" - -io_status read_values_and_derivatives ( - double * restrict X, - double * restrict Y, - double * restrict D, - const int n, - const char * restrict name - ); -void print_values_and_derivatives ( - const double * restrict X, - const double * restrict Y, - const double * restrict D, - const int n, const int p - ); - -#endif diff --git a/2025.05.02/Example/init_f.c b/2025.05.02/Example/init_f.c index 532c96d..22eb5ea 100644 --- a/2025.05.02/Example/init_f.c +++ b/2025.05.02/Example/init_f.c @@ -2,8 +2,17 @@ #include + +static int cl = 0; + +int get_call_count (void) +{ + return cl; +} + double f0 (double x) { + cl++; (void)x; return 1; @@ -11,17 +20,20 @@ double f0 (double x) double f1 (double x) { + cl++; return (x * 1e-100) - 1.0; } double f2 (double x) { + cl++; return 4 - x * x; } double f3 (double x) { double x_2 = x * x; + cl++; return x * x_2 + 3 * x_2 + 16; } @@ -29,17 +41,20 @@ double f3 (double x) double f4 (double x) { double x_2 = x * x; + cl++; return 3 - 2 * x_2 - x_2 * x_2; } double f5 (double x) { + cl++; return sqrt(fabs(x) + 1) - 2; } double f6 (double x) { + cl++; return sqrt(sqrt(fabs(x) + 1) + 1) - 2; } diff --git a/2025.05.02/Example/init_f.h b/2025.05.02/Example/init_f.h index c81bd9b..ee5e8e8 100644 --- a/2025.05.02/Example/init_f.h +++ b/2025.05.02/Example/init_f.h @@ -1,6 +1,7 @@ #ifndef INIT_F_H #define INIT_F_H +int get_call_count (void); double f0 (double x); double f1 (double x); double f2 (double x); diff --git a/2025.05.02/Example/main.c b/2025.05.02/Example/main.c index 8fa99bf..ee1d50d 100644 --- a/2025.05.02/Example/main.c +++ b/2025.05.02/Example/main.c @@ -1,7 +1,5 @@ #include -#include #include -#include #include #include "init_f.h" @@ -12,11 +10,12 @@ int main(int argc, char *argv[]) { double t, a, b, eps, x = 0; - int m, k, task = 1; + int m, k, cl, it = 0, task = 1; status ret; double (*f) (double); - double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6}; + double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6, sin}; + int len_f = sizeof(f_lst) / sizeof(f_lst[0]); if ( !((argc == 6) && @@ -24,7 +23,7 @@ int main(int argc, char *argv[]) sscanf(argv[2], "%lf", &b) == 1 && sscanf(argv[3], "%lf", &eps) == 1 && ((sscanf(argv[4], "%d", &m) == 1) && m > 0) && - ((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= 6)))) + ((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; @@ -33,29 +32,32 @@ int main(int argc, char *argv[]) f = f_lst[k]; t = clock(); - ret = t1_solve(f, a, b, eps, m, &x); + ret = t1_solve(f, a, b, eps, m, &x, &it); t = (clock() - t) / CLOCKS_PER_SEC; + + cl = get_call_count(); do { switch (ret) { case SUCCESS: continue; - case MORE_ONE_ROOT: - continue; case RUN_TIME: + fprintf(stderr, "Error: with code %d - Not enough iterations!\n", ret); + break; + case MORE_ONE_ROOT: + fprintf(stderr, "Error: with code %d - The same signs on the boundaries of the segment!\n", ret); + break; + case HIGH_ERROR: + fprintf(stderr, "Error: with code %d - The solution was found with a high error rate!\n", ret); break; } - fprintf(stdout, "Error: with code %d - The method is not applicable!\n", ret); - + fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t); return ret; } while (0); - if (ret == MORE_ONE_ROOT) - printf("%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, count, t); - else - printf("%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, f(x), it, count, t); + 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/Example/solve.c b/2025.05.02/Example/solve.c index 88b2d21..e639342 100644 --- a/2025.05.02/Example/solve.c +++ b/2025.05.02/Example/solve.c @@ -2,18 +2,17 @@ #include "status.h" #include +#include status t1_solve ( double (*f) (double), double a, double b, - double eps, int m, double *x, - int *m_cl, int *m_it + double eps, int m, double *x, int *m_it ) { - static int it = 0 - int cl = 0; - double y_a = f(a), y_b = f(b); - - it++; + int it = 0; + status ret = SUCCESS; + double c = DBL_MAX, y, y_a = f(a), y_b = f(b); + if (y_a * y_b >= -DBL_EPSILON) { @@ -21,14 +20,18 @@ status t1_solve ( return MORE_ONE_ROOT; } - for (cl = 0; cl < m; ++cl) + for (it = 0; it < m; ++it) { - double c = (a + b) * 0.5; - double y = f(c); + c = (a + b) * 0.5; + y = f(c); if (fabs(y) < eps) { - *x = c; + ret = SUCCESS; + break; + } else if ((fabs(c - a) < DBL_EPSILON) || (fabs(c - b) < DBL_EPSILON)) + { + ret = HIGH_ERROR; break; } else if (y * y_a > DBL_EPSILON) { @@ -41,13 +44,12 @@ status t1_solve ( } } - *m_it = it; - *cl = cl; + if (it >= m) + ret = RUN_TIME; + *x = c; + *m_it = it; - if (cl >= m) - return RUN_TIME; - - return SUCCESS; + return ret; } diff --git a/2025.05.02/Example/solve.h b/2025.05.02/Example/solve.h index d5805da..d57ce24 100644 --- a/2025.05.02/Example/solve.h +++ b/2025.05.02/Example/solve.h @@ -6,7 +6,7 @@ status t1_solve ( double (*f) (double), double a, double b, - double eps, int m, double *x + double eps, int m, double *x, int *m_it ); #endif diff --git a/2025.05.02/Example/status.h b/2025.05.02/Example/status.h index 62ebbc5..e085043 100644 --- a/2025.05.02/Example/status.h +++ b/2025.05.02/Example/status.h @@ -5,6 +5,7 @@ typedef enum _status { SUCCESS, MORE_ONE_ROOT, RUN_TIME, + HIGH_ERROR, } status; #endif