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

本文介绍了一个使用C语言实现的函数指针应用案例,通过一个简单的菜单选择不同的数学函数进行运算,包括二次函数积分、三次函数积分等。代码展示了如何定义函数指针并将其作为参数传递给另一个函数,实现灵活的函数调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值