Task 9 is done
This commit is contained in:
parent
a8b4e4358d
commit
cdc405551d
11 changed files with 328 additions and 1 deletions
42
2025.05.02/09Ex/Makefile
Normal file
42
2025.05.02/09Ex/Makefile
Normal 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 = a09.$(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)
|
61
2025.05.02/09Ex/init_f.c
Normal file
61
2025.05.02/09Ex/init_f.c
Normal file
|
@ -0,0 +1,61 @@
|
|||
#include "init_f.h"
|
||||
|
||||
#include <math.h>
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
13
2025.05.02/09Ex/init_f.h
Normal file
13
2025.05.02/09Ex/init_f.h
Normal file
|
@ -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
|
49
2025.05.02/09Ex/main.c
Normal file
49
2025.05.02/09Ex/main.c
Normal file
|
@ -0,0 +1,49 @@
|
|||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "init_f.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, it, cl, task = 9;
|
||||
|
||||
double (*f) (double);
|
||||
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6};
|
||||
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();
|
||||
it = t9_solve(f, a, b, eps, m, &x);
|
||||
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||
|
||||
cl = get_call_count();
|
||||
|
||||
if (it < 0)
|
||||
{
|
||||
fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t);
|
||||
return -2;
|
||||
} else
|
||||
{
|
||||
fprintf(stdout, "%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;
|
||||
}
|
||||
}
|
||||
|
51
2025.05.02/09Ex/solve.c
Normal file
51
2025.05.02/09Ex/solve.c
Normal file
|
@ -0,0 +1,51 @@
|
|||
#include "solve.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <float.h>
|
||||
|
||||
int t9_solve (
|
||||
double (*f) (double),
|
||||
double a, double b,
|
||||
double eps, int m, double *x
|
||||
) {
|
||||
const double gdrt = 2 / (1 + sqrt(5));
|
||||
int it = 0;
|
||||
|
||||
double x_1 = b + (a - b) * gdrt, x_2 = a + (b - a) * gdrt;
|
||||
double y_1 = f(x_1), y_2 = f(x_2);
|
||||
|
||||
for (it = 1; it <= m; ++it)
|
||||
{
|
||||
if (y_1 - y_2 > DBL_EPSILON)
|
||||
{
|
||||
b = x_2;
|
||||
|
||||
x_2 = x_1;
|
||||
y_2 = y_1;
|
||||
|
||||
x_1 = b + (a - b) * gdrt;
|
||||
y_1 = f(x_1);
|
||||
} else
|
||||
{
|
||||
a = x_1;
|
||||
|
||||
x_1 = x_2;
|
||||
y_1 = y_2;
|
||||
|
||||
x_2 = a + (b - a) * gdrt;
|
||||
y_2 = f(x_2);
|
||||
}
|
||||
|
||||
if (fabs(b - a) < eps)
|
||||
{
|
||||
*x = x_1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (it > m)
|
||||
it = -1;
|
||||
|
||||
return it;
|
||||
}
|
||||
|
10
2025.05.02/09Ex/solve.h
Normal file
10
2025.05.02/09Ex/solve.h
Normal file
|
@ -0,0 +1,10 @@
|
|||
#ifndef SOLVE_H
|
||||
#define SOLVE_H
|
||||
|
||||
int t9_solve (
|
||||
double (*f) (double),
|
||||
double a, double b,
|
||||
double eps, int m, double *x
|
||||
);
|
||||
|
||||
#endif
|
Binary file not shown.
2
2025.05.02/dist/Krivoruchenko_SK/Makefile
vendored
2
2025.05.02/dist/Krivoruchenko_SK/Makefile
vendored
|
@ -5,7 +5,7 @@ FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pe
|
|||
%.o: %.c
|
||||
gcc -c $(FLAGS) $<
|
||||
|
||||
all: a01.out a02.out a03.out a04.out a07.out a08.out
|
||||
all: a01.out a02.out a03.out a04.out a07.out a08.out a09.out
|
||||
|
||||
solve.o: solve.c solve.h
|
||||
init_f.o: init_f.c init_f.h
|
||||
|
|
49
2025.05.02/dist/Krivoruchenko_SK/a09.c
vendored
Normal file
49
2025.05.02/dist/Krivoruchenko_SK/a09.c
vendored
Normal file
|
@ -0,0 +1,49 @@
|
|||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "init_f.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, it, cl, task = 9;
|
||||
|
||||
double (*f) (double);
|
||||
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6};
|
||||
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();
|
||||
it = t9_solve(f, a, b, eps, m, &x);
|
||||
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||
|
||||
cl = get_call_count();
|
||||
|
||||
if (it < 0)
|
||||
{
|
||||
fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t);
|
||||
return -2;
|
||||
} else
|
||||
{
|
||||
fprintf(stdout, "%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;
|
||||
}
|
||||
}
|
||||
|
46
2025.05.02/dist/Krivoruchenko_SK/solve.c
vendored
46
2025.05.02/dist/Krivoruchenko_SK/solve.c
vendored
|
@ -293,3 +293,49 @@ int t8_solve (
|
|||
|
||||
return it;
|
||||
}
|
||||
|
||||
int t9_solve (
|
||||
double (*f) (double),
|
||||
double a, double b,
|
||||
double eps, int m, double *x
|
||||
) {
|
||||
const double gdrt = 2 / (1 + sqrt(5));
|
||||
int it = 0;
|
||||
|
||||
double x_1 = b + (a - b) * gdrt, x_2 = a + (b - a) * gdrt;
|
||||
double y_1 = f(x_1), y_2 = f(x_2);
|
||||
|
||||
for (it = 1; it <= m; ++it)
|
||||
{
|
||||
if (y_1 - y_2 > DBL_EPSILON)
|
||||
{
|
||||
b = x_2;
|
||||
|
||||
x_2 = x_1;
|
||||
y_2 = y_1;
|
||||
|
||||
x_1 = b + (a - b) * gdrt;
|
||||
y_1 = f(x_1);
|
||||
} else
|
||||
{
|
||||
a = x_1;
|
||||
|
||||
x_1 = x_2;
|
||||
y_1 = y_2;
|
||||
|
||||
x_2 = a + (b - a) * gdrt;
|
||||
y_2 = f(x_2);
|
||||
}
|
||||
|
||||
if (fabs(b - a) < eps)
|
||||
{
|
||||
*x = x_1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (it > m)
|
||||
it = -1;
|
||||
|
||||
return it;
|
||||
}
|
||||
|
|
6
2025.05.02/dist/Krivoruchenko_SK/solve.h
vendored
6
2025.05.02/dist/Krivoruchenko_SK/solve.h
vendored
|
@ -38,4 +38,10 @@ int t8_solve (
|
|||
double eps, int m, double *x
|
||||
);
|
||||
|
||||
int t9_solve (
|
||||
double (*f) (double),
|
||||
double a, double b,
|
||||
double eps, int m, double *x
|
||||
);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue