在调用三角函数之前先把角度换算成弧度,调用反三角函数之后把弧度换算成角度就可以了.可以用 pi = 4.0 * atan(1) 算出pi,用 a = h * 180.0/pi 算角度,用 h = a * pi /180 算弧度.
创新互联公司于2013年开始,先为丰宁等服务建站,丰宁等地企业,进行企业商务咨询服务。为丰宁企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
从键盘输入一个角度值,求出该角度的正弦值、余弦值和正切值。
#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);
}
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
# include stdio.h
# include math.h
# define pi 3.1415926
int main(void)
{
double a,n,result;
char func_name[3];
printf("enter the name of triangle function :");
scanf("%s",func_name);
printf("enter angel:");
scanf("%lf",a);
n=pi*a/180;
if (strcmp(func_name, "sin") == 0)
result=sin(n);
if (strcmp(func_name, "cos") == 0)
result=cos(n);
if (strcmp(func_name, "tan") == 0)
result=tan(n);
printf("%0.2f\n",result);
return 0
;
}
调用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;
}
#include stdio.h
#include math.h
#define PI 3.14159265
int main()
{
double ans = sqrt((1-cos(PI/3.0))/2.0);
printf ("%g\n", ans);
return 0;
}