首页 > 其他 > 详细

指针函数小练习

时间:2014-01-22 22:47:50      阅读:360      评论:0      收藏:0      [点我收藏+]
题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数

1/1+1/3+...+1/n(利用指针函数)

代码实现:

//主程序
float sum;
int n = 9;          //给n赋值

if(n%2==0)          //n为偶数
{
    printf("Even=");
    sum=dcall(peven,n);
}
else                //n为奇数
{
    printf("Odd=");
    sum=dcall(podd,n);
}
printf("%f",sum);

float peven(int n)      //偶数时求和
{
    float s;
    int i;
    s=0;
    for(i=2;i<=n;i+=2)
        s+=1/(float)i;
    return(s);
}
float podd(n)           //奇数时求和
int n;
{
    float s;
    int i;
    s=0;
    for(i=1;i<=n;i+=2)
        s+=1/(float)i;
    return(s);
}
float dcall(fp,n)
float (*fp)();          
int n;
{
    float s;
    s=(*fp)(n);
    return(s);
}




指针函数小练习

原文:http://blog.csdn.net/u011439689/article/details/18660407

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!