diff options
author | justanothercatgirl <sotov2070@gmail.com> | 2024-09-13 15:03:36 +0300 |
---|---|---|
committer | justanothercatgirl <sotov2070@gmail.com> | 2024-09-13 15:03:36 +0300 |
commit | 49b2b840c668c64089936e681192c75a8de2c81c (patch) | |
tree | c92fbb6e289a978f083133c07c799a456e29ddd0 /main.c | |
parent | bd2943ca42013a089975893eb84e1e48cec6a03b (diff) |
iniial commitsin-sum
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 72 |
1 files changed, 18 insertions, 54 deletions
@@ -1,59 +1,23 @@ +#include <stdio.h> -/* - * Сотов Константин Алексеевич, 117 группа - * sotov@twistea.su +7 914 329 50 01 - * - * Данный код загружает динамическую библиотеку, - * имя которой передано в аргументах командной строки, - * и считает интеграл функции "f", если она есть - * в этой динамической библиотеке. - */ +#define EPSILON 0.001*0.001 +#define MAX_ITERATIONS 100 -#include <stdio.h> // printf, scanf -#include <stdlib.h> // exit -#include <dlfcn.h> // dlopen, dlsym, dlclose +int main(void) { + double x; + double result; + double last_elem; + int i; + + printf("Input a number\n"); + scanf("%lf", &x); + last_elem = result = x; - -// 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); + for (i = 1; i < MAX_ITERATIONS; ++i) { + last_elem = last_elem * x * x / (2*i * (2*i+1)); + if (last_elem*last_elem <= EPSILON) break; + result += last_elem * ( (i%2 * -2) + 1 ); } - // 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); + printf("result = %lf; iterations = %i; last_elem = %lf\n", result, i, last_elem); + return 0; } |