级数求和算法c++

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;

void input(double * x)
{
	cout<<"0.0<=x<=40.0"<<endl;
	cin>>*x;
}

double db(double x)
{
	int i;
	double sum=1.0;
	double time=-1.0;
	for(i=1;abs(time)>=1e-6;i++)
	{
		time*=(-1.0)*x/i;			//每次计算一个然后判断	
		sum+=time;
	}
	return sum; 
}

void printf(double s)
{
	cout<<setprecision(7)<<s<<endl;
}
main()
{
	double x,y;
	input(&x);
	y=db(x);
	printf(y);
}

posted @ 2019-09-26 17:17  JackieDYH  阅读(6)  评论(0编辑  收藏  举报  来源