这是题目
#include "stdio.h"
#include "math.h"
#include "stdlib.h"
double fun(double x)
{
/begin///这里开始填空
double s=0.0;
double k=0.0,b,c;
b=pow(-1,k-1);
c=b*(1/(2*k-1));
while(1)
{
s=s+c;
if(fabs(c)<x)
break;
k++;
}
return s;
}
/end/
int main()
{
void NONO( );//函数声明
double x,sum;
printf("Enter x:\n");
scanf("%lf",&x);
sum=fun(x);
printf("sum=%.4f\n",sum);
NONO( );
return 0;
}
void NONO( )
{ FILE *fr,*fw;
int i;
double x;
fr=fopen("E:\exam\999999\PROGIN2.DAT","r");
fw=fopen("E:\exam\999999\PROGOUT2.DAT","w");
for(i=1;i<=5;i++)
{ fscanf(fr,"%lf",&x);
fprintf(fw,"s=%.4f\n",fun(x));}
fclose(fr);
fclose(fw);
}
运行无法结束,求解怎么改
#include "stdio.h"
#include "math.h"
#include "stdlib.h"
double fun(double x)
{
/*begin*/ //这里开始填空
double s = 0.0, k = 1, c;
int b = 1;
while (1)
{
c = b * (1 / (2 * k - 1));
s = s + c;
b = -b;
if (fabs(c) < x)
break;
k++;
}
return s;
}
/*end*/
int main()
{
void NONO(); //函数声明
double x, sum;
printf("Enter x:\n");
scanf("%lf", &x);
sum = fun(x);
printf("sum=%.4f\n", sum);
NONO( );
return 0;
}
void NONO()
{
FILE *fr, *fw;
int i;
double x;
fr = fopen("E:\\exam\\999999\\PROGIN2.DAT", "r");
fw = fopen("E:\\exam\\999999\\PROGOUT2.DAT", "w");
for (i = 1; i <= 5; i++)
{
fscanf(fr, "%lf", &x);
fprintf(fw, "s=%.4f\n", fun(x));
}
fclose(fr);
fclose(fw);
}
如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!
for(i=1;i<=5;i++)
循环输入5次。