求sin的:参考下 #includestdio.h void main() { double x,a,b,sum=0; printf("请输入x的弧度值:\n"); scanf("%lf",x); int i,j,count=0; for(i=1;;i+=2) { count++; a=b=1; for(j=1;j=i;j++) { a*=x; b*=(double)j; } if(a/b0.0000001) break; else { if(count%2==0) sum-=a/b; else sum+=a/b; } } printf("%lf\n",sum); }
成都创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站制作、成都网站建设、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的来宾网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
math.h里的三角函数用的单位是弧度,你貌似错在这里。 答案补充 Example
/* SINCOS.C: This program displays the sine, hyperbolic
* sine, cosine, and hyperbolic cosine of pi / 2.
*/
#include math.h
#include stdio.h
void main( void )
{
double pi = 3.1415926535;
double x, y;
x = pi / 2;
y = sin( x );
printf( "sin( %f ) = %f\n", x, y );
y = sinh( x );
printf( "sinh( %f ) = %f\n",x, y );
y = cos( x );
printf( "cos( %f ) = %f\n", x, y );
y = cosh( x );
printf( "cosh( %f ) = %f\n",x, y );
} 答案补充 Output
sin( 1.570796 ) = 1.000000
sinh( 1.570796 ) = 2.301299
cos( 1.570796 ) = 0.000000
cosh( 1.570796 ) = 2.509178
Parameter
x
Angle in radians
调用math.h中的三角函数,需要将角度值变换为弧度值,代码如下:
#includestdio.h
#includemath.h
#define PI 3.14159265359
int main()
{
float st,a;
scanf("%f",st);
a = st * PI/180;
printf("sin(st)=%f\n", sin(a));
printf("cos(st)=%f\n", cos(a));
return 0;
}
从键盘输入一个角度值,求出该角度的正弦值、余弦值和正切值。
#includeiostream
#includecmath
using namespace std;
const double pi(3.14159265);
void main()
{ double a,b;
cina;
b=a*pi/180;
cout"sin("a")="sin(b)endl;
cout"cos("a")="cos(b)endl;
cout"tan("a")="tan(b)endl;
}
求阶乘
#includeiostream.h
int Factorial ( int ) ;
void main ()
{ int k ;
cout "Compute Factorial(k) , Please input k: " ;
cin k ;
cout k "! = " Factorial(k) endl ;
}
int Factorial ( int n )
{ if ( n == 0 )
return 1 ;
else
return n * Factorial ( n - 1 ) ;
}
x的n次方的函数
#include iostream
using namespace std;
double power (double x, int n);
void main(void)
{
cout "5 to the power 2 is " power(5,2) endl;
}
double power (double x, int n)
{
double val = 1.0;
while (n--)
val = val*x;
return(val);
}