From 2efd819d0f2527ac4cd60263def6754936ab2a07 Mon Sep 17 00:00:00 2001 From: AZEN-SGG Date: Fri, 18 Apr 2025 00:41:17 +0300 Subject: [PATCH] Task 6 and 7 are done! --- 2025.04.18/05Ex/main.c | 4 +-- 2025.04.18/06Ex/Makefile | 42 +++++++++++++++++++++++++++++ 2025.04.18/06Ex/contin_func.c | 38 ++++++++++++++++++++++++++ 2025.04.18/06Ex/contin_func.h | 7 +++++ 2025.04.18/06Ex/io_status.h | 9 +++++++ 2025.04.18/06Ex/main.c | 34 +++++++++++++++++++++++ 2025.04.18/06Ex/solve.c | 39 +++++++++++++++++++++++++++ 2025.04.18/06Ex/solve.h | 6 +++++ 2025.04.18/07Ex/Makefile | 42 +++++++++++++++++++++++++++++ 2025.04.18/07Ex/contin_func.c | 21 +++++++++++++++ 2025.04.18/07Ex/contin_func.h | 6 +++++ 2025.04.18/07Ex/io_status.h | 9 +++++++ 2025.04.18/07Ex/main.c | 34 +++++++++++++++++++++++ 2025.04.18/07Ex/solve.c | 31 +++++++++++++++++++++ 2025.04.18/07Ex/solve.h | 6 +++++ 2025.04.18/dist/Ulyanov_MT/task07.c | 10 +++++-- 16 files changed, 334 insertions(+), 4 deletions(-) create mode 100644 2025.04.18/06Ex/Makefile create mode 100644 2025.04.18/06Ex/contin_func.c create mode 100644 2025.04.18/06Ex/contin_func.h create mode 100644 2025.04.18/06Ex/io_status.h create mode 100644 2025.04.18/06Ex/main.c create mode 100644 2025.04.18/06Ex/solve.c create mode 100644 2025.04.18/06Ex/solve.h create mode 100644 2025.04.18/07Ex/Makefile create mode 100644 2025.04.18/07Ex/contin_func.c create mode 100644 2025.04.18/07Ex/contin_func.h create mode 100644 2025.04.18/07Ex/io_status.h create mode 100644 2025.04.18/07Ex/main.c create mode 100644 2025.04.18/07Ex/solve.c create mode 100644 2025.04.18/07Ex/solve.h diff --git a/2025.04.18/05Ex/main.c b/2025.04.18/05Ex/main.c index 8edc338..a3e9cd8 100644 --- a/2025.04.18/05Ex/main.c +++ b/2025.04.18/05Ex/main.c @@ -14,8 +14,8 @@ int main(int argc, char *argv[]) if ( !((argc == 3) && - sscanf(argv[1], "%lf", &x) == 1 && - ((sscanf(argv[2], "%lf", &eps) == 1) && eps > 0)) + sscanf(argv[1], "%le", &x) == 1 && + ((sscanf(argv[2], "%le", &eps) == 1) && eps > 0)) ) { fprintf(stderr, "Usage: %s x eps\n", argv[0]); return 1; diff --git a/2025.04.18/06Ex/Makefile b/2025.04.18/06Ex/Makefile new file mode 100644 index 0000000..9198a33 --- /dev/null +++ b/2025.04.18/06Ex/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 + +CFLAGS = -std=gnu99 -mfpmath=sse -O3 + +ifeq ($(OS),Windows_NT) + EXE = exe + CLEAN = del + LFLAGS = -lssp -lm +else + EXE = out + CLEAN = rm -f + LFLAGS = -lm +endif + +TARGET = a06.$(EXE) +OBJ = main.o solve.o contin_func.o + +%.o: %.c + gcc $(CFLAGS) $(WFLAGS) -c $< -o $@ + +$(TARGET): $(OBJ) + gcc $^ -o $@ $(LFLAGS) + +# Отладочная сборка (gdb) +gdb: CFLAGS = -mfpmath=sse -std=gnu99 -g -O0 +gdb: clean $(TARGET) + +# Профилировочная сборка (gprof) +prof: CFLAGS = -std=gnu99 -mfpmath=sse -pg -O3 +prof: LFLAGS = -lm -pg +prof: clean $(TARGET) + +clean: + $(CLEAN) *.o *$(EXE) diff --git a/2025.04.18/06Ex/contin_func.c b/2025.04.18/06Ex/contin_func.c new file mode 100644 index 0000000..4267921 --- /dev/null +++ b/2025.04.18/06Ex/contin_func.c @@ -0,0 +1,38 @@ +#include "contin_func.h" + +#include +#include + +double ssin (const double x, const double eps) +{ + double value = 0; + double el = x; + int i = 1; + + while (fabs(el) - eps >= DBL_EPSILON) + { + value += el; + + el *= -(x*x / (i*i + 3*i + 2)); + i+=2; + } + + return value; +} + +double scos (const double x, const double eps) +{ + double value = 0; + double el = 1; + int i = 0; + + while (fabs(el) - eps >= DBL_EPSILON) + { + value += el; + + el *= - (x*x / (i*i + 3*i + 2)); + i+=2; + } + + return value; +} diff --git a/2025.04.18/06Ex/contin_func.h b/2025.04.18/06Ex/contin_func.h new file mode 100644 index 0000000..404674a --- /dev/null +++ b/2025.04.18/06Ex/contin_func.h @@ -0,0 +1,7 @@ +#ifndef CONTIN_FUNC_H +#define CONTIN_FUNC_H + +double ssin (const double x, const double eps); +double scos (const double x, const double eps); + +#endif diff --git a/2025.04.18/06Ex/io_status.h b/2025.04.18/06Ex/io_status.h new file mode 100644 index 0000000..4ab1d29 --- /dev/null +++ b/2025.04.18/06Ex/io_status.h @@ -0,0 +1,9 @@ +#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!" + +#endif diff --git a/2025.04.18/06Ex/main.c b/2025.04.18/06Ex/main.c new file mode 100644 index 0000000..bf43037 --- /dev/null +++ b/2025.04.18/06Ex/main.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include + +#include "io_status.h" +#include "solve.h" + +/* ./a.out x eps */ +int main(int argc, char *argv[]) +{ + double x, eps, t, r1 = 0, r2 = 0; + int task = 6; + + if ( + !((argc == 3) && + sscanf(argv[1], "%le", &x) == 1 && + ((sscanf(argv[2], "%le", &eps) == 1) && eps > 0)) + ) { + fprintf(stderr, "Usage: %s x eps\n", argv[0]); + return 1; + } + + t = clock(); + r1 = fcos(x, eps); + t = (clock() - t) / CLOCKS_PER_SEC; + + r2 = fabs(r1 - cos(x)); + + printf("%s : Task = %d Result = %e Residual = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t); + + return 0; +} + diff --git a/2025.04.18/06Ex/solve.c b/2025.04.18/06Ex/solve.c new file mode 100644 index 0000000..06b36dd --- /dev/null +++ b/2025.04.18/06Ex/solve.c @@ -0,0 +1,39 @@ +#include "solve.h" +#include "contin_func.h" + +#include +#include +#include + +double fcos (double x, const double eps) +{ + double answer; + short flag = 1; + + if (x - M_PI*2 >= DBL_EPSILON) + x = fmod(x, M_PI*2); + + if (x - M_PI >= DBL_EPSILON) + { + x -= M_PI; + flag *= -1; + } + + if (x - 1 >= DBL_EPSILON) + { + x -= M_PI_2; + flag *= -2; + } + + if (abs(flag) == 2) + { + x /= 2; + + answer = ((flag > 0) - (flag < 0)) * (2 * ssin(x, eps) * scos(x, eps)); + } else + answer = flag * scos(x, eps); + + return answer; +} + + diff --git a/2025.04.18/06Ex/solve.h b/2025.04.18/06Ex/solve.h new file mode 100644 index 0000000..18245c9 --- /dev/null +++ b/2025.04.18/06Ex/solve.h @@ -0,0 +1,6 @@ +#ifndef SOLVE_H +#define SOLVE_H + +double fcos (double x, const double eps); + +#endif diff --git a/2025.04.18/07Ex/Makefile b/2025.04.18/07Ex/Makefile new file mode 100644 index 0000000..67ad48e --- /dev/null +++ b/2025.04.18/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 + +CFLAGS = -std=gnu99 -mfpmath=sse -O3 + +ifeq ($(OS),Windows_NT) + EXE = exe + CLEAN = del + LFLAGS = -lssp -lm +else + EXE = out + CLEAN = rm -f + LFLAGS = -lm +endif + +TARGET = a07.$(EXE) +OBJ = main.o solve.o contin_func.o + +%.o: %.c + gcc $(CFLAGS) $(WFLAGS) -c $< -o $@ + +$(TARGET): $(OBJ) + gcc $^ -o $@ $(LFLAGS) + +# Отладочная сборка (gdb) +gdb: CFLAGS = -mfpmath=sse -std=gnu99 -g -O0 +gdb: clean $(TARGET) + +# Профилировочная сборка (gprof) +prof: CFLAGS = -std=gnu99 -mfpmath=sse -pg -O3 +prof: LFLAGS = -lm -pg +prof: clean $(TARGET) + +clean: + $(CLEAN) *.o *$(EXE) diff --git a/2025.04.18/07Ex/contin_func.c b/2025.04.18/07Ex/contin_func.c new file mode 100644 index 0000000..a19c48e --- /dev/null +++ b/2025.04.18/07Ex/contin_func.c @@ -0,0 +1,21 @@ +#include "contin_func.h" + +#include +#include + +double sexp (const double x, const double eps) +{ + double value = 1; + double monom = x; + int i = 1; + + while (monom - eps >= DBL_EPSILON) + { + value += monom; + + i++; + monom *= x/i; + } + + return value; +} diff --git a/2025.04.18/07Ex/contin_func.h b/2025.04.18/07Ex/contin_func.h new file mode 100644 index 0000000..c73ced6 --- /dev/null +++ b/2025.04.18/07Ex/contin_func.h @@ -0,0 +1,6 @@ +#ifndef CONTIN_FUNC_H +#define CONTIN_FUNC_H + +double sexp (const double x, const double eps); + +#endif diff --git a/2025.04.18/07Ex/io_status.h b/2025.04.18/07Ex/io_status.h new file mode 100644 index 0000000..4ab1d29 --- /dev/null +++ b/2025.04.18/07Ex/io_status.h @@ -0,0 +1,9 @@ +#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!" + +#endif diff --git a/2025.04.18/07Ex/main.c b/2025.04.18/07Ex/main.c new file mode 100644 index 0000000..040df9e --- /dev/null +++ b/2025.04.18/07Ex/main.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include + +#include "io_status.h" +#include "solve.h" + +/* ./a.out x eps */ +int main(int argc, char *argv[]) +{ + double x, eps, t, r1 = 0, r2 = 0; + int task = 7; + + if ( + !((argc == 3) && + sscanf(argv[1], "%le", &x) == 1 && + ((sscanf(argv[2], "%le", &eps) == 1) && eps > 0)) + ) { + fprintf(stderr, "Usage: %s x eps\n", argv[0]); + return 1; + } + + t = clock(); + r1 = fexp(x, eps); + t = (clock() - t) / CLOCKS_PER_SEC; + + r2 = fabs(r1 - exp(x)); + + printf("%s : Task = %d Result = %e Residual = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t); + + return 0; +} + diff --git a/2025.04.18/07Ex/solve.c b/2025.04.18/07Ex/solve.c new file mode 100644 index 0000000..6494f0a --- /dev/null +++ b/2025.04.18/07Ex/solve.c @@ -0,0 +1,31 @@ +#include "solve.h" +#include "contin_func.h" + +#include +#include +#include +#include + +double fexp (double x, const double eps) +{ + double integral, fractal, answer = 1; + bool is_negative = false; + + if (x < DBL_EPSILON) + { + is_negative = true; + x = -x; + } + + integral = floor(x); + fractal = x - integral; + + for (double i = 0; (integral - i) > DBL_EPSILON; ++i) + answer *= M_E; + + answer *= sexp(fractal, eps); + if (is_negative) + answer = 1. / answer; + + return answer; +} diff --git a/2025.04.18/07Ex/solve.h b/2025.04.18/07Ex/solve.h new file mode 100644 index 0000000..880e0f5 --- /dev/null +++ b/2025.04.18/07Ex/solve.h @@ -0,0 +1,6 @@ +#ifndef SOLVE_H +#define SOLVE_H + +double fexp (double x, const double eps); + +#endif diff --git a/2025.04.18/dist/Ulyanov_MT/task07.c b/2025.04.18/dist/Ulyanov_MT/task07.c index a5e7578..f2e8812 100644 --- a/2025.04.18/dist/Ulyanov_MT/task07.c +++ b/2025.04.18/dist/Ulyanov_MT/task07.c @@ -30,8 +30,13 @@ int main(int argc, char* argv[]) double solve7(double x, double eps) { - int i = 1, intg; + int sign = 1, i = 1, intg; double mult = 1, el = 1, res = 0; + if (x < 0) + { + sign = -1; + x = fabs(x); + } intg = floor(x); x = x - intg; while (fabs(el) >= eps) @@ -44,5 +49,6 @@ double solve7(double x, double eps) { mult *= M_E; } - return mult * res; + if (sign == 1) return mult * res; + else return 1 / (mult * res); }