新手一个呜呜呜 2021-12-17 20:23 采纳率: 100%
浏览 21
已结题

c语言小练习中的问题

这是题目

img


以下为题目给的模板

#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);
}
运行无法结束,求解怎么改

img

  • 写回答

2条回答 默认 最新

  • 关注
    #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);
    }
    

    img

    如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月26日
  • 已采纳回答 12月18日
  • 创建了问题 12月17日

悬赏问题

  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM