计算x - (1/2)*(x3/4) + (1/2)*(3/4)*(x5/6) - (1/2)*(3/4)*(5/6)*(x7/8) + …
编写一个程序,计算上式的近似值(直到最后一项的绝对值小于eps,其中x和eps是键盘输入,且|x|<1.0),并将结果和自己的学号(学号按字符串处理)写入到out.dat文件中保存起来。
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
int n = 2;
double eps, t, s = 0, x;
FILE *fp;
if((fp = fopen("out.dat","w+")) == NULL)
{
printf("Don't open the file!\n");
exit(0);
}
scanf("%lf,%lf",&x,&eps);
t = x;
s = t;
while(fabs(t) >= eps)
{
t = -t * (2 * n - 3) * x * x / (2 * n - 2);
s = s + t / (2 * n);
n++;
}
printf("%d,%lf\n",n,s);
fprintf(fp,"%d ,%.2lf\n",n,s);
fprintf(fp,"%s","20151234567");
fclose(fp);
return 0;
}