diff options
-rw-r--r-- | Makefile | 18 | ||||
-rw-r--r-- | README.md | 15 | ||||
-rw-r--r-- | libs/libpoly.c | 7 | ||||
-rw-r--r-- | libs/libsin.c | 7 | ||||
-rw-r--r-- | libs/libsq.c | 4 | ||||
-rw-r--r-- | main.c | 59 |
6 files changed, 110 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..fdb2c0c --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ + +all: main libs + +# the main program +main: main.c + cc -o main main.c + +# available libraries +.PHONY: libs +libs: libs/* + cc -shared libs/libsq.c -lm -lc -o libsq + cc -shared libs/libsin.c -lm -lc -o libsin + cc -shared libs/libpoly.c -lm -lc -o libpoly + +# clear build +.PHONY: clean +clean: + rm -f main libsq libsin libpoly diff --git a/README.md b/README.md new file mode 100644 index 0000000..8dc07f1 --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# Файлы +* main.c: главный файл +* Makefile: система сброки GNU Make +* libs/ : папка с библиотеками для теста. а каждом файле там по 1 функции. + +# Как протестировать +## Требования +* ОС: Unix +* gnu make +* командная строка +## Сборка +* cd <путь к этому файлу> +* make all +## Запуск +`./main ./libsq `. вместо libsq может быть libsin, libpoly diff --git a/libs/libpoly.c b/libs/libpoly.c new file mode 100644 index 0000000..2daa972 --- /dev/null +++ b/libs/libpoly.c @@ -0,0 +1,7 @@ + +#include <math.h> + +// calculate polynomial +double f(double x) { + return pow(x, 3) - 2*x*x - 10*x + 69; +} diff --git a/libs/libsin.c b/libs/libsin.c new file mode 100644 index 0000000..34200af --- /dev/null +++ b/libs/libsin.c @@ -0,0 +1,7 @@ + +#include <math.h> + +// calculate sine +double f(double x) { + return sin(x); +} diff --git a/libs/libsq.c b/libs/libsq.c new file mode 100644 index 0000000..2b93fb4 --- /dev/null +++ b/libs/libsq.c @@ -0,0 +1,4 @@ +// calculate square +double f(double x) { + return x*x; +} @@ -0,0 +1,59 @@ + +/* + * Сотов Константин Алексеевич, 117 группа + * sotov@twistea.su +7 914 329 50 01 + * + * Данный код загружает динамическую библиотеку, + * имя которой передано в аргументах командной строки, + * и считает интеграл функции "f", если она есть + * в этой динамической библиотеке. + */ + +#include <stdio.h> // printf, scanf +#include <stdlib.h> // exit +#include <dlfcn.h> // dlopen, dlsym, dlclose + + +// Integrates function f from a to b with <sums> steps. +// Uses "naive" and straightforward integration technique +// Assumes a < b +double integral(double (*f)(double), double a, double b, int sums) { + double result = 0; + const double dx = (b-a)/sums; + for (double i = a; i < b; i += dx) + result += dx * f(i); + return result; +} + +int main(int argc, char* argv[]) { + if (argc != 2) { + printf("Недостаточно аргументов. использование:\n" + "%s <путь к динамической библиотеке>\n", argv[0]); + exit(EXIT_FAILURE); + } + // Open the dynamic library + void* handle = dlopen(argv[1], RTLD_NOW); + if (handle == NULL) { + printf("ERROR: %s\n", dlerror()); + exit(EXIT_FAILURE); + } + // Locate the function + void* f = dlsym(handle, "f"); + if (f == NULL) { + printf("ERROR: %s", dlerror()); + dlclose(handle); + exit(EXIT_FAILURE); + } + + // Input-output block + const char* fullname + = "Сотов Константин Алексеевич"; + printf("Автор: %s\n", fullname); + double a, b; + puts("Введите нижнюю и верхнюю границы интегрирования через пробел"); + scanf("%lf %lf", &a, &b); + printf("Интеграл f от %lf до %lf = %lf\n", + a, b, integral(f, a, b, 10000)); + + exit(EXIT_SUCCESS); +} |