C 练习实例76
题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n(利用指针函数)。
程序分析:无。文章源自公式库网-https://www.gongshiku.com/html/202112/c-lianxishili76.html
实例
#include<stdio.h>
#include<stdlib.h>
double evenumber(int n);
double oddnumber(int n);
int main()
{
int n;
double r;
double (*pfunc)(int);
printf("请输入一个数字:");
scanf("%d",&n);
if(n%2==0) pfunc=evenumber;
else pfunc=oddnumber;
r=(*pfunc)(n);
printf("%lf\n",r);
system("pause");
return 0;
}
double evenumber(int n)
{
double s=0,a=0;
int i;
for(i=2;i<=n;i+=2)
{
a=(double)1/i;
s+=a;
}
return s;
}
double oddnumber(int n)
{
double s=0,a=0;
int i;
for(i=1;i<=n;i+=2)
{
a=(double)1/i;
s+=a;
}
return s;
}
以上实例运行输出结果为:文章源自公式库网-https://www.gongshiku.com/html/202112/c-lianxishili76.html
请输入一个数字:2 0.500000文章源自公式库网-https://www.gongshiku.com/html/202112/c-lianxishili76.html文章源自公式库网-https://www.gongshiku.com/html/202112/c-lianxishili76.html
运营不易,
感谢支持!
我的微信
微信号已复制
我的微信公众号
我的微信公众号扫一扫
我的公众号
公众号已复制