From 22ef2dda9b6791ce285d6bcdc503d7cf41ebc0b7 Mon Sep 17 00:00:00 2001 From: AZEN-SGG Date: Wed, 30 Apr 2025 21:24:42 +0300 Subject: [PATCH] Task 4 is done! --- 2025.05.02/02Ex/Makefile | 2 +- 2025.05.02/02Ex/main.c | 13 +++--- 2025.05.02/02Ex/solve.c | 39 ++++++------------ 2025.05.02/02Ex/solve.h | 2 +- 2025.05.02/02Ex/status.h | 3 +- 2025.05.02/03Ex/main.c | 3 ++ 2025.05.02/03Ex/solve.c | 14 +++---- 2025.05.02/03Ex/status.h | 1 + 2025.05.02/04Ex/Makefile | 42 ++++++++++++++++++++ 2025.05.02/04Ex/init_f.c | 61 ++++++++++++++++++++++++++++ 2025.05.02/04Ex/init_f.h | 13 ++++++ 2025.05.02/04Ex/io_status.h | 16 ++++++++ 2025.05.02/04Ex/main.c | 64 ++++++++++++++++++++++++++++++ 2025.05.02/04Ex/solve.c | 62 +++++++++++++++++++++++++++++ 2025.05.02/04Ex/solve.h | 12 ++++++ 2025.05.02/04Ex/status.h | 10 +++++ 2025.05.02/tests/find_str.sh | 19 +++++++++ 2025.05.02/tests/must_find_test.sh | 4 +- 18 files changed, 332 insertions(+), 48 deletions(-) create mode 100644 2025.05.02/04Ex/Makefile create mode 100644 2025.05.02/04Ex/init_f.c create mode 100644 2025.05.02/04Ex/init_f.h create mode 100644 2025.05.02/04Ex/io_status.h create mode 100644 2025.05.02/04Ex/main.c create mode 100644 2025.05.02/04Ex/solve.c create mode 100644 2025.05.02/04Ex/solve.h create mode 100644 2025.05.02/04Ex/status.h create mode 100755 2025.05.02/tests/find_str.sh diff --git a/2025.05.02/02Ex/Makefile b/2025.05.02/02Ex/Makefile index 2eb337d..fdc27cb 100644 --- a/2025.05.02/02Ex/Makefile +++ b/2025.05.02/02Ex/Makefile @@ -20,7 +20,7 @@ else CLEAN = rm -f endif -TARGET = a01.$(EXE) +TARGET = a04.$(EXE) OBJ = main.o solve.o init_f.o %.o: %.c diff --git a/2025.05.02/02Ex/main.c b/2025.05.02/02Ex/main.c index 1229afd..7107d7c 100644 --- a/2025.05.02/02Ex/main.c +++ b/2025.05.02/02Ex/main.c @@ -10,7 +10,7 @@ int main(int argc, char *argv[]) { double t, a, b, eps, x = 0; - int m, k, cl, it = 0, task = 1; + int m, k, cl, it = 0, task = 4; status ret; double (*f) (double); @@ -22,7 +22,7 @@ int main(int argc, char *argv[]) sscanf(argv[1], "%lf", &a) == 1 && sscanf(argv[2], "%lf", &b) == 1 && (a <= b) && - sscanf(argv[3], "%lf", &eps) == 1 && + (sscanf(argv[3], "%lf", &eps) == 1 && (eps >= 0)) && ((sscanf(argv[4], "%d", &m) == 1) && m > 0) && ((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f)))) ) { @@ -33,7 +33,7 @@ int main(int argc, char *argv[]) f = f_lst[k]; t = clock(); - ret = t1_solve(f, a, b, eps, m, &x, &it); + ret = t4_solve(f, a, b, eps, m, &x, &it); t = (clock() - t) / CLOCKS_PER_SEC; cl = get_call_count(); @@ -48,11 +48,8 @@ int main(int argc, char *argv[]) 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); + case EQUAL: + fprintf(stderr, "Error: with code %d - The dots degenerated into one!\n", ret); break; } diff --git a/2025.05.02/02Ex/solve.c b/2025.05.02/02Ex/solve.c index abcbddb..ab009e9 100644 --- a/2025.05.02/02Ex/solve.c +++ b/2025.05.02/02Ex/solve.c @@ -3,25 +3,15 @@ #include #include -#include -#include -#include -status t1_solve ( +status t4_solve ( double (*f) (double), double a, double b, double eps, int m, double *x, int *m_it ) { int it = 0; - uint64_t bits; status ret = SUCCESS; double c = DBL_MAX, y, y_a = f(a), y_b = f(b); - bool sgn_a, sgn_b, sgn_c; - - memcpy(&bits, &y_a, sizeof(bits)); - sgn_a = (bits >> 63) & 1; - memcpy(&bits, &y_b, sizeof(bits)); - sgn_b = (bits >> 63) & 1; if (fabs(y_a) - eps < DBL_EPSILON) { @@ -31,38 +21,33 @@ status t1_solve ( { *x = b; return SUCCESS; + } if (fabs(fabs(y_b) - fabs(y_a)) < DBL_EPSILON) + { + *x = a; + return EQUAL; } - if (sgn_a == sgn_b) - { - *x = DBL_MAX; - return MORE_ONE_ROOT; - } - for (it = 0; it < m; ++it) { - c = (a + b) * 0.5; + c = b - ((b - a) / (y_b - y_a)) * y_b; y = f(c); - memcpy(&bits, &y, sizeof(bits)); - sgn_c = (bits >> 63) & 1; - if (fabs(y) - eps < DBL_EPSILON) { ret = SUCCESS; break; - } else if ((fabs(c - a) < DBL_EPSILON) || (fabs(c - b) < DBL_EPSILON)) - { - ret = HIGH_ERROR; - break; - } else if (sgn_c == sgn_a) + } else if (fabs(y_a) - fabs(y_b) > DBL_EPSILON) { a = c; y_a = y; - } else if (sgn_c == sgn_b) + } else if (fabs(y_b) - fabs(y_a) > DBL_EPSILON) { b = c; y_b = y; + } else + { + ret = EQUAL; + break; } } diff --git a/2025.05.02/02Ex/solve.h b/2025.05.02/02Ex/solve.h index d57ce24..64af0c3 100644 --- a/2025.05.02/02Ex/solve.h +++ b/2025.05.02/02Ex/solve.h @@ -3,7 +3,7 @@ #include "status.h" -status t1_solve ( +status t4_solve ( double (*f) (double), double a, double b, double eps, int m, double *x, int *m_it diff --git a/2025.05.02/02Ex/status.h b/2025.05.02/02Ex/status.h index e085043..b2a4d5e 100644 --- a/2025.05.02/02Ex/status.h +++ b/2025.05.02/02Ex/status.h @@ -3,9 +3,8 @@ typedef enum _status { SUCCESS, - MORE_ONE_ROOT, RUN_TIME, - HIGH_ERROR, + EQUAL, } status; #endif diff --git a/2025.05.02/03Ex/main.c b/2025.05.02/03Ex/main.c index 0c2bcc7..a7b2ed6 100644 --- a/2025.05.02/03Ex/main.c +++ b/2025.05.02/03Ex/main.c @@ -57,6 +57,9 @@ int main(int argc, char *argv[]) case DIVERGE: fprintf(stderr, "Error: with code %d - The function is diverging!\n", ret); break; + case BOUNDARIES: + fprintf(stderr, "Error: with code %d - Goes beyond the boundaries!\n", ret); + break; } fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t); diff --git a/2025.05.02/03Ex/solve.c b/2025.05.02/03Ex/solve.c index 715c828..c054144 100644 --- a/2025.05.02/03Ex/solve.c +++ b/2025.05.02/03Ex/solve.c @@ -41,22 +41,22 @@ status t3_solve ( for (it = 0; it < m; ++it) { - c = (y_a * b - y_b * a) / (y_a - y_b); + c = a - ((a - b) / (y_a - y_b)) * y_a; y = f(c); memcpy(&bits, &y, sizeof(bits)); sgn_c = (bits >> 63) & 1; - if (fabs(y) - eps < DBL_EPSILON) + if ((c - a < DBL_EPSILON) || (c - b > DBL_EPSILON)) + { + ret = BOUNDARIES; + break; + } else if (fabs(y) - eps < DBL_EPSILON) { ret = SUCCESS; break; - } else if ((fabs(c - a) < DBL_EPSILON) || (fabs(c - b) < DBL_EPSILON)) - { - ret = HIGH_ERROR; - break; } else if (sgn_c == sgn_a) - { + { a = c; y_a = y; diff --git a/2025.05.02/03Ex/status.h b/2025.05.02/03Ex/status.h index abacc4f..92b5eb6 100644 --- a/2025.05.02/03Ex/status.h +++ b/2025.05.02/03Ex/status.h @@ -7,6 +7,7 @@ typedef enum _status { RUN_TIME, HIGH_ERROR, DIVERGE, + BOUNDARIES, } status; #endif diff --git a/2025.05.02/04Ex/Makefile b/2025.05.02/04Ex/Makefile new file mode 100644 index 0000000..fdc27cb --- /dev/null +++ b/2025.05.02/04Ex/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 = a04.$(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/04Ex/init_f.c b/2025.05.02/04Ex/init_f.c new file mode 100644 index 0000000..22eb5ea --- /dev/null +++ b/2025.05.02/04Ex/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/04Ex/init_f.h b/2025.05.02/04Ex/init_f.h new file mode 100644 index 0000000..ee5e8e8 --- /dev/null +++ b/2025.05.02/04Ex/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/04Ex/io_status.h b/2025.05.02/04Ex/io_status.h new file mode 100644 index 0000000..8867bee --- /dev/null +++ b/2025.05.02/04Ex/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/04Ex/main.c b/2025.05.02/04Ex/main.c new file mode 100644 index 0000000..7107d7c --- /dev/null +++ b/2025.05.02/04Ex/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 = 4; + status ret; + + 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 == 6) && + sscanf(argv[1], "%lf", &a) == 1 && + sscanf(argv[2], "%lf", &b) == 1 && + (a <= b) && + (sscanf(argv[3], "%lf", &eps) == 1 && (eps >= 0)) && + ((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 = t4_solve(f, a, b, eps, m, &x, &it); + 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; + } + + 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/04Ex/solve.c b/2025.05.02/04Ex/solve.c new file mode 100644 index 0000000..ab009e9 --- /dev/null +++ b/2025.05.02/04Ex/solve.c @@ -0,0 +1,62 @@ +#include "solve.h" +#include "status.h" + +#include +#include + +status t4_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 (fabs(y_a) - eps < 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; + } + + for (it = 0; it < m; ++it) + { + c = b - ((b - a) / (y_b - y_a)) * y_b; + y = f(c); + + if (fabs(y) - eps < DBL_EPSILON) + { + ret = SUCCESS; + break; + } else if (fabs(y_a) - fabs(y_b) > DBL_EPSILON) + { + a = c; + y_a = y; + } else if (fabs(y_b) - fabs(y_a) > DBL_EPSILON) + { + b = c; + y_b = y; + } else + { + ret = EQUAL; + break; + } + } + + if (it >= m) + ret = RUN_TIME; + + *x = c; + *m_it = it; + + return ret; +} + diff --git a/2025.05.02/04Ex/solve.h b/2025.05.02/04Ex/solve.h new file mode 100644 index 0000000..64af0c3 --- /dev/null +++ b/2025.05.02/04Ex/solve.h @@ -0,0 +1,12 @@ +#ifndef SOLVE_H +#define SOLVE_H + +#include "status.h" + +status t4_solve ( + double (*f) (double), + double a, double b, + double eps, int m, double *x, int *m_it + ); + +#endif diff --git a/2025.05.02/04Ex/status.h b/2025.05.02/04Ex/status.h new file mode 100644 index 0000000..b2a4d5e --- /dev/null +++ b/2025.05.02/04Ex/status.h @@ -0,0 +1,10 @@ +#ifndef STATUS_H +#define STATUS_H + +typedef enum _status { + SUCCESS, + RUN_TIME, + EQUAL, +} status; + +#endif diff --git a/2025.05.02/tests/find_str.sh b/2025.05.02/tests/find_str.sh new file mode 100755 index 0000000..42e2485 --- /dev/null +++ b/2025.05.02/tests/find_str.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +# Проверяем, передан ли файл как аргумент +if [ $# -eq 0 ]; then + echo "Использование: $0 <имя_файла>" + exit 1 +fi + +input_file="$1" + +# Проверяем существование файла +if [ ! -f "$input_file" ]; then + echo "Ошибка: файл '$input_file' не существует" + exit 1 +fi + +# Ищем строки с " Res = " и выводим их +grep " Res = " "$input_file" > found_str.log + diff --git a/2025.05.02/tests/must_find_test.sh b/2025.05.02/tests/must_find_test.sh index 2f09c06..c08711a 100755 --- a/2025.05.02/tests/must_find_test.sh +++ b/2025.05.02/tests/must_find_test.sh @@ -14,13 +14,13 @@ for (( k = 3 ; k < 7; k++ )); do for (( a = -100 ; a < -40 ; a++ )); do for (( b = -9 ; b < 10 ; b++ )); do echo "a = $a b = $b Iter = $iter ---" - sudo taskset -c $2 nice -n -20 ./a0$1.out "$(echo "$a / 10" | bc -l)" "$(echo "$b / 10" | bc -l)" 1e-16 $iter $k + ./a0$1.out "$(echo "$a / 10" | bc -l)" "$(echo "$b / 10" | bc -l)" 1e-16 $iter $k done done for (( a = -9 ; a < 10 ; a++ )); do for (( b = 11 ; b < 100 ; b++ )); do echo "a = $a b = $b Iter = $iter ---" - sudo taskset -c $2 nice -n -20 ./a0$1.out "$(echo "$a / 10" | bc -l)" "$(echo "$b / 10" | bc -l)" 1e-16 $iter $k + ./a0$1.out "$(echo "$a / 10" | bc -l)" "$(echo "$b / 10" | bc -l)" 1e-16 $iter $k done done done >$(pwd)/tests/out_$script_name.log 2>$(pwd)/tests/err_$script_name.log