#include "stdio.h"
#include "conio.h"
double collect(double s,double t,int m,double (*p)(double x));
double fun1(double x);
double fun2(double x);
double fun3(double x);
main()
{
int n,flag;
double a,b,v=0.0;
puts("Input the count range(from A to B)and the number of sections\n");
scanf("%lf%lf%d",&a,&b,&n);
puts("enter the function you want to use\n");
scanf("%d",&flag);
if(flag==1)
v=collect(a,b,n,fun1);
else if (flag==2)
v=collect(a,b,n,fun2);
else if (flag==3)
v=collect(a,b,n,fun3);
else
puts("wrong number");
printf("v=%f\n",v);
getch();
return(0);
}
double collect(double a,double b,int n,double (*p)(double x)){
int i;
double f,h,y1,y2,area;
f=0.0;
h=(b-a)/n;
y1=p(a);
for(i=0;i<n;i++){
y2=p(a+i*h+h);
area=(y1+y2)*h/2;
y1=y2;
f+=area;
}
return(f);
}
double fun1(double x){
double fx;
fx=x*x-2*x+2;
return(fx);
}
double fun2(double x){
double fx;
fx=x*x*x+3*x*x-x+2;
return(fx);
}
double fun3(double x){
double fx;
fx=x/(1+x*x);
return(fx);
}
题目是利用梯形法计算定积分,其他代码不用看,只看红色一行。a,b是区间n是分成的小份
如果改为
scanf("%f%f%d",&a,&b,&n);
%f为什么不可以?