C语言中矩形公式计算定积分的通用函数实现
#include <stdio.h> #include <math.h>
double integral(double (*f)(double), double a, double b);
double f_sin(double x); double f_cos(double x); double f_exp(double x);
int main() { double a, b; scanf("%lf %lf", &a, &b); printf("%.6f %.6f %.6f", integral(f_sin, a, b), integral(f_cos, a, b), integral(f_exp, a, b)); return 0; }
double integral(double (*f)(double), double a, double b) { double h = (b - a) / 1000.0; double s = 0.0; for (int i = 0; i < 1000; i++) { s += f(a + (i + 0.5) * h) * h; } return s; }
double f_sin(double x) { return sin(x); }
double f_cos(double x) { return cos(x); }
double f_exp(double x) { return exp(x); }
原文地址: https://www.cveoy.top/t/topic/nwGs 著作权归作者所有。请勿转载和采集!