级数求和算法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);
}
本文来自博客园,作者:JackieDYH,转载请注明原文链接:https://www.cnblogs.com/JackieDYH/p/17635023.html