LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1984|回复: 3

perl程序转换角度为弧度

[复制链接]
发表于 2003-9-15 13:23:19 | 显示全部楼层 |阅读模式
#!/usr/bin/perl
print "please input a degrees:";
$degrees = <STDIN>;
chop ($degrees);
print "$degrees \n";
#转化为弧度
$radians = zp($degrees);
print "radians is $radians \n";
print "\n degrees $degrees turn to radians is $radians \n";
$sin = sin($radians);
$cos = cos($radians);
print "\n sin($degrees) = $sin \n";
print "\n cos($degrees) = $cos \n";
sub zp
{
local ($degrees) = @_;
        local ($radians);
        $radians = atan2(1,1) * $degrees / 45;
        print "local degrees is @_ \n";
        print "radians is $radians \n";
}

这个是转换角度到弧度的,看了几天都不明白,好象不能从子程序传递变量到主程序中,还有子程序中这几条真看不明白
local ($degrees) = @_;
$radians = atan2(1,1) * $degrees / 45;
上面那个@_是干嘛的,不懂,
下面一条更不明白了,

多谢指教
发表于 2003-9-15 15:36:52 | 显示全部楼层
1,@_ 是一个子例程参数,是perl缺省的一个变量
2,这一句大概就是将等号右侧的结果赋值给标量$radians
呵呵~~,我刚刚开始学perl,见过这个例子,不过还是请真正懂得perl的兄弟来解惑吧~~:sorry
发表于 2003-9-15 18:45:19 | 显示全部楼层
local ($degrees) = @_;
把所有参数赋给degrees变量
$radians = atan2(1,1) * $degrees / 45;
atan2(1,1)
atan2 Y,X
               Returns the arctangent of Y/X in the range -PI to PI.
所以这里返回PI/4
其他没什么了吧,弧度=角度*PI/180
 楼主| 发表于 2003-9-15 21:03:24 | 显示全部楼层
192# perl sin.pl
please input a degrees:20
20
local degrees is 20
radians is 0.349065850398866
radians is 1

degrees 20 turn to radians is 1

sin(20) = 0.841470984807897

cos(20) = 0.54030230586814
192# perl sin.pl
please input a degrees:27
27
local degrees is 27
radians is 0.471238898038469
radians is 1

degrees 27 turn to radians is 1

sin(27) = 0.841470984807897

cos(27) = 0.54030230586814
192#

明白了,多谢两位,

不过,不知道两位试过没,好象这个程序有问题,每次算出的sin cos值都相等,不能传递radians到主程序中,怎么办,多谢了,
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表