C语言求定积分 C语言实现求定积分的方法
Chance 人气:10本文实例讲述了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语言程序设计有所帮助。
加载全部内容