用c语言程序编写用下面中间公式方法求正弦和余弦的方法 并定义为函数 在main里面调用
1个回答
展开全部
#include <stdio.h>
#include <math.h>
#define P 3.1415927
double sinx(double x) {
double eps = 1E-8,delta = x;
double sgn = -1,sin = x,n = 1;
double nator = 1;
while(fabs(delta) > eps) {
nator *= (n + 1.0)*(n + 2.0);
delta *= x*x/nator;
sin += sgn*delta;
sgn = -sgn;
n += 2;
}
return sin;
}
double cosx(double x) {
double eps = 1E-8,delta = 1;
double sgn = -1,cos = 1,n = 1;
double nator = 1;
while(fabs(delta) > eps) {
nator *= n*(n + 1.0);
delta *= x*x/nator;
cos = cos + sgn*delta;
sgn = -sgn;
n += 2;
}
return cos;
}
int main() {
double alpha;
int T;
printf("测试次数:");
scanf("%d",&T);
while(T--) {
printf("请输入度数:");
scanf("%lf",&alpha);
alpha *= P/180;
printf("msin(%lf) = %lf\n",alpha,sinx(alpha));
printf("sin(%lf) = %lf\n",alpha,sin(alpha));//调用系统函数用于对比
printf("mcos(%lf) = %lf\n",alpha,cosx(alpha));
printf("cos(%lf) = %lf\n",alpha,cos(alpha));//调用系统函数用于对比
}
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询