Started Example

This commit is contained in:
AZEN-SGG 2025-04-29 14:35:20 +03:00
parent f9716a8c7b
commit 135f27f63d
12 changed files with 310 additions and 0 deletions

Binary file not shown.

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 array_io.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,37 @@
#include <stdio.h>
#include <string.h>
#include "array_io.h"
io_status read_values_and_derivatives (
double * restrict X,
double * restrict Y,
double * restrict D,
const int n,
const char * restrict name
)
{
FILE *fp;
if (!(fp = fopen(name, "r")))
return ERROR_OPEN;
for (int i = 0; i < n; ++i)
if (fscanf(fp, "%lf\t%lf\t%lf", X + i, Y + i, D + i) != 3)
{ fclose(fp); return ERROR_READ; }
fclose(fp);
return SUCCESS;
}
void print_values_and_derivatives (
const double * restrict X,
const double * restrict Y,
const double * restrict D,
const int n, const int p
)
{
int np = (n > p ? p : n);
for (int i = 0; i < np; i++)
printf("f(%lf) = %lf, f`(%lf) = %lf\n", X[i], Y[i], X[i], D[i]);
}

View file

@ -0,0 +1,20 @@
#ifndef ARRAY_IO_H
#define ARRAY_IO_H
#include "io_status.h"
io_status read_values_and_derivatives (
double * restrict X,
double * restrict Y,
double * restrict D,
const int n,
const char * restrict name
);
void print_values_and_derivatives (
const double * restrict X,
const double * restrict Y,
const double * restrict D,
const int n, const int p
);
#endif

View file

@ -0,0 +1,46 @@
#include "init_f.h"
#include <math.h>
double f0 (double x)
{
(void)x;
return 1;
}
double f1 (double x)
{
return (x * 1e-100) - 1.0;
}
double f2 (double x)
{
return 4 - x * x;
}
double f3 (double x)
{
double x_2 = x * x;
return x * x_2 + 3 * x_2 + 16;
}
double f4 (double x)
{
double x_2 = x * x;
return 3 - 2 * x_2 - x_2 * x_2;
}
double f5 (double x)
{
return sqrt(fabs(x) + 1) - 2;
}
double f6 (double x)
{
return sqrt(sqrt(fabs(x) + 1) + 1) - 2;
}

View file

@ -0,0 +1,12 @@
#ifndef INIT_F_H
#define INIT_F_H
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

View file

@ -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

62
2025.05.02/Example/main.c Normal file
View file

@ -0,0 +1,62 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <float.h>
#include <math.h>
#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, task = 1;
status ret;
double (*f) (double);
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6};
if (
!((argc == 6) &&
sscanf(argv[1], "%lf", &a) == 1 &&
sscanf(argv[2], "%lf", &b) == 1 &&
sscanf(argv[3], "%lf", &eps) == 1 &&
((sscanf(argv[4], "%d", &m) == 1) && m > 0) &&
((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= 6))))
) {
fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]);
return 1;
}
f = f_lst[k];
t = clock();
ret = t1_solve(f, a, b, eps, m, &x);
t = (clock() - t) / CLOCKS_PER_SEC;
do {
switch (ret)
{
case SUCCESS:
continue;
case MORE_ONE_ROOT:
continue;
case RUN_TIME:
break;
}
fprintf(stdout, "Error: with code %d - The method is not applicable!\n", ret);
return ret;
} while (0);
if (ret == MORE_ONE_ROOT)
printf("%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, count, t);
else
printf("%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, f(x), it, count, t);
return 0;
}

View file

@ -0,0 +1,53 @@
#include "solve.h"
#include "status.h"
#include <math.h>
status t1_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x,
int *m_cl, int *m_it
) {
static int it = 0
int cl = 0;
double y_a = f(a), y_b = f(b);
it++;
if (y_a * y_b >= -DBL_EPSILON)
{
*x = DBL_MAX;
return MORE_ONE_ROOT;
}
for (cl = 0; cl < m; ++cl)
{
double c = (a + b) * 0.5;
double y = f(c);
if (fabs(y) < eps)
{
*x = c;
break;
} else if (y * y_a > DBL_EPSILON)
{
a = c;
y_a = y;
} else if (y * y_b > DBL_EPSILON)
{
b = c;
y_b = y;
}
}
*m_it = it;
*cl = cl;
*x = c;
if (cl >= m)
return RUN_TIME;
return SUCCESS;
}

View file

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

View file

@ -0,0 +1,10 @@
#ifndef STATUS_H
#define STATUS_H
typedef enum _status {
SUCCESS,
MORE_ONE_ROOT,
RUN_TIME,
} status;
#endif

BIN
2025.05.02/Tasks10.pdf Normal file

Binary file not shown.