From bd2943ca42013a089975893eb84e1e48cec6a03b Mon Sep 17 00:00:00 2001 From: justanothercatgirl Date: Thu, 5 Sep 2024 11:49:07 +0300 Subject: Initial commit --- main.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 main.c (limited to 'main.c') diff --git a/main.c b/main.c new file mode 100644 index 0000000..76afcef --- /dev/null +++ b/main.c @@ -0,0 +1,59 @@ + +/* + * Сотов Константин Алексеевич, 117 группа + * sotov@twistea.su +7 914 329 50 01 + * + * Данный код загружает динамическую библиотеку, + * имя которой передано в аргументах командной строки, + * и считает интеграл функции "f", если она есть + * в этой динамической библиотеке. + */ + +#include // printf, scanf +#include // exit +#include // dlopen, dlsym, dlclose + + +// Integrates function f from a to b with 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); +} -- cgit v1.2.3-70-g09d2