cg_Amaz1ng
2015-09-04 07:38
采纳率: 60%
浏览 2.2k

C语言 关于用矩形法求定积分

#include"stdio.h"
#include"math.h"
int main(){
double fun1(double x);
double fun2(double x);
double fun3(double x);
double calc(double a,double b,double (*p)(double));
int type;
double a,b;
double (*p)(double);
printf("请输入序号:\n1、sin\n2、cos\n3、exp\n0、退出\n");
scanf("%d",&type);
while(type=4){
printf("输入错误,重新输入\n");
scanf("%d",&type);
}
switch(type){
case 1:p=fun1;
break;
case 2:p=fun2;
break;
case 3:p=fun3;
break;
case 0:return 0;
break;
}
printf("输入积分下限:");
scanf("%lf",&a);
printf("输入积分上限:");
scanf("%lf",&b);
printf("%lf\n",calc(a,b,p));
return 0;
}

double fun1(double x){

return sin(x);
}
double fun2(double x){
return cos(x);
}
double fun3(double x){
return exp(x);
}
double calc(double a,double b,double (*p)(double)){
double length,i,sum=0,size;
length=(b-a)/10000000;
for(i=a;i<b;i+=length){
size=(*p)(i)*length;
sum+=size;
}
return sum;
}

当我使用exp的时候,计算结果正确,而使用sin 和 cos 则不行,请问哪里出了错?

3条回答 默认 最新

相关推荐 更多相似问题