Task 10 and 11 are done

This commit is contained in:
AZEN-SGG 2025-05-14 20:12:23 +03:00
parent ff1903ff80
commit c40059bcdc
19 changed files with 741 additions and 0 deletions

42
2025.05.09/10Ex/Makefile Normal file
View file

@ -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 = a10.$(EXE)
OBJ = main.o solve.o integral.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)

66
2025.05.09/10Ex/init_f.h Normal file
View file

@ -0,0 +1,66 @@
#ifndef INIT_F_H
#define INIT_F_H
#include <math.h>
static int cl = 0;
static inline int get_call_count (void)
{
return cl;
}
static inline double f0 (double x)
{
cl++;
(void)x;
return 1;
}
static inline double f1 (double x)
{
cl++;
return x + 1;
}
static inline double f2 (double x)
{
double x_2 = x * x;
cl++;
return x_2 + x + 1;
}
static inline double f3 (double x)
{
double x_2 = x * x;
double x_3 = x_2 * x;
cl++;
return x_3 + x_2 + x + 1;
}
static inline double f4 (double x)
{
double x_2 = x * x;
double x_3 = x_2 * x;
double x_4 = x_3 * x;
cl++;
return x_4 + x_3 + x_2 + x + 1;
}
static inline double f5 (double x)
{
cl++;
return exp(-x);
}
static inline double f6 (double x)
{
double x_2 = x * x;
cl++;
return 1 / (25 * x_2 + 1);
}
#endif

View file

@ -0,0 +1,47 @@
#include "integral.h"
#include <math.h>
#include <float.h>
#define MAX_ITER 30
int trapezoid (
double (*f) (double),
double a, double b,
double eps, double *res
) {
int it;
double n = 1;
double h = (b - a);
double integ_n = (f(a) + f(b)) * h*0.5;
for (it = 1; it <= MAX_ITER; ++it)
{
double x = a + h*0.5;
double integ_2n = 0;
for (int i = 0; i < n; i++)
{
integ_2n += f(x);
x += h;
}
h *= 0.5;
integ_2n = integ_2n * h + integ_n * 0.5;
if (fabs(integ_2n - integ_n) < eps)
break;
integ_n = integ_2n;
n *= 2;
}
if (it > MAX_ITER)
return -1;
*res = integ_n;
return n;
}

View file

@ -0,0 +1,10 @@
#ifndef INTEGRAL_H
#define INTEGRAL_H
int trapezoid (
double (*f) (double),
double a, double b,
double eps, double *res
);
#endif

42
2025.05.09/10Ex/main.c Normal file
View file

@ -0,0 +1,42 @@
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <float.h>
#include "init_f.h"
#include "solve.h"
/* ./a10.out a eps k */
int main (int argc, char *argv[])
{
double t, integral, a, b, eps;
int k, calls, task = 10;
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6};
int len_f = sizeof(f_lst) / sizeof(f_lst[0]);
if (
!((argc == 4) &&
sscanf(argv[1], "%lf", &a) == 1 &&
((sscanf(argv[2], "%lf", &eps) == 1) && (eps > 0)) &&
((sscanf(argv[3], "%d", &k) == 1) && ((0 <= k) && (k < len_f))))
) {
fprintf(stderr, "Usage: %s a eps k\n", argv[0]);
return -1;
}
t = clock();
b = t10_solve(f_lst[k], a, eps, &integral);
t = (clock() - t) / CLOCKS_PER_SEC;
calls = get_call_count();
if (b < 0) {
fprintf (stdout, "%s : Task = %d Method is not applicable Count = %d T = %.2f\n", argv[0], task, calls, t);
return -2;
} else {
fprintf (stdout, "%s : Task = %d Res = %e B = %e Count = %d T = %.2f\n", argv[0], task, integral, b, calls, t);
return 0;
}
}

46
2025.05.09/10Ex/solve.c Normal file
View file

@ -0,0 +1,46 @@
#include "solve.h"
#include "integral.h"
#include <math.h>
#include <float.h>
#define MAX_ITER 30
double t10_solve (
double (*f) (double),
double a, double eps,
double *res
) {
int it;
int h = 1;
double b = a;
double integ = 0;
for (it = 1; it <= MAX_ITER; ++it)
{
int n;
double ipart = 0;
b += h;
n = trapezoid(f, a, b, eps, &ipart);
if (n < 0)
return -2;
integ += ipart;
if (fabs(ipart) < eps)
break;
a = b;
h <<= 1;
}
if (it > MAX_ITER)
return -1;
*res = integ;
return b;
}

10
2025.05.09/10Ex/solve.h Normal file
View file

@ -0,0 +1,10 @@
#ifndef SOLVE_H
#define SOLVE_H
double t10_solve (
double (*f) (double),
double a, double eps,
double *res
);
#endif

42
2025.05.09/11Ex/Makefile Normal file
View file

@ -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 = a11.$(EXE)
OBJ = main.o solve.o integral.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)

66
2025.05.09/11Ex/init_f.h Normal file
View file

@ -0,0 +1,66 @@
#ifndef INIT_F_H
#define INIT_F_H
#include <math.h>
static int cl = 0;
static inline int get_call_count (void)
{
return cl;
}
static inline double f0 (double x)
{
cl++;
(void)x;
return 1;
}
static inline double f1 (double x)
{
cl++;
return x + 1;
}
static inline double f2 (double x)
{
double x_2 = x * x;
cl++;
return x_2 + x + 1;
}
static inline double f3 (double x)
{
double x_2 = x * x;
double x_3 = x_2 * x;
cl++;
return x_3 + x_2 + x + 1;
}
static inline double f4 (double x)
{
double x_2 = x * x;
double x_3 = x_2 * x;
double x_4 = x_3 * x;
cl++;
return x_4 + x_3 + x_2 + x + 1;
}
static inline double f5 (double x)
{
cl++;
return exp(-x);
}
static inline double f6 (double x)
{
double x_2 = x * x;
cl++;
return 1 / (25 * x_2 + 1);
}
#endif

View file

@ -0,0 +1,53 @@
#include "integral.h"
#include <math.h>
#include <float.h>
#define MAX_ITER 30
int simpson (
double (*f) (double),
double a, double b,
double eps, double *res
) {
int it;
double integ_n;
int n = 2;
double h = (b - a) * 0.5;
double s1 = (f(a) + f(b)) * h/3;
double s2 = f(a + h) * h*4/3;
for (it = 1; it <= MAX_ITER; ++it)
{
double x = a + h*0.5;
double s2_2n = 0;
for (int i = 0; i < n; i++)
{
s2_2n += f(x);
x += h;
}
s2_2n *= h*2/3;
if (fabs((s2_2n - s1 * 0.5) - s2 * 0.75) < eps) {
integ_n = s1 + s2;
break;
}
s1 = s1 * 0.5 + s2 * 0.25;
s2 = s2_2n;
h *= 0.5;
n <<= 1;
}
if (it > MAX_ITER)
return -1;
*res = integ_n;
return n;
}

View file

@ -0,0 +1,10 @@
#ifndef INTEGRAL_H
#define INTEGRAL_H
int simpson (
double (*f) (double),
double a, double b,
double eps, double *res
);
#endif

42
2025.05.09/11Ex/main.c Normal file
View file

@ -0,0 +1,42 @@
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <float.h>
#include "init_f.h"
#include "solve.h"
/* ./a11.out a eps k */
int main (int argc, char *argv[])
{
double t, integral, a, b, eps;
int k, calls, task = 11;
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6};
int len_f = sizeof(f_lst) / sizeof(f_lst[0]);
if (
!((argc == 4) &&
sscanf(argv[1], "%lf", &a) == 1 &&
((sscanf(argv[2], "%lf", &eps) == 1) && (eps > 0)) &&
((sscanf(argv[3], "%d", &k) == 1) && ((0 <= k) && (k < len_f))))
) {
fprintf(stderr, "Usage: %s a eps k\n", argv[0]);
return -1;
}
t = clock();
b = t11_solve(f_lst[k], a, eps, &integral);
t = (clock() - t) / CLOCKS_PER_SEC;
calls = get_call_count();
if (b < 0) {
fprintf (stdout, "%s : Task = %d Method is not applicable Count = %d T = %.2f\n", argv[0], task, calls, t);
return -2;
} else {
fprintf (stdout, "%s : Task = %d Res = %e B = %e Count = %d T = %.2f\n", argv[0], task, integral, b, calls, t);
return 0;
}
}

46
2025.05.09/11Ex/solve.c Normal file
View file

@ -0,0 +1,46 @@
#include "solve.h"
#include "integral.h"
#include <math.h>
#include <float.h>
#define MAX_ITER 30
double t11_solve (
double (*f) (double),
double a, double eps,
double *res
) {
int it;
int h = 1;
double b = a;
double integ = 0;
for (it = 1; it <= MAX_ITER; ++it)
{
int n;
double ipart = 0;
b += h;
n = simpson(f, a, b, eps, &ipart);
if (n < 0)
return -2;
integ += ipart;
if (fabs(ipart) < eps)
break;
a = b;
h <<= 1;
}
if (it > MAX_ITER)
return -1;
*res = integ;
return b;
}

10
2025.05.09/11Ex/solve.h Normal file
View file

@ -0,0 +1,10 @@
#ifndef SOLVE_H
#define SOLVE_H
double t11_solve (
double (*f) (double),
double a, double eps,
double *res
);
#endif

42
2025.05.09/12Ex/Makefile Normal file
View file

@ -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 = a08.$(EXE)
OBJ = main.o solve.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)

66
2025.05.09/12Ex/init_f.h Normal file
View file

@ -0,0 +1,66 @@
#ifndef INIT_F_H
#define INIT_F_H
#include <math.h>
static int cl = 0;
static inline int get_call_count (void)
{
return cl;
}
static inline double f0 (double x)
{
cl++;
(void)x;
return 1;
}
static inline double f1 (double x)
{
cl++;
return x + 1;
}
static inline double f2 (double x)
{
double x_2 = x * x;
cl++;
return x_2 + x + 1;
}
static inline double f3 (double x)
{
double x_2 = x * x;
double x_3 = x_2 * x;
cl++;
return x_3 + x_2 + x + 1;
}
static inline double f4 (double x)
{
double x_2 = x * x;
double x_3 = x_2 * x;
double x_4 = x_3 * x;
cl++;
return x_4 + x_3 + x_2 + x + 1;
}
static inline double f5 (double x)
{
cl++;
return exp(-x);
}
static inline double f6 (double x)
{
double x_2 = x * x;
cl++;
return 1 / (25 * x_2 + 1);
}
#endif

44
2025.05.09/12Ex/main.c Normal file
View file

@ -0,0 +1,44 @@
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <float.h>
#include "init_f.h"
#include "solve.h"
/* ./a08.out a b eps k */
int main (int argc, char *argv[])
{
double t, integral, a, b, eps;
int k, n, calls, task = 8;
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6};
int len_f = sizeof(f_lst) / sizeof(f_lst[0]);
if (
!((argc == 5) &&
sscanf(argv[1], "%lf", &a) == 1 &&
sscanf(argv[2], "%lf", &b) == 1 &&
((sscanf(argv[3], "%lf", &eps) == 1) && (eps > 0)) &&
((sscanf(argv[4], "%d", &k) == 1) && ((0 <= k) && (k < len_f))))
) {
fprintf(stderr, "Usage: %s a b eps k\n", argv[0]);
return -1;
}
t = clock();
n = t8_solve(f_lst[k], a, b, eps, &integral);
t = (clock() - t) / CLOCKS_PER_SEC;
calls = get_call_count();
if (n < 0) {
fprintf (stdout, "%s : Task = %d Method is not applicable Count = %d T = %.2f\n", argv[0], task, calls, t);
return -2;
} else {
fprintf (stdout, "%s : Task = %d Res = %e N = %d Count = %d T = %.2f\n", argv[0], task, integral, n, calls, t);
return 0;
}
}

47
2025.05.09/12Ex/solve.c Normal file
View file

@ -0,0 +1,47 @@
#include "solve.h"
#include <math.h>
#include <float.h>
#define MAX_ITER 30
int t8_solve (
double (*f) (double),
double a, double b,
double eps, double *res
) {
int it;
double n = 1;
double h = (b - a);
double integ_n = (f(a) + f(b)) * h*0.5;
for (it = 1; it <= MAX_ITER; ++it)
{
double x = a + h*0.5;
double integ_2n = 0;
for (int i = 0; i < n; i++)
{
integ_2n += f(x);
x += h;
}
h *= 0.5;
integ_2n = integ_2n * h + integ_n * 0.5;
if (fabs(integ_2n - integ_n) < eps)
break;
integ_n = integ_2n;
n *= 2;
}
if (it > MAX_ITER)
return -1;
*res = integ_n;
return n;
}

10
2025.05.09/12Ex/solve.h Normal file
View file

@ -0,0 +1,10 @@
#ifndef SOLVE_H
#define SOLVE_H
int t8_solve (
double (*f) (double),
double a, double b,
double eps, double *res
);
#endif