问题一百三十九:函数cos()和sin()
[plain] #include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.1415926
int factorial(int num);
double COS(double x);
double SIN(double x);
int main()
{
double x;
printf("Please enter the Angle:"); //输入要求的角度
scanf("%lf", &x);
printf("sin(x*PI/180)=%lf\n", SIN(x* PI/180));
printf("cos(x*PI/180)=%lf\n", COS(x* PI/180));
system("PAUSE");
return 0;
}
//The definition of the function
//**factorial function 阶乘函数
int factorial(int num)
{
int sum;
sum=0;
if(num==0 || num==1)
{
sum=1;
}
else
{
sum=num*factorial(num-1);
}
return sum;
}
//**sin() function sin()函数
double SIN(double x)
{
int i;
int k;
double sum;
k=1;
sum=0.0;
for(i=1; pow(x, i) /factorial(i)>0.0000001; i+=2)
{
sum+=k*pow(x, i) /factorial(i);
k=-1*k;
}
return sum;
}
//**cos() function cos()函数
double COS(double x)
{
int i;
int k;
double sum;
k=1;
sum=0.0;
for(i=0; pow(x, i) /factorial(i) >0.0000001; i+=2)
{
sum+=k*pow(x, i)/factorial(i);
k=-1*k;
}
return sum;
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.1415926
int factorial(int num);
double COS(double x);
double SIN(double x);
int main()
{
double x;
printf("Please enter the Angle:"); //输入要求的角度
scanf("%lf", &x);
printf("sin(x*PI/180)=%lf\n", SIN(x* PI/180));
printf("cos(x*PI/180)=%lf\n", COS(x* PI/180));
system("PAUSE");
return 0;
}
//The definition of the function
//**factorial function 阶乘函数
int factorial(int num)
{
int sum;
sum=0;
if(num==0 || num==1)
{
sum=1;
}
else
{
sum=num*factorial(num-1);
}
return sum;
}
//**sin() function sin()函数
double SIN(double x)
{
int i;
int k;
double sum;
k=1;
sum=0.0;
for(i=1; pow(x, i) /factorial(i)>0.0000001; i+=2)
{
sum+=k*pow(x, i) /factorial(i);
k=-1*k;
}
return sum;
}
//**cos() function cos()函数
double COS(double x)
{
int i;
int k;
double sum;
k=1;
sum=0.0;
for(i=0; pow(x, i) /factorial(i) >0.0000001; i+=2)
{
sum+=k*pow(x, i)/factorial(i);
k=-1*k;
}
return sum;
}
补充:软件开发 , C语言 ,