Perl语言入门(第四版)习题标准答案

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Perl语言入门(第四版)习题答案

————————————————————————————————作者:————————————————————————————————日期:

《Perl语言入门习题答案》

2.12 练习

1、写一个程序,计算半径为12.5的圆的周长。圆周长等于2π(π约为3.1415926)乘以半径。答案为78.5。

-----------------------/home/confish/perl/girth

#!/usr/bin/perl -w

#this program calculate a circle's girth

#confish@ubuntu7.10

$r=12.5;

$g=12.5*2*3.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

#confish@ubuntu7.10

print"enter the radius of the circle\n";

chomp($r=);

if($r>0)

{

print"the girth of the circle is ".$r*2*3.1415."\n";

}

else

{

print"nonavailable!\n";

}

-----------------------/home/confish/perl/girthpro

3、修改上述程序,当用户输入小于0 的数字时,程序输出的周长为0,而非负数。

-----------------------/home/confish/perl/girthzero

#!/usr/bin/perl -w

#calculate the girth and print 0 when the radius is lower than 0

#confish@ubuntu7.10

print"enter the radius of the line\n";

chomp($r=);

if($r>0)

{

print"the girth of the circle is $r*2*3.1415\n";

}

else

{

print"the girth of the circle is 0\n";

}

-----------------------/home/confish/perl/girthzero

1、2、3:(一起实现的)

#!/usr/bin/perl -w

$pai=3.141592654;

print "Please Input Radius:";

$r=;

if ( $r lt 0 ){

print "The circumference is 0\n";

}else{

$l=$r*2*$pai;

printf "The circumference is %.1f\n",$l;

}

4、写一个程序,用户能输入2 个数字(不在同一行)。输出为这两个数的积。

-----------------------/home/confish/perl/product

#!/usr/bin/perl -w

#print the two number's product

#confish@ubuntu7.10

print"enter the two numbers:\n";

chomp($m=);

chomp($n=);

print"the product of the two numbers are ".$m*$n."\n";

-----------------------/home/confish/perl/product

5、写一个程序,用户能输入1 个字符串和一个数字(n)(不在同一行)。输出为,n 行这个字符串,1 次1 行(提示,使用“x”操作符)。例如,如果用户输入的是“fred”和“3”,则输出为:3 行,每一行均为fred。如果输入为“fred”和“299792”,则输出为299792 行,每一行均为fred

-----------------------/home/confish/perl/printer

#!/usr/bin/perl -w

#print a string certain times depend on the usr's input

#confish@ubuntu7.10

print"enter a string and a number:\n";

$str=;

chomp($num=);

print ${str}x$num;

-----------------------/home/confish/perl/printer

3.9 练习

1、写一个程序,将一些字符串(不同的行)读入一个列表中,逆向输出它。如果是从键盘输入的,那在Unix 系统中应当使用CTRL+D 表明end-of-file,在Windows 系统中使用CTRL+Z.

------------------------------------/home/confish/reprint

#!/usr/bin/perl -w

#read some input and print them in reverse sequence

#confish@ubuntu7.10

print "enter the string please:\n";

@str=reverse ;

print "\nthe reverse strings are:\n@str";

------------------------------------/home/confish/reprint

2、写一个程序,读入一串数字(一个数字一行),将和这些数字对应的人名(下面列出的)输出来。(将下面的人名列表写入代码中)。fred betty barney dino Wilma pebbles bamm-bamm 例如,当输入为1,2,4 和2,则输出的为fred, betty, dino, 和betty

------------------------------------/home/confish/num_to_name

#!/usr/bin/perl -w

#read some numbers and output the match name

#confish@ubuntu7.10

$i=0;

@names=qw /fred betty barney dino Wilma pebbles bamm-bamm/;

print"enter the numbers please:\n";

chomp(@nums=);

foreach(@nums)

{

@re=@names;

while($i ne $_)

{

$n=shift( @re);

$i++;

}

$i=0;

print $n,"\n";

}

------------------------------------/home/confish/num_to_name

3、写一个程序,将一些字符串(在不同的行中)读入一个列表中。然后按ASCII 顺序将它们输出来。也就是说,当输入的字符串为fred, barney, wilma, betty,则输出为barney betty fred wilma。分别在一行或不同的行将之输出。

相关文档
最新文档