C语言:输入精度e 和实数x,用下列公式求cos x 的近似值,精确到最后一项的绝对值小于e懿

2024-12-14 18:50:32
推荐回答(1个)
回答1:

#include
#include
double funcos(double x,double e);
int main(void)
{
double n,x,e;
scanf("e: %lf%*c",&e);
scanf("x: %lf%*c",&x);
n=funcos(x,e);
printf("cos(x)=%.3lf",n);
return 0;
}
double funcos(double x,double e){
int i,k,flag=1;
double sum,result=0,fact=1;
for(i=0;;i+=2){
sum=x ;
fact=1;
for(k=1;k<=i;k++)
{
sum *=x ;
fact=fact*k;
}
sum/=fact;
result=result+flag*sum;
if(sum break;
flag=-flag;
}
return result;
}