gpt4 book ai didi

C 错误 : "Called error is not a function or function pointer"

转载 作者:太空宇宙 更新时间:2023-11-03 23:40:08 25 4
gpt4 key购买 nike

为了我的编程作业,我必须编写一个计算函数积分的程序

main.c 文件:

    #include <stdio.h>
#include <math.h>
#include "integrand.h"
#define M 64
#define N 64
#define dif 0.01
#define max 8

float (*pf)(float,float);

void integrate(float pf(float x,float y)){
int i,j,k=0,l=1;
float a,b,c,d,dx,dy,dA,x[i],y[j],r=0,r_old;

printf("Enter the integration bounds a,b,c,d separated by commas: \n\n");scanf("%f,%f,%f,%f",&a,&b,&c,&d);

do{
r_old = r;

dx = (b - a)/(l*M);
dy = (d - c)/(l*N);
dA = dx * dy;

for(i=0;i<(l*M)-1;i++){
x[i] = a + (i + 0.5)*dx;
}

for(j=0;j<(l*N)-1;j++){
y[j] = c + (j + 0.5)*dy;
}

for(i=0;i<(l*M)-1;i++){
for(j=0;j<(l*N)-1;j++){
r+=(((*pf)(x[i],y[j]))*dA);
}}

k++;
l*=2;
}while(k<max || abs(r-r_old)<=dif*abs(r));

printf("Integral = %f",r);

return;
}

int main(void)
{
float x,y;
int q;

printf("Choose an integrand:\n\n (1) f_1(x,y) = x^3 + 3y^2*x + 7x^2*y + 10 \n\n (2) f_2(x,y) = cos(0.1*x^5 + y^2) - sin(y*x)^5 + log(|x-y|+1) + 2 \n\n (3) f_3(x,y) = sin(x^2+y^2) + exp(-50*(x^2+y^2))\n\n (4) f_4(x,y) = f_3(f_1(x,y),f_2(x,y))\n\n (5) QUIT \n\n");

do{
scanf("%d",&q);
switch(q){
case 1:
pf = f_1;
integrate((&pf)(x,y));
break;
case 2:
pf = f_2;
integrate((&pf)(x,y));
break;
case 3:
pf = f_3;
integrate((&pf)(x,y));
break;
case 4:
pf = &f_4;
integrate((&pf)(x,y));
break;

}}while(q!=5);

return 0;
}

积分函数.c文件:

    #include <math.h>

float f_1(float x, float y){return (pow(x,3) + 3*pow(y,2)*x + 7*pow(x,2)*y + 10);}

float f_2(float x, float y){return (cos(0.1*pow(x,5)+pow(y,2)) - pow(sin(y*x),5) + log(fabs(x-y)+1) + 2);}

float f_3(float x, float y){return (sin(pow(x,2)+pow(y,2)) + pow(M_E,-50*(pow(x,2)+pow(y,2))));}

float f_4(float x, float y){return (f_3(f_1(x,y),f_2(x,y)));}

积分函数.h文件:

    #ifndef INTEGRAND_H
#define INTEGRAND_H

extern float f_1(float x, float y);
extern float f_2(float x, float y);
extern float f_3(float x, float y);
extern float f_4(float x, float y);

#endif // INTEGRAND_H

标题中的错误在所有四种情况下都出现在这部分:

    integrate((&pf)(x,y));

我无法弄清楚问题到底出在哪里,我试过将 & 换成 * 或完全删除它,但这只会导致错误,指出参数类型与“集成”的参数 1 不兼容

最佳答案

pf 是一个函数指针,你需要将它传递给 integrate 而不需要任何额外的参数。由于您在所有四个分支中以相同的方式调用函数,因此将调用移至 switch 之外的 integrate:

do{
scanf("%d", &q);
switch(q){
case 1: pf = f_1; break;
case 2: pf = f_2; break;
case 3: pf = f_3; break;
case 4: pf = f_4; break;
default: pf = NULL; break;
}
if (pf) {
integrate(pf);
}
} while (q != 5);

这种 switch 的使用模式也可以用数组代替。

关于C 错误 : "Called error is not a function or function pointer",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48093995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com