C语言实现求定积分的方法
本文实例讲述了C语言实现求定积分的方法。分享给大家供大家参考。具体实现方法如下:
#include <cmath>
#include <cstdio>
#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;
}
以上是 C语言实现求定积分的方法 的全部内容, 来源链接: utcz.com/z/357319.html