Perl语言入门第四版习题答案Word文档下载推荐.docx
- 文档编号:16232774
- 上传时间:2022-11-21
- 格式:DOCX
- 页数:31
- 大小:27KB
Perl语言入门第四版习题答案Word文档下载推荐.docx
《Perl语言入门第四版习题答案Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《Perl语言入门第四版习题答案Word文档下载推荐.docx(31页珍藏版)》请在冰豆网上搜索。
{
print"
thegirthofthecircleis"
.$r*2*3.1415."
\n"
}
else
nonavailable!
3、修改上述程序,当用户输入小于0的数字时,程序输出的周长为0,而非负数。
-----------------------/home/confish/perl/girthzero
#calculatethegirthandprint0whentheradiusislowerthan0
entertheradiusoftheline\n"
print"
thegirthofthecircleis$r*2*3.1415\n"
thegirthofthecircleis0\n"
1、2、3:
(一起实现的)
$pai=3.141592654;
PleaseInputRadius:
"
$r=<
if($rlt0){
print"
Thecircumferenceis0\n"
}else{
$l=$r*2*$pai;
printf"
Thecircumferenceis%.1f\n"
$l;
}
4、写一个程序,用户能输入2个数字(不在同一行)。
输出为这两个数的积。
-----------------------/home/confish/perl/product
#printthetwonumber'
sproduct
enterthetwonumbers:
chomp($m=<
chomp($n=<
theproductofthetwonumbersare"
.$m*$n."
5、写一个程序,用户能输入1个字符串和一个数字(n)(不在同一行)。
输出为,n行这个字符串,1次1行(提示,使用“x”操作符)。
例如,如果用户输入的是“fred”和“3”,则输出为:
3行,每一行均为fred。
如果输入为“fred”和“299792”,则输出为299792行,每一行均为fred
-----------------------/home/confish/perl/printer
#printastringcertaintimesdependontheusr'
sinput
enterastringandanumber:
$str=<
chomp($num=<
print${str}x$num;
3.9练习
1、写一个程序,将一些字符串(不同的行)读入一个列表中,逆向输出它。
如果是从键盘输入的,那在Unix系统中应当使用CTRL+D表明end-of-file,在Windows系统中使用CTRL+Z.
------------------------------------/home/confish/reprint
#readsomeinputandprinttheminreversesequence
enterthestringplease:
@str=reverse<
\nthereversestringsare:
\n@str"
2、写一个程序,读入一串数字(一个数字一行),将和这些数字对应的人名(下面列出的)输出来。
(将下面的人名列表写入代码中)。
fredbettybarneydinoWilmapebblesbamm-bamm
例如,当输入为1,2,4和2,则输出的为fred,betty,dino,和betty
------------------------------------/home/confish/num_to_name
#readsomenumbersandoutputthematchname
#confish@ubuntu7.10
$i=0;
@names=qw/fredbettybarneydinoWilmapebblesbamm-bamm/;
enterthenumbersplease:
chomp(@nums=<
foreach(@nums)
{
@re=@names;
while($ine$_)
$n=shift(@re);
$i++;
$i=0;
print$n,"
3、写一个程序,将一些字符串(在不同的行中)读入一个列表中。
然后按ASCII顺序将它们输出来。
也就是说,当输入的字符串为fred,barney,wilma,betty,则输出为barneybettyfredwilma。
分别在一行或不同的行将之输出。
------------------------------------/home/confish/sort_str
#readsomestringsandsorttheminASCII
chomp(@str=sort<
#@str=sort<
willprintthemindiffrentlines
print@str,"
4.11练习
1、写一个名为&
total的子程序,返回一列数字的和。
提示:
子程序不应当有任何的I/O操作;
它处理调用的参数,返回处理后的值给调用者。
结合下面的程序来练习,它检测此子程序是否正常工作。
第一组数组之和25。
my@fred=qw{13579};
my$fred_total=&
total(@fred);
Thetotalof\@fredis$fred_total.\n"
Entersomenumbersonseparatelines:
"
my$user_total=&
total(<
Thetotalofthosenumbersis$user_total.\n"
--------------------------------/home/confish/perl/subr
#asubroutinenamedtotalreturnssumofnumbers
subtotal
foreach$n(0..$#_)
$sum+=$_[$n];
$sum;
my@fred=qw{13579};
my$fred_total=&
my$user_total=&
2、利用上题的子程序,写一个程序计算从1到1000的数字的和。
--------------------------------/home/confish/perl/suber
#usethesubroutineinlastprogramtogetthesumof1..1000
@num=(1..1000);
$sum=&
total(@num);
Thesumof1..1000is$sum\n"
3、额外的练习:
写一个子程序,名为&
above_average,将一列数字作为其参数,返回所有大于平均值的数字(提示:
另外写一个子程序来计算平均值,总和除以数字的个数)。
利用下面的程序进行测试:
my@fred=&
above_average(1..10);
\@fredis@fred\n"
(Shouldbe678910)\n"
my@barney=&
above_average(100,1..10);
\@barneyis@barney\n"
(Shouldbejust100)\n"
--------------------------------/home/confish/perl/aver
#toprintthenumberwhichislargerthantheaverage
#insomenumbers
subaverage
$average=$sum/($#_+1);
subabove_average
@num=@_;
@aba=();
$av=&
average(@num);
if($_[$n]>
$av)
push(@aba,$_[$n]);
}
@aba;
my@fred=&
(Shuoldbe678910)\n"
my@barney=&
above_average(100,1..10);
5.11练习
1、写一个程序,类似于cat,但保持输出的顺序关系。
(某些系统的名字可能是tac。
)如果运行此程序:
./tacfredbarneybetty,输出将是文件betty的内容,从最后一行到第一行,然后是barney,最后是fred,同样是从最后一行到第一行。
(注意使用./确保调用的是你自己的程序,而非系统提供的)
----------------------------------/home/confish/perl/tac
#aprogsameascatbutreversethestring
@ARGV=reverse@ARGV;
@a=reverse<
>
print@a;
2、写一个程序,要求用户在不同的行中输入一些字符串,将此字符串打印出来,规则是:
每一条占20个字符宽度,右对齐。
为了确保正确的输出,在开头打印出一串数字作为比较(帮助调试)。
注意,不要犯19个字符宽度的错误。
例如,如果输入,hello,good-bye,则输出为:
123456789012345678901234567890123456789012345678901234567890
hello
good-bye
----------------------------------/home/confish/perl/20str
#aprogthatprintthestringsas20wordsflushright
@str=<
while($i!
=5)
{
foreach(0..9)
print;
$i++;
foreach(@str)
%21s"
$_;
3、修改上一个程序,允许用户选择宽度,如,用户输入30,hello,good-bye(在不同的行中),则每一行的宽度为30。
(提示:
参阅第二章相应部分)。
提示,如果选择的宽度太长,可以增加比较行的长度。
----------------------------------/home/confish/perl/20strpro
#aprogprintthestringsasnumberusrapiontedwordsflushright
$num=shift@str;
chomp$num;
$conv="
%"
.++$num."
s"
printf$conv,$_;
6.5练习
1、写一个程序,提示用户输入givenname(名),并给出其对应的familyname(姓)。
使用你知道的人名,或者表6-1(如果你在计算机上花了太多时间,以致什么人都不认识):
表6-1样本数据
输入输出
fredflintstone
barneyrubble
wilmaflintstone
-------------------------------------/home/confish/perl/hash
#hashsthatprintthename'
sfamilyname
$family_name{"
fred"
}="
flintstone"
barney"
rubble"
wilma"
enterthegivennameplease:
chomp($gn=<
familynamefor$gnis$family_name{$gn}\n"
------------------------------------/home/confish/perl/hash
2、写一个程序,读入一串单词(一个单词一行)◆,输出每一个单词出现的次数。
如果某个作为数字使用值是undefined的,会自动将它转换为0。
)如果输入单词为fred,barney,dino,wilma,fred(在不同行中),则输出的fred将为
------------------------------------/home/confish/perl/counts
#hashsthatcountstheappearancetimesoftheword
#printsorted
enterthewordplease:
foreach(<
)
$counts{$_}++;
$counts{$_}.="
sort%counts;
print%counts;
------------------------------------/home/confish/perl/counts
7.4练习
1、写一个程序,输出所有提到fred的行(不要输出其它行)。
如果输入字符串Fred,fredrick,Alfred,能匹配上吗?
准备一个小的文本文件,其中包含如:
“fredlintsotne”以及类似的信息。
使用这个文本文件作为此程序的输入,以及本节下面练习的输入。
-------------------------------------------/home/confish/perl/pfred
#progprintthelinewhichcontains"
if(/fred/)
print$_;
2、修改上面的程序,允许匹配Fred。
现在它能匹配,Fred,fredrick,Alfred吗?
(将这些名字加入输入文件中)
-------------------------------------------/home/confish/perl/pffred
or"
Fred"
if(/fred|Fred/)
3、写一个程序,输出出现句号(.)的行,忽略其它行。
使用前面练习中的文件进行练习:
它能找到Mr.Slate吗?
-------------------------------------------/home/confish/perl/pp
#progprintthelinewhichcontainsapoint
if(/\./)
4、写一个程序,输出有一个字母大写,而非所有字母都大写的行。
它能匹配Fred,而不匹配fred和FRED吗?
-------------------------------------------/home/confish/perl/plg
#printthelinewhichcontainsnotcapitalsonly
#
confish@ubuntu7.10
if(/[a-z][A-Z]|[A-Z]+[a-z]/
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Perl 语言 入门 第四 习题 答案