求函数的值;程序能够运行起来,但是结果是错误,显示 0.0000 ;
#include "stdio.h"#include "math.h"
double area(double a,double b,double (*p)(double x));
double fun1(double x);
double fun2(double x);
double fun3(double x);
double fun4(double x);
main()
{
double s;
double a,b;
int fun;
scanf("%f%f",&a,&b);
printf("the funtion is ");
scanf("%d",&fun);
switch(fun)
{
case 1:s=area(a,b,fun1); printf("%lf",s); break;
case 2:s=area(a,b,fun2); printf("%lf",s);break;
case 3:s=area(a,b,fun3); printf("%lf",s);break;
case 4:s=area(a,b,fun4); printf("%lf",s); break;
}
}
double area(double a,double b,double (*p)(double x))
{
int i;
double area,s=0,y1,y2,x,h;
h=0.0001;
x=a;
y1=(*p)(x);
for(i=1;a+i*h<b;i++) {
x=a+i*h;
y2=(*p)(x);
area=(y1+y2)*h/2;
y1=y2;
s=s+area;}
return s;
}
double fun1 (double x) {
double f;
f=x*x-2*x+2;
return f;
}
double fun2 (double x) {
double f;
f=x*x*x+3*x*x-x+2;
return f;
}
double fun3 (double x) {
double f;
f=x*sqrt(x+cos(2*x));
return f;
}
double fun4 (double x) {
double f;
f=1/(1+x*x);
return f;
}
上面的程序能够运行起来,没有编译器发现错误和警告,但是运行的结果是0.0000,不能显示正确的结果。求大侠解。