77范文网 - 专业文章范例文档资料分享平台

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

来源:网络收集 时间:2019-01-07 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

《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 \

-----------------------/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\chomp($r=); if($r>0) {

print\ } else {

print\}

-----------------------/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\chomp($r=);

if($r>0) {

print\ } else {

print\ }

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

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

#!/usr/bin/perl -w $pai=3.141592654;

print \$r=; if ( $r lt 0 ){

print \ }else{

$l=$r*2*$pai;

printf \}

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

-----------------------/home/confish/perl/product #!/usr/bin/perl -w

#print the two number's product #confish@ubuntu7.10

print\chomp($m=); chomp($n=);

print\-----------------------/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\$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 \@str=reverse ;

print \

------------------------------------/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\chomp(@nums=); foreach(@nums) {

@re=@names; while($i ne $_) {

$n=shift( @re); $i++; }

$i=0; print $n,\}

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

3、写一个程序,将一些字符串(在不同的行中)读入一个列表中。然后按ASCII 顺序将它

们输出来。也就是说,当输入的字符串为fred, barney, wilma, betty,则输出为barney betty fred wilma。分别在一行或不同的行将之输出。

------------------------------------/home/confish/sort_str #!/usr/bin/perl -w

#read some strings and sort them in ASCII #confish@ubuntu7.10

chomp(@str=sort);

#@str=sort; will print them in diffrent lines print @str,\

------------------------------------/home/confish/sort_str

4.11练习

1、写一个名为&total 的子程序,返回一列数字的和。

提示:子程序不应当有任何的I/O 操作;它处理调用的参数,返回处理后的值给调用者。结合下面的程序来练习,它检测此子程序是否正常工作。第一组数组之和25。 my @fred = qw{ 1 3 5 7 9 }; my $fred_total = &total(@fred);

print \print \my $user_total = &total();

print \ --------------------------------/home/confish/perl/subr #!/usr/bin/perl -w

#a subroutine named total returns sum of numbers #confish@ubuntu7.10 sub total {

foreach $n(0..$#_) {

$sum+=$_[$n]; }

$sum; }

my@fred=qw{1 3 5 7 9};

my $fred_total=&total(@fred);

print\print\my $user_total=&total();

print\--------------------------------/home/confish/perl/subr

2、利用上题的子程序,写一个程序计算从1 到1000 的数字的和。

--------------------------------/home/confish/perl/suber #!/usr/bin/perl -w

#use the subroutine in last program to get the sum of 1..1000 #confish@ubuntu7.10 sub total {

foreach $n(0..$#_) {

$sum+=$_[$n]; } $sum; }

@num=(1..1000); $sum=&total(@num);

print\

--------------------------------/home/confish/perl/suber

3、额外的练习:写一个子程序,名为&above_average,将一列数字作为其参数,返回所有

大于平均值的数字(提示:另外写一个子程序来计算平均值,总和除以数字的个数)。利用下面的程序进行测试:

my @fred = &above_average(1..10); print \

print \

my @barney = &above_average(100, 1..10); print \print \

--------------------------------/home/confish/perl/aver #!/usr/bin/perl -w

#to print the number which is larger than the average #in some numbers #confish@ubuntu7.10 sub average {

foreach $n(0..$#_) {

$sum+=$_[$n]; }

$average=$sum/($#_+1); }

sub above_average {

@num=@_;

@aba=();

$av=&average(@num); foreach $n(0..$#_)

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库Perl语言入门(第四版)习题答案在线全文阅读。

Perl语言入门(第四版)习题答案.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/407590.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: