📄 no.8.c
字号:
#include "stdio.h"
#include "conio.h"
#define ROOT1_PRECISION 10e-4
#define ROOT2_PRECISION 10e-3
#define PI 3.1415926
float x1(float x)
{
return x;
}
float x2(float x)
{
return ( ( 2 * x) / ( 1 - x) );
}
float ln(float (*x)(float a), float a, float precision, int* n)
{
int flag = 1;
int i = 0;
float fx = 1;
float t = x(a);
float s = 0;
while ( t > precision ) {
i++;
fx = fx * x(a);
t = fx / i;
s = s + flag * t;
flag = - flag;
}
*n = i;
return s;
}
float ex(float (*x)(float a), float a, float precision)
{
float fx = 1;
float fy = 1;
float i = 0;
float t = 1;
float s = 1;
while ( t > precision ) {
i++;
fy = fy * i;
fx = fx * x(a);
t = fx / fy;
s = s + t;
}
return s;
}
int main()
{
float s;
int n;
float x;
s = ln(x1, 1, ROOT1_PRECISION, &n);
printf("ln(1+x) ln2 = %.4f you should compute %d times.\n", s, n);
s = ln(x2, 1.0 / 3, ROOT1_PRECISION, &n);
printf("ln((1+x)/(1-x)) ln2 = %.4f you should compute %d times.\n", s, n);
printf("Input x:");
scanf("%f", &x);
s = ex(x1, x, ROOT2_PRECISION);
printf("e(x=%.0f) = %.3f", x, s);
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -