Add dist ans Task 5 is done!

This commit is contained in:
AZEN-SGG 2025-04-17 21:56:31 +03:00
parent 4a5c5ea41e
commit 2f21a20fd1
21 changed files with 970 additions and 37 deletions

42
2025.04.18/05Ex/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
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 = a05.$(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)

View file

@ -0,0 +1,38 @@
#include "contin_func.h"
#include <math.h>
#include <float.h>
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;
}

View file

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

View file

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

34
2025.04.18/05Ex/main.c Normal file
View file

@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#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 = 5;
if (
!((argc == 3) &&
sscanf(argv[1], "%lf", &x) == 1 &&
((sscanf(argv[2], "%lf", &eps) == 1) && eps > 0))
) {
fprintf(stderr, "Usage: %s x eps\n", argv[0]);
return 1;
}
t = clock();
r1 = fsin(x, eps);
t = (clock() - t) / CLOCKS_PER_SEC;
r2 = fabs(r1 - sin(x));
printf("%s : Task = %d Result = %e Residual = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t);
return 0;
}

49
2025.04.18/05Ex/solve.c Normal file
View file

@ -0,0 +1,49 @@
#include "solve.h"
#include "contin_func.h"
#include <stdlib.h>
#include <math.h>
#include <float.h>
double fsin (double x, const double eps)
{
double answer;
short flag = 1;
if (-x > DBL_EPSILON)
{
x = -x;
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 - M_PI_2 >= DBL_EPSILON)
{
x -= M_PI_2;
flag *= 2;
}
if (abs(flag) == 2)
{
double cosinus, sinus;
x /= 2;
cosinus = scos(x, eps);
sinus = ssin(x, eps);
answer = ((flag > 0) - (flag < 0)) * (cosinus * cosinus - sinus * sinus);
} else
answer = flag * ssin(x, eps);
return answer;
}

6
2025.04.18/05Ex/solve.h Normal file
View file

@ -0,0 +1,6 @@
#ifndef SOLVE_H
#define SOLVE_H
double fsin (double x, const double eps);
#endif