This commit is contained in:
AZEN-SGG 2025-05-07 19:27:45 +03:00
commit 7dc4921cef
58 changed files with 2187 additions and 104 deletions

View file

@ -1,16 +0,0 @@
#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

View file

@ -12,8 +12,8 @@ int main(int argc, char *argv[])
int m, k, cl, it, task = 2; int m, k, cl, it, task = 2;
double (*f) (double); double (*f) (double);
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6, sin}; // TODO: Remove sin double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6};
double (*d_lst[]) (double) = {d0, d1, d2, d3, d4, d5, d6, cos}; // TODO: Remove cos double (*d_lst[]) (double) = {d0, d1, d2, d3, d4, d5, d6};
int len_f = sizeof(f_lst) / sizeof(f_lst[0]); int len_f = sizeof(f_lst) / sizeof(f_lst[0]);
@ -36,13 +36,13 @@ int main(int argc, char *argv[])
cl = get_call_function_count(); cl = get_call_function_count();
if (it > m) if (it < 0)
{ {
fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t); fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t);
return -2; return -2;
} else } 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); 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; return 0;
} }
} }

View file

@ -21,12 +21,15 @@ int t2_solve (
if (fabs(dy) < DBL_EPSILON) if (fabs(dy) < DBL_EPSILON)
{ {
it = m+1; it = -1;
break; break;
} }
x_0 -= (y / dy); x_0 -= (y / dy);
} }
if (it > m)
it = -1;
*x = x_0; *x = x_0;
return it; return it;

42
2025.05.02/06Ex/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 = a06.$(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/06Ex/init_f.c Normal file
View 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/06Ex/init_f.h Normal file
View 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/06Ex/main.c Normal file
View 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, cl, it, task = 1;
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 = t1_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
{
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;
}
}

103
2025.05.02/06Ex/solve.c Normal file
View file

@ -0,0 +1,103 @@
#include "solve.h"
#include <math.h>
#include <float.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
int t1_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
) {
int it = 0;
uint64_t bits;
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)
{
*x = a;
return 1;
} if (fabs(y_b) - eps < DBL_EPSILON)
{
*x = b;
return 1;
}
if (sgn_a == sgn_b)
{
*x = DBL_MAX;
return -1;
}
for (it = 1; it <= m; ++it)
{
c = (a + b) * 0.5;
y = f(c);
memcpy(&bits, &y, sizeof(bits));
sgn_c = (bits >> 63) & 1;
if (fabs(y) - eps < DBL_EPSILON)
break;
else if ((fabs(c - a) < DBL_EPSILON) || (fabs(c - b) < DBL_EPSILON))
it = m+1;
else if (sgn_c == sgn_a)
{
a = c;
y_a = y;
} else if (sgn_c == sgn_b)
{
b = c;
y_b = y;
}
}
if (it > m)
it = -1;
*x = c;
return it;
}
int t6_solve (
double (*f) (double),
const int m, double *d
double a, double b,
const double eps, const int M, double *x
) {
const int len = m + 1;
double *y_lst = d;
double *x_lst = d + len;
double *t_lst = d + (len << 1);
double last_x = a;
double last_y = f(a);
const int h = (b - a) / m;
y_lst[0] = last_y;
x_lst[0] = last_x;
t_lst[0] = last_x;
for (int i = 1, double t_x = a ; i < len ; i++, t_x += h)
{
double t_y = f(t_x);
y_lst[i] = t_y;
x_lst[i] = t_x;
}
}

10
2025.05.02/06Ex/solve.h Normal file
View file

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

View file

@ -2,8 +2,6 @@
#include <math.h> #include <math.h>
#include <float.h> #include <float.h>
#include <stdbool.h>
int t8_solve ( int t8_solve (
double (*f) (double), double (*f) (double),

View file

@ -3,18 +3,16 @@
#include <math.h> #include <math.h>
#include "init_f.h" #include "init_f.h"
#include "status.h"
#include "solve.h" #include "solve.h"
/* ./a.out a b eps M k */ /* ./a.out a b eps M k */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
double t, a, b, eps, x = 0; double t, a, b, eps, x = 0;
int m, k, cl, it = 0, task = 1; int m, k, it, cl, task = 9;
status ret;
double (*f) (double); double (*f) (double);
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6, sin}; // TODO: Remove sin double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6};
int len_f = sizeof(f_lst) / sizeof(f_lst[0]); int len_f = sizeof(f_lst) / sizeof(f_lst[0]);
if ( if (
@ -22,45 +20,30 @@ int main(int argc, char *argv[])
sscanf(argv[1], "%lf", &a) == 1 && sscanf(argv[1], "%lf", &a) == 1 &&
sscanf(argv[2], "%lf", &b) == 1 && sscanf(argv[2], "%lf", &b) == 1 &&
(a <= b) && (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[4], "%d", &m) == 1) && m > 0) &&
((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f)))) ((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f))))
) { ) {
fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]); fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]);
return 1; return -1;
} }
f = f_lst[k]; f = f_lst[k];
t = clock(); t = clock();
ret = t1_solve(f, a, b, eps, m, &x, &it); it = t9_solve(f, a, b, eps, m, &x);
t = (clock() - t) / CLOCKS_PER_SEC; t = (clock() - t) / CLOCKS_PER_SEC;
cl = get_call_count(); cl = get_call_count();
if (it < 0)
do { {
switch (ret)
{
case SUCCESS:
continue;
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);
break;
}
fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t); fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t);
return ret; return -2;
} while (0); } 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); 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;
return 0; }
} }

View file

@ -1,68 +1,51 @@
#include "solve.h" #include "solve.h"
#include "status.h"
#include <math.h> #include <math.h>
#include <float.h> #include <float.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
int t9_solve ( int t9_solve (
double (*f) (double), double (*f) (double),
double a, double b, double a, double b,
double eps, int m, double *x double eps, int m, double *x
) { ) {
const double gdrt = 2 / (1 + sqrt(5));
int it = 0; int it = 0;
double c = DBL_MAX, y, y_a = f(a), y_b = f(b);
if (fabs(y_a) - eps < DBL_EPSILON) double x_1 = b + (a - b) * gdrt, x_2 = a + (b - a) * gdrt;
{ double y_1 = f(x_1), y_2 = f(x_2);
*x = a;
return SUCCESS;
} if (fabs(y_b) - eps < DBL_EPSILON)
{
*x = b;
return SUCCESS;
}
if (sgn_a == sgn_b) for (it = 1; it <= m; ++it)
{ {
*x = DBL_MAX; if (y_1 - y_2 > DBL_EPSILON)
return MORE_ONE_ROOT;
}
for (it = 0; it < m; ++it)
{
c = (a + b) * 0.5;
y = f(c);
memcpy(&bits, &y, sizeof(bits));
sgn_c = (bits >> 63) & 1;
if (fabs(y) - eps < DBL_EPSILON)
{ {
ret = SUCCESS; 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; 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;
} else if (sgn_c == sgn_b)
{
b = c;
y_b = y;
} }
} }
if (it >= m)
ret = RUN_TIME;
*x = c;
return ret; if (it > m)
it = -1;
return it;
} }

View file

@ -1,12 +1,10 @@
#ifndef SOLVE_H #ifndef SOLVE_H
#define SOLVE_H #define SOLVE_H
#include "status.h" int t9_solve (
status t1_solve (
double (*f) (double), double (*f) (double),
double a, double b, double a, double b,
double eps, int m, double *x, int *m_it double eps, int m, double *x
); );
#endif #endif

Binary file not shown.

View file

@ -0,0 +1,14 @@
FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -O3
%.out: %.o solve.o init_f.o
gcc $(FLAGS) $^ -o $@ -lm
%.o: %.c
gcc -c $(FLAGS) $<
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
clean:
rm -f *.o *.out

49
2025.05.02/dist/Krivoruchenko_SK/a01.c vendored Normal file
View 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, cl, it, task = 1;
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 = t1_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
{
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;
}
}

49
2025.05.02/dist/Krivoruchenko_SK/a02.c vendored Normal file
View file

@ -0,0 +1,49 @@
#include <stdio.h>
#include <time.h>
#include <math.h>
#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 = 2;
double (*f) (double);
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6};
double (*d_lst[]) (double) = {d0, d1, d2, d3, d4, d5, d6};
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 = t2_solve(f, d_lst[k], x_0, eps, m, &x);
t = (clock() - t) / CLOCKS_PER_SEC;
cl = get_call_function_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;
}
}

49
2025.05.02/dist/Krivoruchenko_SK/a03.c vendored Normal file
View 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, cl, it, task = 3;
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 = t3_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;
}
}

49
2025.05.02/dist/Krivoruchenko_SK/a04.c vendored Normal file
View 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, cl, it, task = 4;
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 = t4_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
{
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;
}
}

47
2025.05.02/dist/Krivoruchenko_SK/a07.c vendored Normal file
View file

@ -0,0 +1,47 @@
#include <stdio.h>
#include <time.h>
#include <math.h>
#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};
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 < 0)
{
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;
}
}

49
2025.05.02/dist/Krivoruchenko_SK/a08.c vendored Normal file
View 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 = 8;
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 = t8_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;
}
}

49
2025.05.02/dist/Krivoruchenko_SK/a09.c vendored Normal file
View 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;
}
}

View file

@ -0,0 +1,128 @@
#include "init_f.h"
#include <math.h>
#include <float.h>
static int cl_f = 0;
static int cl_d = 0;
int get_call_count (void)
{
return cl_f;
}
int get_call_function_count (void)
{
return cl_f;
}
int get_call_derivative_count (void)
{
return cl_d;
}
double f0 (double x)
{
cl_f++;
(void)x;
return 1;
}
double d0 (double x)
{
cl_d++;
(void)x;
return 0;
}
double f1 (double x)
{
cl_f++;
return ((x * 1e-100) - 1.0) * 1e100;
}
double d1 (double x)
{
(void)x;
cl_d++;
return 1;
}
double f2 (double x)
{
cl_f++;
return 4 - x * x;
}
double d2 (double x)
{
cl_d++;
return -2 * x;
}
double f3 (double x)
{
double x_2 = x * x;
cl_f++;
return x * x_2 + 3 * x_2 + 16;
}
double d3 (double x)
{
cl_d++;
return 3 * x * x + 6 * x;
}
double f4 (double x)
{
double x_2 = x * x;
cl_f++;
return 3 - 2 * x_2 - x_2 * x_2;
}
double d4 (double x)
{
cl_d++;
return -4 * x - 4 * ((x * x) * x);
}
double f5 (double x)
{
cl_f++;
return sqrt(fabs(x) + 1) - 2;
}
double d5 (double x)
{
cl_d++;
if (x < -DBL_EPSILON)
return (-1.0 / (sqrt(fabs(x) + 1))) * 0.5;
else if (x > DBL_EPSILON)
return (1.0 / (sqrt(fabs(x) + 1))) * 0.5;
return 0;
}
double f6 (double x)
{
cl_f++;
return sqrt(sqrt(fabs(x) + 1) + 1) - 2;
}
double d6 (double x)
{
double sqrt_x = sqrt(fabs(x) + 1);
cl_d++;
if (x < -DBL_EPSILON)
return (-1.0 / (sqrt(sqrt_x + 1) * sqrt_x)) * 0.25;
else if (x > DBL_EPSILON)
return (1.0 / (sqrt(sqrt_x + 1) * sqrt_x)) * 0.25;
return 0;
}

View file

@ -0,0 +1,29 @@
#ifndef INIT_F_H
#define INIT_F_H
int get_call_count (void);
int get_call_function_count (void) ;
int get_call_derivative_count (void);
double f0 (double x);
double d0 (double x);
double f1 (double x);
double d1 (double x);
double f2 (double x);
double d2 (double x);
double f3 (double x);
double d3 (double x);
double f4 (double x);
double d4 (double x);
double f5 (double x);
double d5 (double x);
double f6 (double x);
double d6 (double x);
#endif

341
2025.05.02/dist/Krivoruchenko_SK/solve.c vendored Normal file
View file

@ -0,0 +1,341 @@
#include "solve.h"
#include <math.h>
#include <float.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
int t1_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
) {
int it = 0;
uint64_t bits;
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)
{
*x = a;
return 1;
} if (fabs(y_b) - eps < DBL_EPSILON)
{
*x = b;
return 1;
}
if (sgn_a == sgn_b)
{
*x = DBL_MAX;
return -1;
}
for (it = 1; it <= m; ++it)
{
c = (a + b) * 0.5;
y = f(c);
memcpy(&bits, &y, sizeof(bits));
sgn_c = (bits >> 63) & 1;
if (fabs(y) - eps < DBL_EPSILON)
break;
else if ((fabs(c - a) < DBL_EPSILON) || (fabs(c - b) < DBL_EPSILON))
it = m+1;
else if (sgn_c == sgn_a)
{
a = c;
y_a = y;
} else if (sgn_c == sgn_b)
{
b = c;
y_b = y;
}
}
if (it > m)
it = -1;
*x = c;
return it;
}
int t2_solve (
double (*f) (double),
double (*d) (double),
double x_0, double eps,
int m, double *x
) {
int it = 0;
for (it = 1; it <= m; ++it)
{
double y = f(x_0);
double dy = d(x_0);
if (fabs(y) - eps < DBL_EPSILON)
break;
if (fabs(dy) < DBL_EPSILON)
{
it = -1;
break;
}
x_0 -= (y / dy);
}
if (it > m)
it = -1;
*x = x_0;
return it;
}
int t3_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
) {
int it = 0;
uint64_t bits;
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)
{
*x = a;
return 1;
} if (fabs(y_b) - eps < DBL_EPSILON)
{
*x = b;
return 1;
}
if (sgn_a == sgn_b)
{
*x = DBL_MAX;
return -1;
}
for (it = 1; it <= m; ++it)
{
c = a - ((a - b) / (y_a - y_b)) * y_a;
y = f(c);
memcpy(&bits, &y, sizeof(bits));
sgn_c = (bits >> 63) & 1;
if ((c - a < DBL_EPSILON) || (c - b > DBL_EPSILON))
it = m+1;
else if (fabs(y) - eps < DBL_EPSILON)
break;
else if (sgn_c == sgn_a)
{
a = c;
y_a = y;
} else if (sgn_c == sgn_b)
{
b = c;
y_b = y;
}
}
if (it > m)
it = -1;
*x = c;
return it;
}
int t4_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
) {
int it = 0;
double c = DBL_MAX, y, y_a = f(a), y_b = f(b);
if (fabs(y_a) - eps < DBL_EPSILON)
{
*x = a;
return 1;
} if (fabs(y_b) - eps < DBL_EPSILON)
{
*x = b;
return 1;
} if (fabs(fabs(y_b) - fabs(y_a)) < DBL_EPSILON)
{
*x = a;
return -1;
}
for (it = 1; it <= m; ++it)
{
c = b - ((b - a) / (y_b - y_a)) * y_b;
y = f(c);
if (fabs(y) - eps < DBL_EPSILON)
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
it = m+1;
}
if (it > m)
it = -1;
*x = c;
return it;
}
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 = 1; it <= m; ++it)
{
x_0 = y;
y = f(x_0);
if (fabs(y - x_0) - eps < DBL_EPSILON)
break;
}
if (it > m)
it = -1;
*x = x_0;
return it;
}
int t8_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
) {
int it = 1;
double x_l = 0, y_l = 0, c = a, y = f(a);
if (fabs(b - a) < DBL_EPSILON)
{
*x = a;
return 1;
}
for (double h = (b - a) * 0.1; fabs(h) > DBL_EPSILON; h *= -0.1)
{
do {
if (it > m)
break;
it++;
x_l = c;
y_l = y;
c += h;
y = f(c);
} while (((y - y_l) - eps) > DBL_EPSILON);
if (it > m)
{
it = -1;
break;
}
if ((c - b) > DBL_EPSILON)
{
x_l = b;
y_l = f(b);
break;
} else if ((a - c) > DBL_EPSILON)
{
x_l = a;
y_l = f(b);
break;
}
}
*x = x_l;
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;
}

View file

@ -0,0 +1,47 @@
#ifndef SOLVE_H
#define SOLVE_H
int t1_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
);
int t2_solve (
double (*f) (double),
double (*d) (double),
double x_0, double eps,
int m, double *x
);
int t3_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
);
int t4_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
);
int t7_solve (
double (*f) (double),
double x_0, double eps,
int m, double *x
);
int t8_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
);
int t9_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
);
#endif

View file

@ -0,0 +1,18 @@
script_name="$(basename "$0")"
iter="1000"
mkdir -p tests
if [ "$#" -ne 2 ]; then
echo "The number of parameters is less than 2"
exit 1
fi
make
for (( k = 3 ; k < 7; k++ )); do
echo "------- K = $k -------"
for (( a = -100 ; a < 100 ; a++ )); do
echo "k = $k x_0 = "$(echo "$a / 10" | bc -l)" Iter = $iter ---"
./a0$1.out "$(echo "$a / 10" | bc -l)" 1e-16 $iter $k
done
done >$(pwd)/tests/out_$script_name.log 2>$(pwd)/tests/err_$script_name.log

42
2025.05.09/01Ex/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 = a01.$(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.09/01Ex/init_f.c Normal file
View 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 + 1;
}
double f2 (double x)
{
cl++;
return x * x + x + 1;
}
double f3 (double x)
{
double x_2 = x * x;
cl++;
return x * x_2 + x_2 + x + 1;
}
double f4 (double x)
{
double x_2 = x * x;
double x_3 = x_2 * x;
cl++;
return x * x_3 + x_3 + x_2 + x + 1;
}
double f5 (double x)
{
cl++;
return exp(-x);
}
double f6 (double x)
{
cl++;
return 1 / (25 * x * x + 1);
}

13
2025.05.09/01Ex/init_f.h Normal file
View 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

45
2025.05.09/01Ex/main.c Normal file
View file

@ -0,0 +1,45 @@
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <float.h>
#include "init_f.h"
#include "solve.h"
/* ./a01.out x h k */
int main (int argc, char *argv[])
{
double t, x, h, d;
int k, cl, task = 1;
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", &x) == 1 &&
((sscanf(argv[2], "%lf", &h) == 1) && (h > 0)) &&
((sscanf(argv[3], "%d", &k) == 1) && ((0 <= k) && (k <= len_f))))
) {
fprintf(stderr, "Usage: %s x h k\n", argv[0]);
return -1;
}
t = clock();
d = t1_solve(f_lst[k], x, h);
t = (clock() - t) / CLOCKS_PER_SEC;
cl = get_call_count();
if (fabs(d - DBL_MAX) < DBL_EPSILON)
{
fprintf (stdout, "%s : Task = %d Method is not applicable Count = %d T = %.2f\n", argv[0], task, cl, t);
return -2;
} else
{
fprintf (stdout, "%s : Task = %d Res = %e Count = %d T = %.2f\n", argv[0], task, d, cl, t);
return 0;
}
}

14
2025.05.09/01Ex/solve.c Normal file
View file

@ -0,0 +1,14 @@
#include "solve.h"
#include <math.h>
#include <float.h>
int t1_solve (
double (*f) (double),
double x, double h
) {
if (h < DBL_EPSILON)
return DBL_MAX;
return (f(x + h) - f(x)) / h;
}

9
2025.05.09/01Ex/solve.h Normal file
View file

@ -0,0 +1,9 @@
#ifndef SOLVE_H
#define SOLVE_H
int t1_solve (
double (*f) (double),
double x, double h
);
#endif

42
2025.05.09/02Ex/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 = a02.$(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.09/02Ex/init_f.c Normal file
View 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 + 1;
}
double f2 (double x)
{
cl++;
return x * x + x + 1;
}
double f3 (double x)
{
double x_2 = x * x;
cl++;
return x * x_2 + x_2 + x + 1;
}
double f4 (double x)
{
double x_2 = x * x;
double x_3 = x_2 * x;
cl++;
return x * x_3 + x_3 + x_2 + x + 1;
}
double f5 (double x)
{
cl++;
return exp(-x);
}
double f6 (double x)
{
cl++;
return 1 / (25 * x * x + 1);
}

13
2025.05.09/02Ex/init_f.h Normal file
View 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

45
2025.05.09/02Ex/main.c Normal file
View file

@ -0,0 +1,45 @@
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <float.h>
#include "init_f.h"
#include "solve.h"
/* ./a02.out x h k */
int main (int argc, char *argv[])
{
double t, x, h, d;
int k, cl, task = 2;
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", &x) == 1 &&
((sscanf(argv[2], "%lf", &h) == 1) && (h > 0)) &&
((sscanf(argv[3], "%d", &k) == 1) && ((0 <= k) && (k <= len_f))))
) {
fprintf(stderr, "Usage: %s x h k\n", argv[0]);
return -1;
}
t = clock();
d = t2_solve(f_lst[k], x, h);
t = (clock() - t) / CLOCKS_PER_SEC;
cl = get_call_count();
if (fabs(d - DBL_MAX) < DBL_EPSILON)
{
fprintf (stdout, "%s : Task = %d Method is not applicable Count = %d T = %.2f\n", argv[0], task, cl, t);
return -2;
} else
{
fprintf (stdout, "%s : Task = %d Res = %e Count = %d T = %.2f\n", argv[0], task, d, cl, t);
return 0;
}
}

14
2025.05.09/02Ex/solve.c Normal file
View file

@ -0,0 +1,14 @@
#include "solve.h"
#include <math.h>
#include <float.h>
int t2_solve (
double (*f) (double),
double x, double h
) {
if (h < DBL_EPSILON)
return DBL_MAX;
return (f(x + h) - f(x - h)) / (2 * h);
}

9
2025.05.09/02Ex/solve.h Normal file
View file

@ -0,0 +1,9 @@
#ifndef SOLVE_H
#define SOLVE_H
int t2_solve (
double (*f) (double),
double x, double h
);
#endif

42
2025.05.09/03Ex/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 = a03.$(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.09/03Ex/init_f.c Normal file
View 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 + 1;
}
double f2 (double x)
{
cl++;
return x * x + x + 1;
}
double f3 (double x)
{
double x_2 = x * x;
cl++;
return x * x_2 + x_2 + x + 1;
}
double f4 (double x)
{
double x_2 = x * x;
double x_3 = x_2 * x;
cl++;
return x * x_3 + x_3 + x_2 + x + 1;
}
double f5 (double x)
{
cl++;
return exp(-x);
}
double f6 (double x)
{
cl++;
return 1 / (25 * x * x + 1);
}

13
2025.05.09/03Ex/init_f.h Normal file
View 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

45
2025.05.09/03Ex/main.c Normal file
View file

@ -0,0 +1,45 @@
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <float.h>
#include "init_f.h"
#include "solve.h"
/* ./a03.out x h k */
int main (int argc, char *argv[])
{
double t, x, h, d;
int k, cl, task = 3;
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", &x) == 1 &&
((sscanf(argv[2], "%lf", &h) == 1) && (h > 0)) &&
((sscanf(argv[3], "%d", &k) == 1) && ((0 <= k) && (k <= len_f))))
) {
fprintf(stderr, "Usage: %s x h k\n", argv[0]);
return -1;
}
t = clock();
d = t3_solve(f_lst[k], x, h);
t = (clock() - t) / CLOCKS_PER_SEC;
cl = get_call_count();
if (fabs(d - DBL_MAX) < DBL_EPSILON)
{
fprintf (stdout, "%s : Task = %d Method is not applicable Count = %d T = %.2f\n", argv[0], task, cl, t);
return -2;
} else
{
fprintf (stdout, "%s : Task = %d Res = %e Count = %d T = %.2f\n", argv[0], task, d, cl, t);
return 0;
}
}

14
2025.05.09/03Ex/solve.c Normal file
View file

@ -0,0 +1,14 @@
#include "solve.h"
#include <math.h>
#include <float.h>
int t3_solve (
double (*f) (double),
double x, double h
) {
if (h < DBL_EPSILON)
return DBL_MAX;
return (f(x + h) - 2 * f(x) + f(x - h)) / (h * h);
}

9
2025.05.09/03Ex/solve.h Normal file
View file

@ -0,0 +1,9 @@
#ifndef SOLVE_H
#define SOLVE_H
int t3_solve (
double (*f) (double),
double x, double h
);
#endif

42
2025.05.09/04Ex/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 = 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)

61
2025.05.09/04Ex/init_f.c Normal file
View 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 + 1;
}
double f2 (double x)
{
cl++;
return x * x + x + 1;
}
double f3 (double x)
{
double x_2 = x * x;
cl++;
return x * x_2 + x_2 + x + 1;
}
double f4 (double x)
{
double x_2 = x * x;
double x_3 = x_2 * x;
cl++;
return x * x_3 + x_3 + x_2 + x + 1;
}
double f5 (double x)
{
cl++;
return exp(-x);
}
double f6 (double x)
{
cl++;
return 1 / (25 * x * x + 1);
}

13
2025.05.09/04Ex/init_f.h Normal file
View 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

46
2025.05.09/04Ex/main.c Normal file
View file

@ -0,0 +1,46 @@
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <float.h>
#include "init_f.h"
#include "solve.h"
/* ./a04.out a b n k */
int main (int argc, char *argv[])
{
double t, x, a, b;
int k, n, cl, task = 4;
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], "%d", &n) == 1) && (n > 0)) &&
((sscanf(argv[4], "%d", &k) == 1) && ((0 <= k) && (k <= len_f))))
) {
fprintf(stderr, "Usage: %s x h k\n", argv[0]);
return -1;
}
t = clock();
d = t4_solve(f_lst[k], x, h);
t = (clock() - t) / CLOCKS_PER_SEC;
cl = get_call_count();
if (fabs(d - DBL_MAX) < DBL_EPSILON)
{
fprintf (stdout, "%s : Task = %d Method is not applicable Count = %d T = %.2f\n", argv[0], task, cl, t);
return -2;
} else
{
fprintf (stdout, "%s : Task = %d Res = %e Count = %d T = %.2f\n", argv[0], task, d, cl, t);
return 0;
}
}

14
2025.05.09/04Ex/solve.c Normal file
View file

@ -0,0 +1,14 @@
#include "solve.h"
#include <math.h>
#include <float.h>
int t3_solve (
double (*f) (double),
double x, double h
) {
if (h < DBL_EPSILON)
return DBL_MAX;
return (f(x + h) - 2 * f(x) + f(x - h)) / (h * h);
}

9
2025.05.09/04Ex/solve.h Normal file
View file

@ -0,0 +1,9 @@
#ifndef SOLVE_H
#define SOLVE_H
int t3_solve (
double (*f) (double),
double x, double h
);
#endif

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 = a01.$(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)

View 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 + 1;
}
double f2 (double x)
{
cl++;
return x * x + x + 1;
}
double f3 (double x)
{
double x_2 = x * x;
cl++;
return x * x_2 + x_2 + x + 1;
}
double f4 (double x)
{
double x_2 = x * x;
double x_3 = x_2 * x;
cl++;
return x * x_3 + x_3 + x_2 + x + 1;
}
double f5 (double x)
{
cl++;
return exp(-x);
}
double f6 (double x)
{
cl++;
return 1 / (25 * x * x + 1);
}

View 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

45
2025.05.09/Example/main.c Normal file
View file

@ -0,0 +1,45 @@
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <float.h>
#include "init_f.h"
#include "solve.h"
/* ./a01.out x h k */
int main (int argc, char *argv[])
{
double t, x, h, d;
int k, cl, task = 1;
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", &x) == 1 &&
((sscanf(argv[2], "%lf", &h) == 1) && (h > 0)) &&
((sscanf(argv[3], "%d", &k) == 1) && ((0 <= k) && (k <= len_f))))
) {
fprintf(stderr, "Usage: %s x h k\n", argv[0]);
return -1;
}
t = clock();
d = t1_solve(f_lst[k], x, h);
t = (clock() - t) / CLOCKS_PER_SEC;
cl = get_call_count();
if (fabs(d - DBL_MAX) < DBL_EPSILON)
{
fprintf (stdout, "%s : Task = %d Method is not applicable Count = %d T = %.2f\n", argv[0], task, cl, t);
return -2;
} else
{
fprintf (stdout, "%s : Task = %d Res = %e Count = %d T = %.2f\n", argv[0], task, d, cl, t);
return 0;
}
}

View file

@ -0,0 +1,14 @@
#include "solve.h"
#include <math.h>
#include <float.h>
int t1_solve (
double (*f) (double),
double x, double h
) {
if (h < DBL_EPSILON)
return DBL_MAX;
return (f(x + h) - f(x)) / h;
}

View file

@ -0,0 +1,9 @@
#ifndef SOLVE_H
#define SOLVE_H
int t1_solve (
double (*f) (double),
double x, double h
);
#endif

View file

@ -0,0 +1,17 @@
script_name="$(basename "$0")"
mkdir -p tests
if [ "$#" -ne 1 ]; then
echo "The number of parameters is not equal 1"
exit 1
fi
make
for (( k = 0 ; k < 7; k++ )); do
echo "------- K = $k -------"
for (( a = -100 ; a < 100 ; a++ )); do
echo "k = $k x_0 = "$(echo "$a / 10" | bc -l)" ---"
./a0$1.out "$(echo "$a / 10" | bc -l)" 1e-14 $k
done
done >$(pwd)/tests/out_$script_name.log 2>$(pwd)/tests/err_$script_name.log