Perl语言入门(第四版)习题答案_第1页
Perl语言入门(第四版)习题答案_第2页
Perl语言入门(第四版)习题答案_第3页
Perl语言入门(第四版)习题答案_第4页
Perl语言入门(第四版)习题答案_第5页
已阅读5页,还剩18页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、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#confishubuntu7.10$r=12.5;$g=12.5*2*3.1415;print "the girth of the circle is $gn"-/home/confish/perl/girth 2、修改上述程序,用户可以在程序运行时输入半

2、径。如果,用户输入12.5,则应得到和上题一样的结果。 -/home/confish/perl/girthpro#!/usr/bin/perl -w#a better one to calculate girth#confishubuntu7.10print"enter the radius of the circlen"chomp($r=<STDIN>);if($r>0) print"the girth of the circle is ".$r*2*3.1415."n" else print"nonav

3、ailable!n" -/home/confish/perl/girthpro3、修改上述程序,当用户输入小于0 的数字时,程序输出的周长为0,而非负数。-/home/confish/perl/girthzero#!/usr/bin/perl -w#calculate the girth and print 0 when the radius is lower than 0#confishubuntu7.10print"enter the radius of the linen"chomp($r=<STDIN>);if($r>0) print&q

4、uot;the girth of the circle is $r*2*3.1415n" else print"the girth of the circle is 0n" -/home/confish/perl/girthzero1、2、3:(一起实现的)#!/usr/bin/perl -w$pai=3.141592654;print "Please Input Radius:"$r=<STDIN>if ( $r lt 0 ) print "The circumference is 0n" else $l=$r

5、*2*$pai; printf "The circumference is %.1fn",$l;4、写一个程序,用户能输入2 个数字(不在同一行)。输出为这两个数的积。 -/home/confish/perl/product#!/usr/bin/perl -w#print the two number's product#confishubuntu7.10print"enter the two numbers:n"chomp($m=<STDIN>);chomp($n=<STDIN>);print"the prod

6、uct of the two numbers are ".$m*$n."n"-/home/confish/perl/product5、写一个程序,用户能输入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

7、 times depend on the usr's input#confishubuntu7.10print"enter a string and a number:n"$str=<STDIN>chomp($num=<STDIN>);print $strx$num;-/home/confish/perl/printer3.9 练习1、写一个程序,将一些字符串(不同的行)读入一个列表中,逆向输出它。如果是从键盘输入的,那在Unix 系统中应当使用CTRL+D 表明end-of-file,在Windows 系统中使用CTRL+Z. -/home

8、/confish/reprint#!/usr/bin/perl -w#read some input and print them in reverse sequence#confishubuntu7.10print "enter the string please:n"str=reverse <STDIN>print "nthe reverse strings are:nstr"-/home/confish/reprint2、写一个程序,读入一串数字(一个数字一行),将和这些数字对应的人名(下面列出的)输出来。(将下面的人名列表写入代码中)

9、。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#confishubuntu7.10 $i=0;names=qw /fred betty barney dino Wilma pebbles bamm-bamm/;print"enter the numbers please:

10、n"chomp(nums=<STDIN>);foreach(nums) re=names; while($i ne $_) $n=shift( re); $i+; $i=0; print $n,"n"-/home/confish/num_to_name3、写一个程序,将一些字符串(在不同的行中)读入一个列表中。然后按ASCII 顺序将它们输出来。也就是说,当输入的字符串为fred, barney, wilma, betty,则输出为barney betty fred wilma。分别在一行或不同的行将之输出。-/home/confish/sort_st

11、r#!/usr/bin/perl -w#read some strings and sort them in ASCII#confishubuntu7.10chomp(str=sort<STDIN>);#str=sort<STDIN> will print them in diffrent linesprint str,"n"-/home/confish/sort_str4.11练习1、写一个名为&total 的子程序,返回一列数字的和。提示:子程序不应当有任何的I/O 操作;它处理调用的参数,返回处理后的值给调用者。结合下面的程序来练习,它

12、检测此子程序是否正常工作。第一组数组之和25。my fred = qw 1 3 5 7 9 ;my $fred_total = &total(fred);print "The total of fred is $fred_total.n"print "Enter some numbers on separate lines: "my $user_total = &total(<STDIN>);print "The total of those numbers is $user_total.n" -/home

13、/confish/perl/subr#!/usr/bin/perl -w#a subroutine named total returns sum of numbers#confishubuntu7.10sub total foreach $n(0.$#_) $sum+=$_$n; $sum; myfred=qw1 3 5 7 9;my $fred_total=&total(fred);print"The total of fred is $fred_total.n"print"Enter some numbers on separate lines:n&

14、quot;my $user_total=&total(<STDIN>);print"The total of those numbers is $user_total.n"-/home/confish/perl/subr2、利用上题的子程序,写一个程序计算从1 到1000 的数字的和。-/home/confish/perl/suber#!/usr/bin/perl -w#use the subroutine in last program to get the sum of 1.1000#confishubuntu7.10sub total foreac

15、h $n(0.$#_) $sum+=$_$n; $sum; num=(1.1000);$sum=&total(num);print"The sum of 1.1000 is $sumn"-/home/confish/perl/suber3、额外的练习:写一个子程序,名为&above_average,将一列数字作为其参数,返回所有大于平均值的数字(提示:另外写一个子程序来计算平均值,总和除以数字的个数)。利用下面的程序进行测试:my fred = &above_average(1.10);print "fred is fredn"p

16、rint "(Should be 6 7 8 9 10)n"my barney = &above_average(100, 1.10);print "barney is barneyn"print "(Should be just 100)n" -/home/confish/perl/aver#!/usr/bin/perl -w#to print the number which is larger than the average#in some numbers#confishubuntu7.10sub average fo

17、reach $n(0.$#_) $sum+=$_$n; $average=$sum/($#_+1); sub above_average num=_; aba=(); $av=&average(num); foreach $n(0.$#_) if($_$n>$av) push ( aba,$_$n); aba; my fred=&above_average(1.10);print"fred is fredn"print"(Shuold be 6 7 8 9 10)n"my barney=&above_average(100,

18、1.10);print"barney is barneyn"print"(Should be just 100)n"-/home/confish/perl/aver5.11 练习1、写一个程序,类似于 cat,但保持输出的顺序关系。(某些系统的名字可能是 tac 。) 如果运行此程序:./tac fred barney betty, 输出将是文件 betty 的内容,从最后一行到第一行,然后是 barney, 最后是 fred, 同样是从最后一行到第一行。(注意使用 ./ 确保调用的是你自己的程序,而非系统提供的)-/home/confish/perl/

19、tac#!/usr/bin/perl -w#a prog same as cat but reverse the string#confishubuntu7.10ARGV=reverse ARGV;a=reverse<>print a;-/home/confish/perl/tac2、写一个程序,要求用户在不同的行中输入一些字符串,将此字符串打印出来,规则是:每一条占20 个字符宽度,右对齐。为了确保正确的输出,在开头打印出一串数字作为比较(帮助调试)。注意,不要犯19 个字符宽度的错误。例如,如果输入,hello, good-bye,则输出为:123456789012345678

20、901234567890123456789012345678901234567890 hello good-bye-/home/confish/perl/20str#!/usr/bin/perl -w#a prog that print the strings as 20 words flush right#confishubuntu7.10str=<STDIN>while($i!=5) foreach(0.9) print; $i+; print"n"foreach(str) printf "%21s",$_; -/home/confish

21、/perl/20str3、修改上一个程序,允许用户选择宽度,如,用户输入30,hello, good-bye(在不同的行中),则每一行的宽度为30。(提示:参阅第二章相应部分)。提示,如果选择的宽度太长,可以增加比较行的长度。 -/home/confish/perl/20strpro#!/usr/bin/perl -w#a prog print the strings as number usr apionted words flush right#confishubuntu7.10str=<STDIN>while($i!=5) foreach(0.9) print; $i+; p

22、rint "n"$num=shift str;chomp $num;$conv="%".+$num."s"foreach(str) printf $conv,$_; -/home/confish/perl/20strpro6.5 练习1、写一个程序,提示用户输入 given name(名),并给出其对应的 family name(姓)。 使用你知道的人名,或者表 6- 1(如果你在计算机上花了太多时间,以致什么人都不认识):表 6 - 1 样本数据输入 输出fred flintstonebarney rubblewilma flint

23、stone-/home/confish/perl/hash#!/usr/bin/perl -w#hashs that print the name's family name#confishubuntu7.10$family_name"fred"="flintstone"$family_name"barney"="rubble"$family_name"wilma"="flintstone"print "enter the given name please

24、:n"chomp($gn=<STDIN>);print "family name for $gn is $family_name$gnn"-/home/confish/perl/hash2、写一个程序,读入一串单词( 一个单词一行) ,输出每一个单词出现的次数。(提示:如果某个作为数字使用值是undefined 的,会自动将它转换为 0。)如果输入单词为 fred, barney, dino, wilma , fred(在不同行中), 则输出的 fred 将为-/home/confish/perl/counts#!/usr/bin/perl -w#ha

25、shs that counts the appearance times of the word#print sorted#confishubuntu7.10print "enter the word please:n"foreach(<>) $counts$_+; $counts$_.="n" sort %counts;print %counts;-/home/confish/perl/counts 7.4 练习1、写一个程序,输出所有提到 fred 的行(不要输出其它行)。如果输入字符串 Fred, f redrick, Alfred,能

26、匹配上吗?准备一个小的文本文件,其中包含如:“ fred lintsotne” 以及类似的信息。使用这个文本文件作为此程序的输入,以及本节下面练习的输入。-/home/confish/perl/pfred#!/usr/bin/perl -w#prog print the line which contains "fred"#confishubuntu7.10foreach(<>) if(/fred/) print $_; -/home/confish/perl/pfred2、修改上面的程序,允许匹配 Fred。现在它能匹配,Fred, f redrick , A

27、lfred 吗? (将这些名字加入输入文件中)-/home/confish/perl/pffred#!/usr/bin/perl -w#prog print the line which contains "fred" or "Fred"#confishubuntu7.10foreach(<>) if(/fred|Fred/) print $_; -/home/confish/perl/pffred3、写一个程序,输出出现句号( . ) 的行,忽略其它行。使用前面练习中的文件进行练习:它能找到 Mr. Slate 吗?-/home/confi

28、sh/perl/pp#!/usr/bin/perl -w#prog print the line which contains a point#confishubuntu7.10foreach(<>) if(/./) print $_; -/home/confish/perl/pp4、写一个程序,输出有一个字母大写,而非所有字母都大写的行。它能匹配 Fred ,而不匹配 fred 和 FRED 吗?-/home/confish/perl/plg#!/usr/bin/perl -w#print the line which contains not capitals only#con

29、fishubuntu7.10foreach(<>) if(/a-zA-Z|A-Z+a-z/) print $_; -/home/confish/perl/plg5、额外的练习:写一个程序,它能输出所有同时提到 wilma 和 fred 的行。-/home/confish/perl/pfw#!/usr/bin/perl -w#print a line which contains fred and wilma#confishubuntu7.10foreach(<>) if(/wilma.+fred|fred.+wilma/) print $_; -/home/confis

30、h/perl/pfw8.10 练习1、使用模式测试程序。 创造一个模式能匹配字符串 match。 使用字符串 beforematchafter 进行测试。 输出结果将其三部分放在正确位置了吗?-/home/confish/perl/mm#!/usr/bin/perl -w#match "match"#confishubuntu7.10while(<>) chomp; if(/match/) print "Matched:|$<$&>$'|n" else print "no match:|$_|n"

31、; -/home/confish/perl/mm2、使用模式测试程序, 创造一个模式能匹配任何单词 ( w 意义下的单词), 但这个单词必需以字母 a 结尾。 它匹配 wilma而没匹配 barney 吗?它匹配 Mrs. Wilma Flintstone 吗? wilma&fred 呢?使用前一章习题的文件进行练习(如果没有上述字符串,则加上它们)-/home/confish/perl/ma#!/usr/bin/perl -w#match "word" ends with a#confishubuntu7.10while(<>) chomp; if(/

32、ab/) print "Matched:|$<$&>$'|n" else print "no match:|$_|n" -/home/confish/perl/ma3、修改第二题的程序,使之将由 a 结尾的单词放到$1 之中。同时修改源代码,使此变量对应的值被放在单引号之中,如$1 contains Wilma 。-/home/confish/perl/mapro#!/usr/bin/perl -w #match word end with a and storage it#confishubuntu7.10while(<

33、;>) chomp; if(/(a$)/) my $temp=$1; print "$1 contains '$&$''n" else print "no match:|$_|n" -/home/confish/perl/mapro4、额外练习:修改第三题程序,使之能捕捉由 a 结尾的单词之后的 5 个字符(如果有那么多),并将之放入一个独立变量中。例如,如果输入的是 I saw Wilma yester day, 则紧接的 5 个字符是 yest(前有空格)。如果输入是 I , Wilma! ,则只有一个字符。它现

34、在还能匹配 wilma 吗?-/home/confish/perl/mwa#!/usr/bin/perl -w#match a word end with a and print the next five character#confishubuntu7.10while(<>) if(/ab/) my $temp=$' if($temp=/.0,5/) my $match=$& print $match; else print "no match:|$_|n" -/home/confish/perl/mwa5、写一个程序(不是测试程序), 能输

35、出任何由空白结尾的输入行(非换行符)。 在输出的结尾处放置一个标记符,使之能标记出空白。-/home/confish/perl/ms#!/usr/bin/perl -w#match a space#confishubuntu7.10while(<>) if(/ +$/) print; -/home/confish/perl/ms9.6 练习1、写一个模式, 它能匹配$what 当前的内容的 3 份连续拷贝。也就是说, 如果$what 为 fred, 则此模式能匹配 fredfredfred 。如果$what 为 fred|barney ,则此模式能匹配 fredfredbarney

36、, barneyfredfred, barneybarneybarney , 或者其它的变种。(提示:你应当在程序的顶端设置$what 的值,如 my $what = fred|barney ;)-/home/confish/perl/sfb#!/usr/bin/perl -w #match and replace fred three times#confishubuntu7.10my $what="fred|barney"if("fredbarneybarney"=/($what)3/)print $."/".$&.&qu

37、ot;/".$'."n"-/home/confish/perl/sfb2、写一个程序, 它可以得到当前文本文件的一个拷贝。 在拷贝的文件中, 字符串 Fred( 大小写无关) 将被 Larry 替换掉。(因此,“ Manfred Mann” 将变成 “ ManLarry Mann” . )输入的文件名已经在命令行中指定(不需要询问用户),输出的文件名是对应的输入文件名后面加上.out。-/home/confish/perl/sfl#!/usr/bin/perl -w #match fred to Larry#confishubuntu7.10$I=&quo

38、t;.out"while(<>) s/fred/Larry/i; print; -/home/confish/perl/sfl3、修改上面程序, 使之将 Fred 由 Wilma 替换, Wilma 由 Fred 替换。 如果输入的为 fred&wilma, 则输出为 Wilma$Fred。-/home/confish/perl/addc#!/usr/bin/perl -w#add copyright#confishubuntu7.10$I=".out"$a="/usr/bin/perl -w n #Copyright(C) 2008

39、 by Yours Truly confish"while(<>) s#/usr/bin/perls+-w#$a#i; print; -/home/confish/perl/addc4、额外练习:写一个程序在你所有的练习的答案前加上下面这样一行:a) # Copyright (C) 20XX by Yours Truly将上面一行放在“ shebang” 行(Perl 程序的第一行,#!/usr/bin/perl (可能随 Perl 安装的位置而有所不同, 但是指第一行,译者注) )下面。你应当在“源文件”中修改,但请备份文件。假定你可以在命令行中同时输入程序和需要的修改

40、的文件名。10.10 练习1、写一个程序,能重复要求用户猜测某个在 1 到 100 之间的数字,直到猜对为止。你的程序应当能随机的产生一个数字,使用公式 int(1 + rand 100)。当用户猜测错误时,程序应该回应“ Too high” 或者 “ Too low ” 。如果用户输入 quit或 exit ,或者回车时,程序应立即退出。如果用户猜测正确,程序也退出。-/home/confish/perl/gn#!/usr/bin/perl -w#a game to guess a number#confishubuntu7.10my $y=int(1+rand(100);print&quo

41、t;enter a number please:n"while(chomp($t=<STDIN>) if($t ne "exit"&&$t ne"quit" ) if($t>$y) print"tow hign" elsif($t<$y) print"two lown" else print "an"last; else last; -/home/confish/perl/gn11.6 练习1、写一个程序,读入命令行中的一串文件,报告其是否可读,

42、可写,可执行,或不存在。(提示:如果一个函数能一次对一个文件进行所有的检测将非常有帮助。) 如果一个文件被执行了 chmod 0 操作, 将报告什么?(在 Unix 系统中,chmod 0 some_file 将一个文件变成不可读,不可写,不可执行的)在大多数 shell 中,星号(*)表示当前目录中的所有的普通文件。也就是说,可以输入像 ./ex11 - 1 *这样的命令,返回当前目录下文件的属性。-/home/confish/perl/crw#!/usr/bin/perl -w#to check whether a file is exits have readability and wr

43、iteable#confishubuntu7.10 foreach(ARGV) if(-e) print "$_ exitsn" if(-r) print "$_ readablen" else print"$_ does not be readablen" if(-w) print "$_ writeablen" else print "$_ does not be writeablen" else print "$_ does not exitsn" -/home/con

44、fish/perl/crw2、写一个程序,找出命令行中存在时间最长的文件名,并报告其天数。当参数为空时,其行为如何(例如,命令行中没有输入任何的文件)?-/home/confish/perl/et#!/usr/bin/perl -w#to check the longest exits file#confishubuntu7.10if(ARGV) foreach(ARGV) if(-M>$such) $such=-M; $file=$_; print"$file has exits $such daysn" else print "no input file

45、sn" -/home/confish/perl/et12.14 练习1、写一个程序要求用户输入一个目录名,再改变到那个目录去。如果用户输入的值是空白,则转变到他/她的主目录去。改变后,将这个目录下的普通内容(不包括有点(. )开头的项)按照字母顺序列出来。(提示:使用目录句柄还是 glob 更方便?)如果没有成功改变目录,提示用户,但不要尝试输出目录里的内容。#! /usr/bin/env perl -wuse strict;#Date:2005-05-31#ex12_1if (ARGV) chdir $ARGV0;else chdir ""_=glob '*'print _;2、修改程序,使之包含所有的文件,不仅仅是那些不以点( . ) 开头的文件。#! /usr/bin/env pe

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论