Vuvko's Testing Forum

Объявление

Информация о пользователе

Привет, Гость! Войдите или зарегистрируйтесь.



Last C

Сообщений 1 страница 2 из 2

1

1)

Код:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

enum {
    A12 = 18,
    B12 = 30,
    A13 = -10,
    B13 = 0,
    A23 = 0,
    B23 = 10,
    EPS = 1000
};

typedef double (*func)(double);

double f1(double x);
double f2(double x);
double f3(double x);
double deriv(func f, double x, double dx);
double root(func, func, double, double, double);
double integral(func, double, double, double);

int
main(void)
{
    //printf("----------\nDEBUG:\n----------\n");
    double eps1 = 0.1 / EPS, eps2 = 0.01 / EPS;
    double a = root(f1, f2, A12 / 10.0, B12 / 10.0, eps1);
    double b = root(f1, f3, A13 / 10.0, B13 / 10.0, eps1);
    double c = root(f2, f3, A23 / 10.0, B23 / 10.0, eps1);
    double i1, i2, i3;
    if(a < b)
        i1 = integral(f1, a, b, eps2);
    else
        i1 = integral(f1, b, a, eps2);
    //printf("i1 - %lf\n", i1);
    if(a < c)
        i2 = integral(f2, a, c, eps2);
    else
        i2 = integral(f2, c, a, eps2);
    //printf("i2 - %lf\n", i2);
    if(b < c)
        i3 = integral(f3, b, c, eps2);
    else
        i3 = integral(f3, c, b, eps2);
    //printf("i3 - %lf\n", i3);
    double I = i1 - i2 - i3;
    //printf("%lf\n", I);
    //printf("----------\nSTOP DEBUG\n----------\n");
    printf("f1(x) = 3/((x - 1) * (x - 1) + 1)\nf2(x) = sqrt(x + 0.5)\nf3(x) = exp(-x)\n");
    printf("Points of intersection:\n");
    printf("  f1 and f2: %.5lf\n", a);
    printf("  f1 and f3: %.5lf\n", b);
    printf("  f2 and f3: %.5lf\n", c);
    printf("Area: %.5lf\n", I);
    return 0;
}


double f1(double x)
{
    return 3/((x - 1) * (x - 1) + 1);
}

double f2(double x)
{
    return sqrt(x + 0.5);
}

double f3(double x)
{
    return exp(-x);
}

double deriv(func f, double x, double dx)
{
    double dy = (*f)(x + dx) - (*f)(x);
    return dy / dx;
}

// комбинированный способ
double root(func f, func g, double a, double b, double eps)
{
    char sgnd1 = ((*f)(a) - (*g)(a)) < 0;   // знак первой производной F(x) = f(x) - g(x)
    char sgnd2f = (*f)((a + b) / 2) <= (((*f)(a) + (*f)(b)) / 2);
    char sgnd2g = (*g)((a + b) / 2) <= (((*g)(a) + (*g)(b)) / 2);
    char sgnd2 = sgnd2f * sgnd2g;           // знак второй производной F(x)
    char sgn = !(sgnd1 ^ sgnd2);             // F'(x) * F''(x) > 0
    double c1, c2, Fa, Fb;
    while(b - a >= eps){
        Fa = (*f)(a) - (*g)(a);
        Fb = (*f)(b) - (*g)(b);
        c1 = (a * Fb - b * Fa) / (Fb - Fa); // точка пересечения хорды с осью абсцисс
        if(sgn)
            c2 = b - Fb / (deriv(f, b, eps) - deriv(g, b, eps));
        else
            c2 = a - Fa / (deriv(f, a, eps) - deriv(g, a, eps));
        if(c2 > c1){
            b = c2;
            a = c1;
        } else {
            b = c1;
            a = c2;
        }
    }
    return (a + b) / 2;
}

// метод прямоугольников
double integral(func f, double a, double b, double eps)
{
    int n = 1 / eps;
    double p = 1 / 3.0;
    double h;
    double Fi;
    double In = 0, I2n = 0;
    int i;
    do{
        In = I2n;
        n *= 2;
        h = (b - a) / n;
        for(i = 0; i < n; i++){
            Fi = (*f)(a + (i + 0.5) * h);
            I2n += Fi;
        }
        I2n *= h;
    }while(p * abs(In - I2n) >= eps);
    return I2n;
}

0

2

2)

Код:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

enum{
    VARIABLE = 'X'
};

typedef struct TreeNode{
    struct TreeNode *left;
    struct TreeNode *right;
    char op;
    int num;
} TreeNode;

void freetree(TreeNode *tree)
{
    if(!tree) return;
    freetree(tree->left);
    freetree(tree->right);
    free(tree);
}

void tree_to_func(TreeNode *tree)
{
    if(!tree) return;
    if(tree->op == VARIABLE)
        printf("%c", VARIABLE);
    else if(tree->op == 'n')
        printf("%d", tree->num);
    else {
        printf("(");
        tree_to_func(tree->left);
        printf("%c", tree->op);
        tree_to_func(tree->right);
        printf(")");
    }
}

int is_operation(char c)
{
    return (c == '+' || c == '-' || c == '*');
}

TreeNode *func_to_tree(char *str, int *i, int l)
{
    if(*i > l) return NULL;
    TreeNode *n_left = malloc(sizeof(TreeNode));
    TreeNode *n_right= malloc(sizeof(TreeNode));
    TreeNode *node = malloc(sizeof(TreeNode));
    n_left->left = NULL;
    n_left->right = NULL;
    n_right->left = NULL;
    n_right->right = NULL;
    node->left = NULL;
    node->right = NULL;
    node->num = 0;
    (*i)++;
    if(str[*i - 1] == '(')
       n_left = func_to_tree(str, i, l);
    else if(str[*i - 1] == VARIABLE){
        n_left->op = VARIABLE;
        n_left->num = 0;
    } else if(str[*i - 1] <= '9' && str[*i - 1] >= '0'){
        n_left->op = 'n';
        n_left->num = str[*i - 1] - '0';
    } else {
        free(n_left);
        free(n_right);
        free(node);
        return NULL;
    }
    if(!is_operation(str[*i])) {
        free(n_left);
        free(n_right);
        free(node);
        return NULL;
    }
    node->op = str[*i];
    (*i)+=2;
    if(str[*i - 1] == '(')
       n_right = func_to_tree(str, i, l);
    else if(str[*i - 1] == VARIABLE){
        n_right->op = VARIABLE;
        n_right->num = 0;
    } else if(str[*i - 1] <= '9' && str[*i - 1] >= '0'){
        n_right->op = 'n';
        n_right->num = str[*i - 1] - '0';
    } else {
        free(n_left);
        free(n_right);
        free(node);
        return NULL;
    }
    if(n_left == NULL || n_right == NULL){
        free(n_left);
        free(n_right);
        free(node);
        return NULL;
    }
    (*i)++;
    node->left = n_left;
    node->right = n_right;
    return node;
}

TreeNode *copy(TreeNode *func)
{
    if(!func) return NULL;
    TreeNode *buf = malloc(sizeof(TreeNode));
    buf->op = func->op;
    buf->num = func->num;
    buf->left = copy(func->left);
    buf->right = copy(func->right);
    return buf;
}

TreeNode *deriv(TreeNode *func)
{
    if(!func || func->op == 0) return NULL;
    TreeNode *buf1 = NULL, *buf2 = NULL;
    switch (func->op){
        case VARIABLE:
            buf1 = malloc(sizeof(TreeNode));
            buf1->op = 'n';
            buf1->num = 1;
            buf1->left = NULL;
            buf1->right = NULL;
            break;
        case 'n':
            buf1 = malloc(sizeof(TreeNode));
            buf1->op = 'n';
            buf1->num = 0;
            buf1->left = NULL;
            buf1->right = NULL;
            break;
        case '+':
            buf1 = malloc(sizeof(TreeNode));
            buf1->op = '+';
            buf1->num = 0;
            buf1->left = deriv(func->left);
            buf1->right = deriv(func->right);
            break;
        case '-':
            buf1 = malloc(sizeof(TreeNode));
            buf1->op = '-';
            buf1->num = 0;
            buf1->left = deriv(func->left);
            buf1->right = deriv(func->right);
            break;
        case '*':
            buf1 = malloc(sizeof(TreeNode));
            buf2 = malloc(sizeof(TreeNode));
            buf2->op = '*';
            buf2->num = 0;
            buf2->left = deriv(func->left);
            buf2->right = copy(func->right);
            buf1->op = '+';
            buf1->num = 0;
            buf1->left = buf2;
            buf2 = malloc(sizeof(TreeNode));
            buf2->op = '*';
            buf2->num = 0;
            buf2->left = copy(func->left);
            buf2->right = deriv(func->right);
            buf1->right = buf2;
            break;
        default: return NULL;
    }
    return buf1;
}

int
main(void)
{
    TreeNode *root = NULL;
    char *str = malloc(sizeof(char));
    int length;
    int *i = malloc(sizeof(int));
    *i = 1;
    scanf("%s", str);
    length = strlen(str);
    root = NULL;
    root = func_to_tree(str, i, length);
    if(!root || *i != length){
        printf("Impossible...\n");
        return 0;
    }
    printf("\n");
    tree_to_func(root);
    TreeNode *derivroot = deriv(root);
    printf("\nderiv:\n");
    tree_to_func(derivroot);
    printf("\n");
    freetree(root);
    freetree(derivroot);
    return 0;
}

0