#include <iostream>
using namespace std;
const double pi=3.1415926;
double mysin(double a)
{
double n=1;
double r=1;
// int x=a;
double y=a;
double sum=a;
double sign=-1;
while(y/r*sign>0.00001||y/r*sign<-0.00001)//(!(y/r*sign>-0.00001&&y/r*sign<0.00001))
{
r=r*(n+1)*(n+2);
y=y*a*a;
// cout<<"r="<<r<<endl;
sum+=y/r*sign;
sign=-sign;
n=n+2;
// cout<<"sum="<<sum<<endl;
// cout<<"sign="<<sign<<endl;
// cout<<"n="<<n<<endl;
// cout<<"y="<<y<<endl;
// cout<<"y/r="<<y/r<<endl;
}
return sum;
}
double mycos(double a)
{
double n=1;
double r=1;
// int x=a;
double y=1;
double sum=1;
double sign=-1;
while(y/r*sign>0.00001||y/r*sign<-0.00001)
{
r=r*(n)*(n+1);
y=y*a*a;
sum+=y/r*sign;
sign=-sign;
n=n+2;
}
return sum;
}
int main( )
{
cout<<"sin(π/2)的值为:"<<mysin(pi/2)<<endl;
cout<<"sin(56°)的值为:"<<mysin((56.0/180)*pi)<<endl;
cout<<"cos(87°)的值为:"<<mycos((87.0/180)*pi)<<endl;
cout<<"cos(π/3)的值为:"<<mycos(pi/3)<<endl;
return 0;
}
//下面定义mysin函数,求sin值
//下面定义mycos函数,求cos值
//下面定义myabs函数,求绝对值
项目2-太乐了
最新推荐文章于 2016-03-29 20:13:56 发布