C语言编程>第十三周 ⑥ 请编写函数fun,其功能是:计算并输出当x<0.97时下列多项式的值,直到|Fn-Fn-1|<0.000001为止。

例题:请编写函数fun,其功能是:计算并输出当x<0.97时下列多项式的值,直到|Fn-Fn-1|<0.000001为止。
Fn=1+0.5x+ x2+ x3+…+ xn

例如,若主函数从键盘给x输入0.33后,则输出为f=1.153256
请勿改动主函数main与其它函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。

代码如下:

#include<stdio.h>
#include<math.h>
double fun(double x)
{
    
    
	double f1=1.0,p=1.0,sum=0.0,f0,t=1.0;
	int n=1;
	do
	{
    
    
		f0=f1;
		sum+=f0;
		t*=n;
		p*=(0.5-n+1)*x;
		f1=p/t;
		n++;
	}while(fabs(f1-f0)>=1e-6);
	return sum;
}
main()
{
    
    
	int i;
	double x,f;
	FILE*out;
	printf("INput x: ");
	scanf("%lf",&x);
	f=fun(x);
	printf("f=%f\n",f);
	out=fopen("outfile.dat","w");
	for(i=20;i<30;i++)
	fprintf(out,"%f\n",fun(i/100.0));
	fclose(out);
}

输出运行窗口如下:
在这里插入图片描述

越努力越幸运!
加油,奥力给!!!

猜你喜欢

转载自blog.csdn.net/qq_45385706/article/details/111994941
今日推荐