From 7144337f0be47dc4aeaf48eb0979df5474a5a5ed Mon Sep 17 00:00:00 2001 From: AZEN-SGG <74971141+AZEN-SGG@users.noreply.github.com> Date: Thu, 1 May 2025 19:05:23 +0300 Subject: [PATCH] Task 7 is done --- 2025.05.02/07Ex/Makefile | 42 +++++++++++++++++++++++++ 2025.05.02/07Ex/init_f.c | 61 +++++++++++++++++++++++++++++++++++++ 2025.05.02/07Ex/init_f.h | 13 ++++++++ 2025.05.02/07Ex/io_status.h | 16 ++++++++++ 2025.05.02/07Ex/main.c | 47 ++++++++++++++++++++++++++++ 2025.05.02/07Ex/solve.c | 33 ++++++++++++++++++++ 2025.05.02/07Ex/solve.h | 10 ++++++ 7 files changed, 222 insertions(+) create mode 100644 2025.05.02/07Ex/Makefile create mode 100644 2025.05.02/07Ex/init_f.c create mode 100644 2025.05.02/07Ex/init_f.h create mode 100644 2025.05.02/07Ex/io_status.h create mode 100644 2025.05.02/07Ex/main.c create mode 100644 2025.05.02/07Ex/solve.c create mode 100644 2025.05.02/07Ex/solve.h diff --git a/2025.05.02/07Ex/Makefile b/2025.05.02/07Ex/Makefile new file mode 100644 index 0000000..9febf6f --- /dev/null +++ b/2025.05.02/07Ex/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 = a07.$(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/07Ex/init_f.c b/2025.05.02/07Ex/init_f.c new file mode 100644 index 0000000..2dad4af --- /dev/null +++ b/2025.05.02/07Ex/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) * 1e100; +} + +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/07Ex/init_f.h b/2025.05.02/07Ex/init_f.h new file mode 100644 index 0000000..ee5e8e8 --- /dev/null +++ b/2025.05.02/07Ex/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/07Ex/io_status.h b/2025.05.02/07Ex/io_status.h new file mode 100644 index 0000000..8867bee --- /dev/null +++ b/2025.05.02/07Ex/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/07Ex/main.c b/2025.05.02/07Ex/main.c new file mode 100644 index 0000000..79aa0e8 --- /dev/null +++ b/2025.05.02/07Ex/main.c @@ -0,0 +1,47 @@ +#include +#include +#include + +#include "init_f.h" +#include "solve.h" + +/* ./a.out x_0 eps M k */ +int main(int argc, char *argv[]) +{ + double t, x_0, eps, x = 0; + int m, k, cl, it, task = 7; + + double (*f) (double); + double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6, sin}; // TODO: Remove sin + int len_f = sizeof(f_lst) / sizeof(f_lst[0]); + + if ( + !((argc == 5) && + sscanf(argv[1], "%lf", &x_0) == 1 && + (sscanf(argv[2], "%lf", &eps) == 1 && (eps >= 0)) && + ((sscanf(argv[3], "%d", &m) == 1) && (m > 0)) && + ((sscanf(argv[4], "%d", &k) == 1) && ((0 <= k) && (k <= len_f)))) + ) { + fprintf(stderr, "Usage: %s x_0 eps M k\n", argv[0]); + return -1; + } + + f = f_lst[k]; + + t = clock(); + it = t7_solve(f, x_0, eps, m, &x); + t = (clock() - t) / CLOCKS_PER_SEC; + + cl = get_call_count(); + + if (it >= m) + { + fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t); + 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/07Ex/solve.c b/2025.05.02/07Ex/solve.c new file mode 100644 index 0000000..ed2c4ac --- /dev/null +++ b/2025.05.02/07Ex/solve.c @@ -0,0 +1,33 @@ +#include "solve.h" + +#include +#include + +int t7_solve ( + double (*f) (double), + double x_0, double eps, + int m, double *x +) { + int it = 0; + double y = f(x_0); + + if (fabs(y - x_0) - eps < DBL_EPSILON) + { + *x = x_0; + return 1; + } + + for (it = 0; it < m; ++it) + { + x_0 = y; + y = f(x_0); + + if (fabs(y - x_0) - eps < DBL_EPSILON) + break; + } + + *x = x_0; + + return it; +} + diff --git a/2025.05.02/07Ex/solve.h b/2025.05.02/07Ex/solve.h new file mode 100644 index 0000000..3302d16 --- /dev/null +++ b/2025.05.02/07Ex/solve.h @@ -0,0 +1,10 @@ +#ifndef SOLVE_H +#define SOLVE_H + +int t7_solve ( + double (*f) (double), + double x_0, double eps, + int m, double *x + ); + +#endif