游客已登陆 (0)未知
笔行证 257310
昵称 kuan 
笔贝 Score1
加为好友 发送短信
<< << 2008 十月 >> >>
1234
567891011
12131415161718
19202122232425
262728293031

访问计数:21274
本文:383 今天:1 本月 383

本地音乐播放器



 
       指向函数的指针
2006-11-11 晴

出处或者来源:C程序设计(第二版)

[非站内图片]

#include<stdio.h>
#include<math.h>
void main()
{float a1,b1,a2,b2,a3,b3,c,(*p)(float); //指向函数的指针
int n=20;
float integral(float(*p)(float),float a,float b,int n);
float fsin(float);
float fcos(float);
float fexp(float);

printf("Input a1,b1:");
scanf("%f,%f",&a1,&b1);
printf("Input a2,b2:");
scanf("%f,%f",&a2,&b2);
printf("Input a3,b3:");
scanf("%f,%f",&a3,&b3);

p=fsin;
c=integral(p,a1,b1,n);
printf("The integral of sinx is:%f
",c);

p=fcos;
c=integral(p,a2,b2,n);
printf("The integral of cosx is:%f
",c);

p=fexp;
c=integral(p,a3,b3,n);
printf("The integral of expx is:%f
",c);
}

float integral(float(*p)(float),float a,float b,int n)
{int i;
float x,h,s;
h=(b-a)/n;
x=a;
s=0;
for(i=1;i<=n;i++)
{
x+=h;
s+=(*p)(x)*h;
}
return s;
}

float fsin(float x)
{return (float)sin(x);} /*类型强制转换由double转为float*/

float fcos(float x)
{return (float)cos(x);}

float fexp(float x)
{return (float)exp(x);}
运行结果:
Input a1,b1:0,1
Input a2,b2:-1,1
Input a3,b3:1,2
The integral of sinx is:0.480639
The integral of cosx is:1.681539
The integral of expx is:4.788514
Press any key to continue.
# posted by kuan @ 2006-11-11 21:44:23 评论(0)
 






   
 
  kuan  2007-04-13 11:54:06
这个函数是计算积分的.算法思想:求积分我们可以求他的面积,,沿X轴分成n份,计算n份的面积再相加.





float integral(float(*p)(float),float a,float b,int n)

//这里的第一个参数是定义一个指向函数的指针,



下面有一句语句

s+=(*p)(x)*h;//其中的(*p)(x)也就是函数的调用了,.
 
 
   


   
 
  米爆啦  2007-04-12 22:12:50
float integral(float(*p)(float),float a,float b,int n)

{int i;

float x,h,s;

h=(b-a)/n;

x=a;

s=0;

for(i=1;i<=n;i++)

{

x+=h;

s+=(*p)(x)*h;

}

return s;

}

这一段看不懂啊.
 
 
   


   
 
  hacder  2006-11-11 21:46:02
^_^ 写得不错哦

支持下.
 
 
   


 
笔 名:
*
评 论:
最多1000字。当前字数:0
*
联系方式: