哪位可以帮忙编辑一下这个C语言,非常感谢

来源:百度知道 编辑:UC知道 时间:2024/09/21 19:42:50
Write a program, that after the entering of a real number x and a real number eps (the accuracy) computes
the value for sin_x (until |Term(n)|<eps complies) , using factoring over n terms.
Print x, sin_x, sin(x) and the n-th term. Use functions Fac() and Term() according to:

sin_x = Term1 + Term2 + Term3 + … = x - x3/3! + x5/5! - x7/7! + …. [ example: Fac(3) = 1.2.3 ]

#include <math.h>
#include <stdio.h>
int Fac(int i)/*求一个数的阶乘,由于阶乘增长太快,eps不要太大*/
{
if(i==0||i==1)
return 1;
return i*Fac(i-1);
}
double Term(double x, int i)/*计算一个数所在的termi的值*/
{
int sign(1);
int i2sub1=2*i-1;
if(i%2==0)
sign=-1;
return sign*(i2sub1)/(double)Fac(i2sub1);
}
int main()
{

double x,eps;
double sum=0;
int i;

printf("Please input your x and the eps:\t");
scanf("%lf%lf",&x,&eps);
for (i=1; i<eps; ++i)
{
sum+=Term(x,i);
}
printf("x is %lf, sin_x is %lf, and sin(x) is %lf\n", x, sum, sin(x));
return 0;
}