c语言cos函数求和 c语言按公式计算cosx值

C语言中 COS()的用法

cos()是库函数,在头文件math.h中,原型是double cos(double x);,其中x要用弧度表示。如求30°的余弦值可用下列代码实现:

我们一直强调网站建设、做网站对于企业的重要性,如果您也觉得重要,那么就需要我们慎重对待,选择一个安全靠谱的网站建设公司,企业网站我们建议是要么不做,要么就做好,让网站能真正成为企业发展过程中的有力推手。专业网站设计公司不一定是大公司,创新互联建站作为专业的网络公司选择我们就是放心。

//#include "stdafx.h"//If the vc++6.0, with this line.

#include "stdio.h"

#include "math.h"

int main(void){

printf("cos30°= %.10f\n",cos(30*3.1415926535897932/180));

return 0;

}

用c语言求cos(x)的近似值

我用VS2010,没错啊

// Cos.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include "stdio.h"

#include "math.h"

int fact(int x)

{

int s=1;

int i;

for(i=1;i=x;i++)

s=s*i;

return s;

}

double funcos(double e, double x);

int _tmain(int argc, _TCHAR* argv[])

{

double e, sum, x;

scanf("%le%le", e, x);

sum=funcos(e,x);

printf("sum = %f\n", sum);

return 0;

}

double funcos(double e, double x)

{

double s=1;

double item;

int i=2;

int j=1;

item=(double)pow(x,i)/fact(i);

while(iteme)

{

if(j%2==1)

s=s-item;

else

s=s+item;

i=i+2;

j++;

item=(double)pow(x,i)/fact(i);

}

if(j%2==1)

s=s-item;

else

s=s+item;

return s;

}

c语言编程 cos(x)=1-x^2/2!+x^3/3!-x^4/4!+......-x^10/10!

下面是代码 ,我测试过是对的,希望我的努力对你有帮助。

#include stdio.h

#include math.h

/*求阶乘函数*/

int jiecheng(int n)

{

if(n 0){

printf("错误的数字\n");

return -1;

}

else if( n == 0 || n== 1){

return 1;

}

else{

return jiecheng(n-1)*n;

}

}

/*自己写的cos 函数*/

double mycos(double x)

{

int i = 2;

double sum = 1.0f;

int k = -1;

while(i=10){

sum += k*pow(x,i)/jiecheng(i); //pow 是 math.h 里的函数 求x的i次方

k = -k;

i++;

}

return sum;

}

void main()

{

double x = 3.0f;

printf("%d\n",jiecheng(2)); //测试阶乘

printf("%f\n",mycos(x));

printf("%f\n",cos(x)); //库函数里的cos函数 用来对比

}


当前标题:c语言cos函数求和 c语言按公式计算cosx值
文章链接:http://ybzwz.com/article/doggdps.html