C语言怎样表示三角函数计算(注:要用“角度制”表示)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言怎样表示三角函数计算(注:要用“角度制”表示)相关的知识,希望对你有一定的参考价值。
1.C语言的三角函数库采用的单位都是弧度,如果要使用角度,就必须转换,从角度转换成弧度,或者是重写一个三角函数库。
2.
方法一,在调用三角函数之前先把角度换算成弧度,调用反三角函数之后把弧度换算成角度就可以了。可以用
pi
=
4.0
*
atan(1)
算出pi,用
a
=
d
/180.0*pi
转换角度到弧度。
例如:
sin(45
/180.0*pi);
就是计算的sin45。
3.
方法二,直接覆写三角函数。
例如sin函数:
double
dsin(double
d)
return
sin(45
/180.0*pi);
//原理和方法一样,调用的时候直接使用dsin(45)即可
参考技术A 在调用三角函数之前先把角度换算成弧度,调用反三角函数之后把弧度换算成角度就可以了。可以用
pi
=
4.0
*
atan(1)
算出pi,用
a
=
h
*
180.0/pi
算角度,用
h
=
a
*
pi
/180
算弧度。 参考技术B c语言中的三角函数计算需要将角度转弧度,,比如以下代码是计算sin()的值:
#include"stdio.h"
#include"math.h"
#define
pi
3.1415926
main()
int
i;
float
t;
printf("请输入要计算的角度:");
scanf("%d",i);
t=sin(180*i/pi);
printf("sin(%d)=%f",i,t);
角度制下的锐角三角函数计算程序
Perl
①
use Win32::Console::ANSI;
print "\33[2J\33[H";
print "度:";
chomp($d=<STDIN>);
if($d<=0||$d>=90){print "输入错误:非锐角!\12";}
else {
print "分:";
chomp($_=<STDIN>);
$d+=$_/60;
if($d<=0||$d>=90){print "输入错误:非锐角!\12";}
else {
print "秒:";
chomp($_=<STDIN>);
$d+=$_/60/60;
if($d<=0||$d>=90){print "输入错误:非锐角!\12";}
else {
$r=.31415926535897932384626433832795/18*$d;
$x=cos($r);
$y=sin($r);
print "\33[2J\33[H";
print "\33[91msin (";
print $d;
print "°) = ";
print $y;
print "\12";
print "\33[92msec (";
print $d;
print "°) = ";
print(1/$x);
print "\12";
print "\33[93mcos (";
print $d;
print "°) = ";
print $x;
print "\12";
print "\33[94mcsc (";
print $d;
print "°) = ";
print(1/$y);
print "\12";
print "\33[95mtan (";
print $d;
print "°) = ";
print($y/$x);
print "\12";
print "\33[96mcot (";
print $d;
print "°) = ";
print($x/$y);
print "\12";
print "\33[0m";
}}}
②
use Win32::Console::ANSI;
use Math::Trig;
print "\33[2J\33[H",
"度:";
chomp($d=<STDIN>);
if($d<=0||$d>=90){print "输入错误:非锐角!\12";}
else {
print "分:";
chomp($_=<STDIN>);
$d+=$_/60;
if($d<=0||$d>=90){print "输入错误:非锐角!\12";}
else {
print "秒:";
chomp($_=<STDIN>);
$d+=$_/3600;
if($d<=0||$d>=90){print "输入错误:非锐角!\12";}
else {
$r=deg2rad($d);
print "\33[2J\33[H",
"\33[91msin (",
$d,
"°) = ",
sin($r),
"\12",
"\33[92msec (",
$d,
"°) = ",
sec($r),
"\12",
"\33[93mcos (",
$d,
"°) = ",
cos($r),
"\12",
"\33[94mcsc (",
$d,
"°) = ",
csc($r),
"\12",
"\33[95mtan (",
$d,
"°) = ",
tan($r),
"\12",
"\33[96mcot (",
$d,
"°) = ",
cot($r),
"\12",
"\33[0m";
}}}
VB.NET
①
Module Trigonometry
Dim d, r, x, y As Primitive
Sub Main()
TextWindow.Clear()
TextWindow.Write("度:")
d = TextWindow.ReadNumber()
If (d <= 0) Or (d >= 90) Then
TextWindow.WriteLine("输入错误:非锐角!")
Else
TextWindow.Write("分:")
d = d + (TextWindow.ReadNumber() / 60)
If (d <= 0) Or (d >= 90) Then
TextWindow.WriteLine("输入错误:非锐角!")
Else
TextWindow.Write("秒:")
d = d + (TextWindow.ReadNumber() / 60 / 60)
If (d <= 0) Or (d >= 90) Then
TextWindow.WriteLine("输入错误:非锐角!")
Else
r = .31415926535897932384626433832795 / 18 * d
x = Microsoft.SmallBasic.Library.Math.Cos(r)
y = Microsoft.SmallBasic.Library.Math.Sin(r)
TextWindow.Clear()
TextWindow.ForegroundColor = "Red"
TextWindow.WriteLine("sin (" + d + "°) = " + y)
TextWindow.ForegroundColor = "Green"
TextWindow.WriteLine("sec (" + d + "°) = " + (1 / x))
TextWindow.ForegroundColor = "Yellow"
TextWindow.WriteLine("cos (" + d + "°) = " + x)
TextWindow.ForegroundColor = "Blue"
TextWindow.WriteLine("csc (" + d + "°) = " + (1 / y))
TextWindow.ForegroundColor = "Magenta"
TextWindow.WriteLine("tan (" + d + "°) = " + (y / x))
TextWindow.ForegroundColor = "Cyan"
TextWindow.WriteLine("cot (" + d + "°) = " + (x / y))
TextWindow.ForegroundColor = "Gray"
End If
End If
End If
End Sub
End Module
②
Module Trigonometry
Dim d, r, x, y, z As Primitive
Sub Main()
TextWindow.Clear()
TextWindow.Write("度:")
d = TextWindow.ReadNumber()
If (d <= 0) Or (d >= 90) Then
TextWindow.WriteLine("输入错误:非锐角!")
Else
TextWindow.Write("分:")
d = d + (TextWindow.ReadNumber() / 60)
If (d <= 0) Or (d >= 90) Then
TextWindow.WriteLine("输入错误:非锐角!")
Else
TextWindow.Write("秒:")
d = d + (TextWindow.ReadNumber() / 3600)
If (d <= 0) Or (d >= 90) Then
TextWindow.WriteLine("输入错误:非锐角!")
Else
r = Microsoft.SmallBasic.Library.Math.GetRadians(d)
x = Microsoft.SmallBasic.Library.Math.Cos(r)
y = Microsoft.SmallBasic.Library.Math.Sin(r)
z = Microsoft.SmallBasic.Library.Math.Tan(r)
TextWindow.Clear()
TextWindow.ForegroundColor = "Red"
TextWindow.Write("sin (")
TextWindow.Write(d)
TextWindow.Write("°) = ")
TextWindow.WriteLine(y)
TextWindow.ForegroundColor = "Green"
TextWindow.Write("sec (")
TextWindow.Write(d)
TextWindow.Write("°) = ")
TextWindow.WriteLine(1 / x)
TextWindow.ForegroundColor = "Yellow"
TextWindow.Write("cos (")
TextWindow.Write(d)
TextWindow.Write("°) = ")
TextWindow.WriteLine(x)
TextWindow.ForegroundColor = "Blue"
TextWindow.Write("csc (")
TextWindow.Write(d)
TextWindow.Write("°) = ")
TextWindow.WriteLine(1 / y)
TextWindow.ForegroundColor = "Magenta"
TextWindow.Write("tan (")
TextWindow.Write(d)
TextWindow.Write("°) = ")
TextWindow.WriteLine(z)
TextWindow.ForegroundColor = "Cyan"
TextWindow.Write("cot (")
TextWindow.Write(d)
TextWindow.Write("°) = ")
TextWindow.WriteLine(1 / z)
TextWindow.ForegroundColor = "Gray"
End If
End If
End If
End Sub
End Module
Forse altri canterà con miglior plettro.
以上是关于C语言怎样表示三角函数计算(注:要用“角度制”表示)的主要内容,如果未能解决你的问题,请参考以下文章