用c++求函数

来源:百度知道 编辑:UC知道 时间:2024/05/28 04:47:08
高手帮忙~!
1.用c++求sinx的函数(利用数学公式求)
2.用c++求cosx的函数(利用数学公式求解)

//求sin(x)的值
#include<iostream.h>
int power(int t)
{
long s=1;
for(long i=1;i<=t;i++)
s=s*i;
return s;
}
double sin(double x)
{
long i,m=-1;
double s=0.0,p=1/x;
for(i=1;i<30;i=i+2)
{
p=p*x*x;
m=-m;
s=s+m*p/power(i);
}
return s;
}
void main()
{
double p;
cout<<"输入一个角度:";
cin>>p;
double s=p*3.1415926/180;
cout<<"sin("<<p<<")的值:"<<sin(s)<<endl;
}
//注意此过程有可能overflow,在<math.h>在有这个函数sin(x)可以直接调用,还有你题目问的问题有点矛盾,那个本来就是sin(x)是泰勒展开式,恒等式何来求x,可不要在犯低级错误,怎么我这样回答你满意吗?

//
/求cos(x)的值
//
//long factor(int n)用int (long)计算阶乘, 很容易超过long的表示范围,
//因为是近似计算, 改用double吧.

#include <stdio.h>
#include <stdlib.h>

double pow(d