|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<math.h>
int main()
{
double fac1(double x,double y);
double fac2(double x,double y);
double fac3(double x,double y);
double a1,a2,a3,b1,b2,b3,i,j,k;
double *p;
printf("请输入a1,b1的值;");//上下标的值
scanf("%d%d",&a1,&b1);
printf("请输入a2,b2的值;");
scanf("%d%d",&a2,&b2);
printf("请输入a3,b3的值;");
scanf("%d%d",&a3,&b3);
p=fac1; //关键在这,为什么我指针用了double型还是类型不匹配
fac1(a1,b1);
printf("sinx的积分值为%.4lf\n",*p);
p=fac2; //同上
fac2(a2,b2);
printf("cosx的积分值为%.4lf\n",*p);
p=fac3; //同上
fac3(a3,b3);
printf("e^x的积分值为%.4lf\n",*p);
return 0;
}
double fac1(double x,double y)
{
double t;
t=cos(y)-cos(x);
return (t);
}
double fac2(double x,double y)
{
double t;
t=sin(x)-cos(y);
return (t);
}
double fac3(double x,double y)
{
double t;
t=exp(y)-exp(x);
return (t);
}
本帖最后由 superbe 于 2019-10-10 21:26 编辑 #include<stdio.h>
#include<math.h>
int main()
{
double fac1(double x,double y);
double fac2(double x,double y);
double fac3(double x,double y);
double a1,a2,a3,b1,b2,b3,i,j,k;
double (*p)(double x,double y); //声明函数指针,只要把函数原型中的函数名替换为(*p)就可以了
printf("请输入a1,b1的值:");
scanf("%lf%lf",&a1,&b1); //a1,b1是double,使用%lf,不能用%d
printf("请输入a2,b2的值:");
scanf("%lf%lf",&a2,&b2);
printf("请输入a3,b3的值:");
scanf("%lf%lf",&a3,&b3);
p=fac1; //给函数指针赋值
printf("sinx的积分值为%.4lf\n",(*p)(a1,b1)); //调用函数指针,把(*p)或p当成函数名像普通函数一样调用就可以了
p=fac2;
printf("cosx的积分值为%.4lf\n",(*p)(a2,b2)); //p(a2,b2)也可以
p=fac3;
printf("e^x的积分值为%.4lf\n",(*p)(a3,b3));
return 0;
}
|
|