In this paper, we illustrate the method of C language to achieve the definite integral. Share to everyone for your reference. The implementation methods are as follows:
Copy Code code as follows:
#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 ("Please select integrand: \ n"), printf ("1, exp (x) 2, x+1 \ n"), scanf ("%c", &selection), Selection!= ' # ') {
printf ("Please enter the lower limit of the points:");
scanf ("%f,%f", &up,&down);
Switch (selection) {
Case ' 1 ':
printf ("Result:%4.4f\n", Solve (FUN_EXP,UP,DOWN,ACC));
Break
Case ' 2 ':
printf ("Result:%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) {//unary two-time equation quadratics
return x+1;
}
I hope this article will help you with the C language program.