📄 sum.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// #define f(x) (sqrt(1 - exp(-x)) / x)
// #define f(x) sqrt(4 - sin(x) * sin(x))
// #define PI 3.1415926535897932384626433832795
// #define f(x) (x * exp(x) / (1 + x) / (1 + x))
float f(float x)
{
return sqrt(1 - exp(-x)) / x;
}
float sumS(float a, float b, int n, float *x)
{
float tmp1, tmp2;
int i;
tmp1 = 0; tmp2 = 0;
n /= 2;
for(i = 0; i <= n - 1; i++)
{
tmp1 += f(x[2 * i + 1]);
}
tmp1 *= 4;
for(i = 1; i <= n - 1; i++)
{
tmp2 += f(x[2 * i]);
}
tmp2 *= 2;
return (b - a) / 6 / n * (f(a) + tmp1 + tmp2 + f(b));
}
float sumT(float a, float b, int n, float *x)
{
float tmp;
int i;
tmp = 0;
for(i = 1; i <= n - 1; i++)
{
tmp += f(x[i]);
}
tmp *= 2;
return (b - a) / 2 / n * (f(a) + tmp + f(b));
}
void main(void)
{
int i, n;
float a, b, h;
float* x;
float s, t;
printf("a="); scanf("%f", &a);
printf("b="); scanf("%f", &b);
// a = 0; b = PI / 6;
printf("n="); scanf("%d", &n);
n *= 2;
h = (b - a) / n;
printf("h=%f\n", h);
x = (float*)calloc(n + 1, sizeof(float));
for(i = 0; i <= n; i++)
{
x[i] = a + i * h;
}
for(i = 0; i <= n; i++)
{
printf("x%d=%f\tf(x%d)=%f\n", i, x[i], i, f(x[i]));
}
s = sumS(a, b, n, x);
t = sumT(a, b, n, x);
printf("t%d=%f\n", n, t);
printf("s%d=%f\n", n / 2, s);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -