本文实例讲述了C语言实现求定积分的方法。分享给大家供大家参考。具体实现方法如下:
#define ACC 1000
float solve(float (*p)(float),float up,float down,int acc);
float fun_exp(float x);
float fun_qua(float x);
void main(){
char selection;
float up,down;
while(printf(\"请选择被积函数:\\n\"),printf(\"1、exp(x) 2、x+1 \\n\"),scanf(\"%c\",&selection),selection != \'#\'){
printf(\"请输入积分上下限: \");
scanf(\"%f,%f\",&up,&down);
switch(selection){
case \'1\':
printf(\"结果是: %4.4f\\n\",solve(fun_exp,up,down,ACC));
break;
case \'2\':
printf(\"结果是: %4.4f\\n\",solve(fun_qua,up,down,ACC));
break;
}
}
}
float solve(float (*p)(float),float up,float down,int acc){
float sum,base,area;
area = 0;
sum = 0;
base = (up-down)/acc;
for(int i=0; i
area = base*((*p)(down+i*base));
sum+=area;
}
return sum;
}
float fun_exp(float x){
return exp(x);
}
float fun_qua(float x){ //一元二次方程 quadratics
return x+1;
}
本文地址:https://www.stayed.cn/item/6045
转载请注明出处。
本站部分内容来源于网络,如侵犯到您的权益,请 联系我