float sinx(float x)
{
float term=x,nextterm;
float sum=x;
int r=1, sign=1;
do
{
sign=-sign;
term=term*x*x/(r+1)/(r+2);
nextterm=sign*term;
sum=sum+nextterm;
r=r+2;
}while (fabs(nextterm)>=1e-6);
return sum;
}
void main()
{
float x=0;
double sum=0;
printf("输入一个浮点数x=");
scanf("%f",&x);
sum=sinx(x);
printf("sin(%f)的值为:%5.4f\n",x,sum);
}
看看是否有启发了
----------------解决方案--------------------------------------------------------