83 date:2021.2.23
要点:
求绝对值函数:
fabs() 浮点型
abs() 整型
详细代码如下:
#include <stdio.h>
#include <math.h>
double fun(double x)
{
double f, t; int n;
f = 1.0 + x;
t= x;
n = 1;
do {
n++;
t*=(-1.0)*x/n;
f += t;
}
while(fabs(t) >=1e-6);
return f;
}
void main()
{
double x, y;
x=2.5;
y = fun(x);
printf("\nThe result is :\n");
printf("x=%-12.6f y=%-12.6f\n", x, y);
}
要点:
详细代码如下:
#include <math.h>
#include <stdio.h>
double fun(double x)
{
/*
analyse:
……
*/
double s1 = 1.0, p = 1.0, sum = 0.0, s0, t =1.0;
int n = 1;
do{
s0 = s1;
sum += s0;
t *= n;
p *= (0.5-n+1)*x;
s1 = p/t;
n++;
}while(fabs(s1-s0) >= 1e-6);
return sum;
}
void main()
{
int i;
double x,s;
FILE *out;
printf("Input x: ");
scanf("%lf",&x);
s=fun(x);
printf("s=%f\n ",s);
/******************************/ /*这里包含输出文件程序*/
out=fopen("out.dat","w");
for(i=20;i<30;i++)
fprintf(out,"%f\n",fun(i/100.0));
fclose(out);
/******************************/
}