首页 > 代码库 > P290.13 实验报告 指针求定积分
P290.13 实验报告 指针求定积分
#include<stdio.h>
#include<math.h>
double f1(float a,float b)
{ int i;
double result,h,l;
double sum=0;
l=(a-b)/1000;
for(i=0;i<1000;i++)
{ h=sin(b+l*i)+sin(b+l*(i++));
result=h*l;
sum+=result; }
return sum; }
double f2(float a,float b)
{ int i;
double result,h,l;
double sum=0;
l=(a-b)/1000;
for(i=0;i<1000;i++)
{ h=cos(b+l*i)+cos(b+l*(i++));
result=h*l;
sum+=result; }
return sum; }
double f3(float a,float b)
{ int i;
double result,h,l;
double sum=0;
l=(a-b)/1000;
for(i=0;i<1000;i++)
{ h=exp(b+l*i)+exp(b+(l*(i++)));
result=h*l;
sum+=result; }
return sum; }
double f4(float a,float b)
{ int i;
double result,h,l;
double sum=0;
l=(a-b)/1000;
for(i=0;i<1000;i++)
{ h=1+(b+i*l)*(b+i*l)+1+(b+l*(i++))*(b+l*(i++));
result=h*l;
sum+=result; }
return sum; }
double f5(float a,float b)
{ int i;
double result,h,l;
double sum=0;
l=(a-b)/1000;
for(i=0;i<1000;i++)
{ h=exp(b+l*i)+exp(b+((i++)*l))+2;
result=h*l;
sum+=result; }
return sum; }
double f6(float a,float b)
{ int i;
double result,h,l;
double sum=0;
l=(a-b)/1000;
for(i=0;i<1000;i++)
{ h=(2*(b+l*i)+2+2*(b+l*(i++))+2);
result=h*l;
sum+=result; }
return sum; }
double f7(float a,float b)
{ int i;
double result,h,l;
double sum=0;
l=(a-b)/1000;
for(i=0;i<1000;i++)
{ h=(1+b+l*i)+(1+b+l*(i++));
result=h*l;
sum+=result; }
return sum; }
double f8(float a,float b)
{ int i;
double result,h,l;
double sum=0;
l=(a-b)/1000;
for(i=0;i<1000;i++)
{ h=(b+i)*(b+i*l)*(b+i*l)+(b+l*(i++))*(b+l*(i++))*(b+l*(i++));
result=h*l;
sum+=result; }
return sum; }
void intergrat(float a,float b,double (*fun)(float,float))
{ printf("the result is %.2lf\n",(*fun)(a,b)); }
int main()
{ int c;
float a,b;
double num;
double (*p)(float,float);
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;intergrat(a,b,p);break;
case 2:p=f2;intergrat(a,b,p);break;
case 3:p=f3;intergrat(a,b,p);break;
case 4:p=f4;intergrat(a,b,p);break;
case 5:p=f5;intergrat(a,b,p);break;
case 6:p=f6;intergrat(a,b,p);break;
case 7:p=f7;intergrat(a,b,p);break;
case 8:p=f8;intergrat(a,b,p);break;
default:printf("inlegal number please try again!\n"); }
printf("\nNow you can use another function\n");
goto to;
return 0; }
P290.13 实验报告 指针求定积分