《Perl 语言入门习题答案》 2
1 2 练习 1 、写一个程序,计算半径为12
5的圆的周长
圆周长等于2π(π约为3
1415926)乘以半径
-----------------------/home/confish/perl/girth #
/usr/bin/perl -w #this program calculate a circle's girth #****************** $r=12
5; $g=12
1415; print "the girth of the circle is $g\n"; -----------------------/home/confish/perl/girth 2 、修改上述程序,用户可以在程序运行时输入半径
如果,用户输入12
5,则应得到和上题一样的结果
-----------------------/home/confish/perl/girthpro #
/usr/bin/perl -w #a better one to calculate girth #****************** print"enter the radius of the circle\n"; chomp($r=); if($r>0) { print"the girth of the circle is "
$r*2*3
"\n"; } else { print"nonavailable
\n"; } -----------------------/home/confish/perl/girthpro 3 、修改上述程序,当用户输入小于0 的数字时,程序输出的周长为0,而非负数
-----------------------/home/confish/perl/girthze