Task 4 and 5 are done
This commit is contained in:
parent
6efd4f5786
commit
67a26248b1
13 changed files with 285 additions and 90 deletions
|
@ -19,7 +19,7 @@ int main (int argc, char *argv[])
|
|||
!((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))))
|
||||
((sscanf(argv[3], "%d", &k) == 1) && ((0 <= k) && (k < len_f))))
|
||||
) {
|
||||
fprintf(stderr, "Usage: %s x h k\n", argv[0]);
|
||||
return -1;
|
||||
|
|
|
@ -19,7 +19,7 @@ int main (int argc, char *argv[])
|
|||
!((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))))
|
||||
((sscanf(argv[3], "%d", &k) == 1) && ((0 <= k) && (k < len_f))))
|
||||
) {
|
||||
fprintf(stderr, "Usage: %s x h k\n", argv[0]);
|
||||
return -1;
|
||||
|
|
|
@ -21,7 +21,7 @@ else
|
|||
endif
|
||||
|
||||
TARGET = a04.$(EXE)
|
||||
OBJ = main.o solve.o init_f.o
|
||||
OBJ = main.o solve.o
|
||||
|
||||
%.o: %.c
|
||||
gcc $(WFLAGS) $(LDFLAGS) -c $< -o $@
|
||||
|
|
|
@ -1,61 +0,0 @@
|
|||
#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);
|
||||
}
|
||||
|
|
@ -1,13 +1,66 @@
|
|||
#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);
|
||||
#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
|
||||
|
|
|
@ -9,8 +9,8 @@
|
|||
/* ./a04.out a b n k */
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
double t, x, a, b;
|
||||
int k, n, cl, task = 4;
|
||||
double t, integral, a, b;
|
||||
int k, n, calls, task = 4;
|
||||
|
||||
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6};
|
||||
int len_f = sizeof(f_lst) / sizeof(f_lst[0]);
|
||||
|
@ -20,26 +20,24 @@ int main (int argc, char *argv[])
|
|||
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))))
|
||||
((sscanf(argv[4], "%d", &k) == 1) && ((0 <= k) && (k < len_f))))
|
||||
) {
|
||||
fprintf(stderr, "Usage: %s x h k\n", argv[0]);
|
||||
fprintf(stderr, "Usage: %s a b n k\n", argv[0]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
t = clock();
|
||||
d = t4_solve(f_lst[k], x, h);
|
||||
integral = t4_solve(f_lst[k], a, b, n);
|
||||
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||
|
||||
cl = get_call_count();
|
||||
calls = 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);
|
||||
if (fabs(integral - DBL_MAX) < DBL_EPSILON) {
|
||||
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 Count = %d T = %.2f\n", argv[0], task, d, cl, t);
|
||||
} else {
|
||||
fprintf (stdout, "%s : Task = %d Res = %e Count = %d T = %.2f\n", argv[0], task, integral, calls, t);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,12 +3,24 @@
|
|||
#include <math.h>
|
||||
#include <float.h>
|
||||
|
||||
int t3_solve (
|
||||
double t4_solve (
|
||||
double (*f) (double),
|
||||
double x, double h
|
||||
double a, double b,
|
||||
int n
|
||||
) {
|
||||
if (h < DBL_EPSILON)
|
||||
const double h = (b - a) / n;
|
||||
double x = a;
|
||||
double sum = (f(a) + f(b)) * 0.5;
|
||||
|
||||
if (h < NUM_FPE)
|
||||
return DBL_MAX;
|
||||
return (f(x + h) - 2 * f(x) + f(x - h)) / (h * h);
|
||||
|
||||
for (int i = 1; i < (n - 1); ++i)
|
||||
{
|
||||
x += h;
|
||||
sum += f(x);
|
||||
}
|
||||
|
||||
return h * sum;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
#ifndef SOLVE_H
|
||||
#define SOLVE_H
|
||||
|
||||
int t3_solve (
|
||||
#define NUM_FPE 1e-300
|
||||
|
||||
double t4_solve (
|
||||
double (*f) (double),
|
||||
double x, double h
|
||||
double a, double b,
|
||||
int n
|
||||
);
|
||||
|
||||
#endif
|
||||
|
|
42
2025.05.09/05Ex/Makefile
Normal file
42
2025.05.09/05Ex/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 = a05.$(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/05Ex/init_f.h
Normal file
66
2025.05.09/05Ex/init_f.h
Normal 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/05Ex/main.c
Normal file
44
2025.05.09/05Ex/main.c
Normal file
|
@ -0,0 +1,44 @@
|
|||
#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, integral, a, b;
|
||||
int k, n, calls, task = 5;
|
||||
|
||||
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 a b n k\n", argv[0]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
t = clock();
|
||||
integral = t5_solve(f_lst[k], a, b, n);
|
||||
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||
|
||||
calls = get_call_count();
|
||||
|
||||
if (fabs(integral - DBL_MAX) < DBL_EPSILON) {
|
||||
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 Count = %d T = %.2f\n", argv[0], task, integral, calls, t);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
26
2025.05.09/05Ex/solve.c
Normal file
26
2025.05.09/05Ex/solve.c
Normal file
|
@ -0,0 +1,26 @@
|
|||
#include "solve.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <float.h>
|
||||
|
||||
double t5_solve (
|
||||
double (*f) (double),
|
||||
double a, double b,
|
||||
int n
|
||||
) {
|
||||
const double h = (b - a) / (2 * n);
|
||||
double x = a;
|
||||
double sum = (f(a) + f(b)) * 0.5;
|
||||
|
||||
if (h < NUM_FPE)
|
||||
return DBL_MAX;
|
||||
|
||||
for (int i = 1; i < (2 * n - 1); ++i)
|
||||
{
|
||||
x += h;
|
||||
sum += ((i & 1) + 1) * f(x);
|
||||
}
|
||||
|
||||
return (b - a) * sum / (3 * n);
|
||||
}
|
||||
|
12
2025.05.09/05Ex/solve.h
Normal file
12
2025.05.09/05Ex/solve.h
Normal file
|
@ -0,0 +1,12 @@
|
|||
#ifndef SOLVE_H
|
||||
#define SOLVE_H
|
||||
|
||||
#define NUM_FPE 1e-300
|
||||
|
||||
double t5_solve (
|
||||
double (*f) (double),
|
||||
double a, double b,
|
||||
int n
|
||||
);
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue