/*------------------------------------------------
【程序设计】
--------------------------------------------------
功能:根据整型形参m,计算如下公式的值:y=1/2+1/4+
1/6+...+1/2m 。
例如:若m=9,则应输出:1.414484
------------------------------------------------*/
#include <stdio.h>
void wwjt();
double fun(int m)
{
/**********Program**********/
double y=0;
int i;
for(i=1;i<=m;i++){
y+=1.0/(2*i);
}
return y;
/********** End **********/
}
main()
{
int n;
printf("Enter n: ");
scanf("%d", &n);
printf("\nThe result is %1f\n", fun(n));
wwjt();
}
void wwjt()
{
FILE *IN,*OUT;
int i ;
int t;
double o;
IN=fopen("in.dat","r");
if(IN==NULL)
{
printf("Read FILE Error");
}
OUT=fopen("out.dat","w");
if(OUT==NULL)
{
printf("Write FILE Error");
}
for(i=0;i<5;i++)
{
fscanf(IN,"%d",&t);
o=fun(t);
fprintf(OUT,"%f\n",o);
}
fclose(IN);
fclose(OUT);
}