利用指针变量调用他所指向的函数求定积分,列举了5个普通的函数,代码如下

#include<stdio.h>
#include<math.h>
int main()
{
    float fun(float a, float b, float(*p)(float, float));
    float fun1(float,float);
    float fun2(float, float);
    float fun3(float, float);
    float fun4(float, float);
    float fun5(float, float);
    int n;
    printf("please input a number of function from 1 to 5:\n");
    scanf_s("%d", &n);
    switch (n)
    {
    case 1:printf("this is the first function,");
        fun(1, 4,fun1);
        break;
    case 2:printf("this is the second function,");
        fun(1, 4,fun2);
        break;
    case 3:printf("this is the third function,");
        fun(1, 4,fun3);
        break;
    case 4:printf("this is the 4th function,");
        fun(1, 4,fun4);
        break;
    case 5:printf("this is the 5th function,");
        fun(1, 4,fun5);
        break;
    default:printf("DATA ERROR");
    }
    return 0;
}
float fun(float x, float y, float(*p)(float, float))
{
    float result;
    result = (*p)(x, y);
    printf("the integrate of the function is %8.2f\n", result);
}
float fun1(float a, float b)
{
    float sum;
    sum = b + 0.5*b*b - a - 0.5*a*a;
    return sum;
}
float fun2(float a, float b)
{
    float sum;
    sum = b * b + 3 * b - a * a - 3 * a;
    return sum;
}
float fun3(float a, float b)
    {
        float sum;
        sum = exp(b) + b - exp(a) - a;
        return sum;
    }
float fun4(float a, float b)
{
    float sum;
    sum = (1.0 / 3.0)*(pow((1 + b), 3)) - (1.0 / 3.0)*(pow((1 + a), 3));
    return sum;
}
float fun5(float a, float b)
{
    float sum;
    sum = (1.0 / 4.0)*pow(b, 4) - (1.0 / 4.0)*pow(a, 4);
    return sum;
}

猜你喜欢

转载自blog.csdn.net/progess_every_day/article/details/104860316