【C和C++】 指针求定积分
#include<stdio.h> #include<math.h> double f1(float b,double l,int i) { double k; k=sin(b+l*i); return k; } double f2(float b,double l,int i) { double k; k=cos(b+l*i); return k; } double f3(float b,double l,int i) { double k; k=exp(b+l*i); return k; } double f4(float b,double l,int i) { double k; k=1+(b+i*l)*(b+i*l); return k; } double f5(float b,double l,int i) { double k; k=exp(b+l*i)+1; return k; } double f6(float b,double l,int i) { double k; k=2*(b+l*i)+3; return k; } double f7(float b,double l,int i) { double k; k=1+(b+l*i); return k; } double f8(float b,double l,int i) { double k; k=(b+i*l)*(b+i*l)*(b+i*l); return k; } int main() { int c; float a,b; double num; double (*p)(float,double,int); int i,j; double result,h,l; double sum=0; puts("1 (sinx)\n2 (cosx)\n3 (e^x)\n4 (1+x^2)\n5 (e^x+1)\n6 (2*x+3)\n7 (1+x)\n8 (x^3)\n"); to: printf("please enter a b\n"); scanf("%f%f",&a,&b); printf("a=%f,b=%f\n",a,b); fflush(stdin); printf("choose a number to use function\n"); scanf("%d",&c); switch(c) { case 1:p=f1;break; case 2:p=f2;break; case 3:p=f3;break; case 4:p=f4;break; case 5:p=f5;break; case 6:p=f6;break; case 7:p=f7;break; case 8:p=f8;break; default:printf("inlegal number please try again!\n"); } l=(a-b)/1000; for(i=0;i<1000;i++) { h=(*p)(b,l,i); result=h*l; sum+=result; } printf("\n%.2lf",sum); printf("\nNow you can use another function\n"); goto to; return 0; }