Perl练习题文档格式.docx
《Perl练习题文档格式.docx》由会员分享,可在线阅读,更多相关《Perl练习题文档格式.docx(31页珍藏版)》请在冰豆网上搜索。
写一个程序,用户能输入1个字符串和一个数字(n)(不在同一行)。
输出为,n行这个字符串,1次1行(提示,使用“x”操作符)。
例如,如果用户输入的是“fred”和“3”,则输出为:
3行,每一行均为fred。
如果输入为“fred”和“299792”,则输出为299792行,每一行均为fred。
/usr.bin/perl
$string=<
$int=<
$output=$stringx$int
print$output;
3.9练习
写一个程序,将一些字符串(不同的行)读入一个列表中,逆向输出它。
如果是从键盘输入的,那在Unix系统中应当使用CTRL+D表明end-of-file,在Windows系统中使用CTRL+Z.
写一个程序,读入一串数字(一个数字一行),将和这些数字对应的人名(下面列出的)输出来。
(将下面的人名列表写入代码中)。
fredbettybarneydinoWilmapebblesbamm-bamm
例如,当输入为1,2,4和2,则输出的为fred,betty,dino,和betty
写一个程序,将一些字符串(在不同的行中)读入一个列表中。
然后按ASCII顺序将它们输出来。
也就是说,当输入的字符串为fred,barney,wilma,betty,则输出为barneybettyfredwilma。
分别在一行或不同的行将之输出。
1:
/usr/bin/perl-w
@michael=reverse(<
>
);
print"
@michael"
或:
@userinput=<
foreach(@userinput)
{
unshift(@array,$_);
arrayis@array\n"
2:
@name=qw(fredbettybarneydinoWilmapebblesbamm-bamm);
@number=<
foreach(@number)
$name[$_-1]\n"
3:
@array=<
@array=sort@array;
@array"
4.11练习
写一个名为&
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"
利用上题的子程序,写一个程序计算从1到1000的数字的和。
额外的练习:
写一个子程序,名为&
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"
#Date:
2009-6-12
#<
LeanningPerl4thEdition>
Exercise4-1
usestrict;
subtotal{
my$sum=shift@_;
foreach(@_){
$sum=$sum+$_;
}
$sum;
my@fred=qw{13579};
my$fred_total=&
TheTotalof\@fredis$fred_total.\n"
TheTotalofthosenumbersis$user_total.\n"
Exercise4-2
my@array=1..1000;
my$array_total=&
total(@array);
Thesumof1to1000is$array_total.\n"
Exercise4-3
subaverage{
my$number=@_;
my$array_average=$sum/$number;
subabove_average{
my@above;
if($_>
&
average(@_)){
push@above,$_;
@above;
my@above_fred=&
above_average(1..10);
Theaboveaverageof\@fredis@above_fred\n"
my@above_barney=&
above_average(100,1..10);
Theaboveaverageof\@barneyis@above_barney\n"
5.11练习
写一个程序,类似于cat,但保持输出的顺序关系。
(某些系统的名字可能是tac。
)如果运行此程序:
./tacfredbarneybetty,输出将是文件betty的内容,从最后一行到第一行,然后是barney,最后是fred,同样是从最后一行到第一行。
(注意使用./确保调用的是你自己的程序,而非系统提供的)
写一个程序,要求用户在不同的行中输入一些字符串,将此字符串打印出来,规则是:
每一条占20个字符宽度,右对齐。
为了确保正确的输出,在开头打印出一串数字作为比较(帮助调试)。
注意,不要犯19个字符宽度的错误。
例如,如果输入,hello,good-bye,则输出为:
123456789012345678901234567890123456789012345678901234567890
hello
good-bye
修改上一个程序,允许用户选择宽度,如,用户输入30,hello,good-bye(在不同的行中),则每一行的宽度为30。
(提示:
参阅第二章相应部分)。
提示,如果选择的宽度太长,可以增加比较行的长度。
2009-6-18
Exercise5-1
printreverse<
2-3:
Exercise5-2
Whatcolumnwidthwouldyoulike?
"
chomp(my$width=<
Entersomelines,thenpressCtrl+D:
\n"
chomp(my@line=<
1234567890"
x(($width+9)/10),"
foreach(@line){
printf"
%${width}s\n"
$_;
chapter2
1.
-----------------------/home/confish/perl/girth
#thisprogramcalculateacircle'
sgirth
#confish@ubuntu7.10
$g=12.5*2*3.1415;
thegirthofthecircleis$g\n"
2.
-----------------------/home/confish/perl/girthpro
#abetteronetocalculategirth
print"
entertheradiusofthecircle\n"
chomp($r=<
0)
{
print"
thegirthofthecircleis"
.$r*2*3.1415."
else
nonavailable!
3.
-----------------------/home/confish/perl/girthzero
#calculatethegirthandprint0whentheradiusislowerthan0
entertheradiusoftheline\n"
thegirthofthecircleis$r*2*3.1415\n"
thegirthofthecircleis0\n"
4.
-----------------------/home/confish/perl/product
#printthetwonumber'
sproduct
enterthetwonumbers:
chomp($m=<
chomp($n=<
theproductofthetwonumbersare"
.$m*$n."
5.
-----------------------/home/confish/perl/printer
#printastringcertaintimesdependontheusr'
sinput
enterastringandanumber:
$str=<
chomp($num=<
print${str}x$num;
chapter3
------------------------------------/home/confish/reprint
#readsomeinputandprinttheminreversesequence
enterthestringplease:
@str=reverse<
\nthereversestringsare:
\n@str"
------------------------------------/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,"
------------------------------------/home/confish/sort_str
#readsomestringsandsorttheminASCII
chomp(@str=sort<
#@str=sort<
willprintthemindiffrentlines
print@str,"
chapter4
--------------------------------/home/confish/perl/subr
#asubroutinenamedtotalreturnssumofnumbers
subtotal
foreach$n(0..$#_)
$sum+=$_[$n];
my@fred=qw{13579};
my$user_total=&
--------------------------------/home/confish/perl/suber
#usethesubroutineinlastprogramtogetthesumof1..1000
@num=(1..1000);
$sum=&
total(@num);
Thesumof1..1000is$sum\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);
chapter5
----------------------------------/home/confish/perl/tac
#aprogsameascatbutreversethestring
@ARGV=reverse@ARGV;
@a=reverse<
print@a;
----------------------------------/home/confish/perl/20str
#aprogthatprintthestringsas20wordsflushright
@str=<
while($i!
=5)
{
foreach(0..9)
print;
foreach(@str)
printf"
%21s"
$_;
----------------------------------/home/confish/perl/20strpro
#aprogprintthestringsasnumberusrapiontedwordsflushright
$num=shift@str;
chomp$num;
$conv="
%"
.++$num."
s"
printf$conv,$_;
chapter6
-------------------------------------/home/confish/perl/hash
#hashsthatprintthename'
sfamilyname
#confish@ubun