#define _CRT_SECURE_NO_DEPRECATE
#include
double integrate(double, double, int);
double equa(double); /*函數說明,背積函數,具體函數可替換*/
float a, b, c;
void main() {
double leftlimit, rightlimit;/*積分上限,下限*/
int n;/*梯形劃分數*/
printf("請輸入被積分函數的系數:a,b,c:\n");
scanf("%f%f%f", &a, &b, &c);
printf("請輸入被積分函數的上限及下限:\n");
do {
scanf("%lf%lf", &leftlimit, &rightlimit);
if (leftlimit > rightlimit) printf("積分下限應該小於上限,請重新輸入:");
} while (leftlimit < rightlimit);
printf("請輸入被積分函數的劃分數(要求正整數):\n");
getchar();
scanf("%d", &n);
if (n < 0) n = -n;/*對用戶輸入的負整數進行取正*/
printf("積分結果為=%.2f\n", integrate(leftlimit, rightlimit,n));
system("pause");
}
/*積分求解函數*/
double integrate(double lower, double upper, int n) {
double h, si, area = 0;
int i;
h = (upper - lower) / n;
for (i = 0;i < n;i++) {
si = h*(equa(lower + i*h) + equa(lower + (i + 1)*h)) / 2;
area = si + area;
}
return area;
}
/*被積函數*/
double equa(double xi) {
return a*xi*xi + b*xi + c;
}
這段函數寫的有問題
do
{
scanf("%lf%lf", &leftlimit, &rightlimit);
if (leftlimit > rightlimit) printf("積分下限應該小於上限,請重新輸入:");
}
while (leftlimit < rightlimit);