Perl练习题DOC.docx

上传人:b****5 文档编号:3545959 上传时间:2022-11-23 格式:DOCX 页数:31 大小:25.13KB
下载 相关 举报
Perl练习题DOC.docx_第1页
第1页 / 共31页
Perl练习题DOC.docx_第2页
第2页 / 共31页
Perl练习题DOC.docx_第3页
第3页 / 共31页
Perl练习题DOC.docx_第4页
第4页 / 共31页
Perl练习题DOC.docx_第5页
第5页 / 共31页
点击查看更多>>
下载资源
资源描述

Perl练习题DOC.docx

《Perl练习题DOC.docx》由会员分享,可在线阅读,更多相关《Perl练习题DOC.docx(31页珍藏版)》请在冰豆网上搜索。

Perl练习题DOC.docx

Perl练习题DOC

2.12练习

写一个程序,计算半径为12.5的圆的周长。

圆周长等于2π(π约为3.1415926)乘以半径。

答案为78.5。

#!

/usr/bin/perl

$r=12.5;

$pai=3.1415926 ;

$C=2*$pai*$r;

Print“$C\n”;

修改上述程序,用户可以在程序运行时输入半径。

如果,用户输入12.5,则应得到和上题一样的结果。

#!

/usr/bin/perl

$r=;

$pai=3.1415926 ;

$C=2*$pai*$r;

Print“$C\n”;

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

#!

/usr/bin/perl

$r=;

$pai=3.1415926 ;

if($r>=0){

$C=2*$pai*$r;

}

If($r<0){

$C=0;

}

Print“$C\n”;

写一个程序,用户能输入2个数字(不在同一行)。

输出为这两个数的积。

#!

/usr/bim/perl

$a=;

$b=;

$c=$a*$b;

Print”$c”;

 

写一个程序,用户能输入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";

或:

#!

/usr/bin/perl-w

@userinput=;

foreach(@userinput)

{

unshift(@array,$_);

}

print"arrayis@array\n";

2:

#!

/usr/bin/perl

@name=qw(fredbettybarneydinoWilmapebblesbamm-bamm);

@number=<>;

foreach(@number)

{

print"$name[$_-1]\n";

}

3:

#!

/usr/bin/perl

@array=<>;

@array=sort@array;

print"@array";

 

4.11练习

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

提示:

子程序不应当有任何的I/O操作;它处理调用的参数,返回处理后的值给调用者。

结合下面的程序来练习,它检测此子程序是否正常工作。

第一组数组之和我25。

my@fred=qw{13579};

my$fred_total=&total(@fred);

print"Thetotalof\@fredis$fred_total.\n";

print"Entersomenumbersonseparatelines:

";

my$user_total=&total();

print"Thetotalofthosenumbersis$user_total.\n";

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

额外的练习:

写一个子程序,名为&above_average,将一列数字作为其参数,返回所有大于平均值的数字(提示:

另外写一个子程序来计算平均值,总和除以数字的个数)。

利用下面的程序进行测试:

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

print"\@fredis@fred\n";

print"(Shouldbe678910)\n";

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

print"\@barneyis@barney\n";

print"(Shouldbejust100)\n";

1:

#!

/usr/bin/perl-w

#Date:

2009-6-12

#Exercise4-1

usestrict;

subtotal{

my$sum=shift@_;

foreach(@_){

$sum=$sum+$_;

}

$sum;

}

my@fred=qw{13579};

my$fred_total=&total(@fred);

print"TheTotalof\@fredis$fred_total.\n";

print"Entersomenumbersonseparatelines:

";

my$user_total=&total();

print"TheTotalofthosenumbersis$user_total.\n";

2:

#!

/usr/bin/perl-w

#Date:

2009-6-12

#Exercise4-2

usestrict;

subtotal{

my$sum=shift@_;

foreach(@_){

$sum=$sum+$_;

}

$sum;

}

my@array=1..1000;

my$array_total=&total(@array);

print"Thesumof1to1000is$array_total.\n";

3:

#!

/usr/bin/perl-w

#Date:

2009-6-12

#Exercise4-3

usestrict;

subaverage{

my$number=@_;

my$sum=shift@_;

foreach(@_){

$sum=$sum+$_;

}

my$array_average=$sum/$number;

}

subabove_average{

my@above;

foreach(@_){

if($_>&average(@_)){

push@above,$_;

}

}

@above;

}

my@above_fred=&above_average(1..10);

print"Theaboveaverageof\@fredis@above_fred\n";

my@above_barney=&above_average(100,1..10);

print"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。

(提示:

参阅第二章相应部分)。

提示,如果选择的宽度太长,可以增加比较行的长度。

1:

#!

/usr/bin/perl-w

#Date:

2009-6-18

#Exercise5-1

printreverse<>;

2-3:

#!

/usr/bin/perl-w

#Date:

2009-6-18

#Exercise5-2

print"Whatcolumnwidthwouldyoulike?

";

chomp(my$width=);

print"Entersomelines,thenpressCtrl+D:

\n";

chomp(my@line=);

print"1234567890"x(($width+9)/10),"\n";

foreach(@line){

printf"%${width}s\n",$_;

}

 

chapter2

1.

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

#!

/usr/bin/perl-w

#thisprogramcalculateacircle'sgirth

#confish@ubuntu7.10

$r=12.5;

$g=12.5*2*3.1415;

print"thegirthofthecircleis$g\n";

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

2.

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

#!

/usr/bin/perl-w

#abetteronetocalculategirth

#confish@ubuntu7.10

print"entertheradiusofthecircle\n";

chomp($r=);

if($r>0)

{

print"thegirthofthecircleis".$r*2*3.1415."\n";

}

else

{

print"nonavailable!

\n";

}

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

3.

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

#!

/usr/bin/perl-w

#calculatethegirthandprint0whentheradiusislowerthan0

#confish@ubuntu7.10

print"entertheradiusoftheline\n";

chomp($r=);

if($r>0)

{

print"thegirthofthecircleis$r*2*3.1415\n";

}

else

{

print"thegirthofthecircleis0\n";

}

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

4.

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

#!

/usr/bin/perl-w

#printthetwonumber'sproduct

#confish@ubuntu7.10

print"enterthetwonumbers:

\n";

chomp($m=);

chomp($n=);

print"theproductofthetwonumbersare".$m*$n."\n";

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

5.

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

#!

/usr/bin/perl-w

#printastringcertaintimesdependontheusr'sinput

#confish@ubuntu7.10

print"enterastringandanumber:

\n";

$str=;

chomp($num=);

print${str}x$num;

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

chapter3

1.

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

#!

/usr/bin/perl-w

#readsomeinputandprinttheminreversesequence

#confish@ubuntu7.10

print"enterthestringplease:

\n";

@str=reverse;

print"\nthereversestringsare:

\n@str";

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

2.

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

#!

/usr/bin/perl-w

#readsomenumbersandoutputthematchname

#confish@ubuntu7.10

$i=0;

@names=qw/fredbettybarneydinoWilmapebblesbamm-bamm/;

print"enterthenumbersplease:

\n";

chomp(@nums=);

foreach(@nums)

{

@re=@names;

while($ine$_)

{

$n=shift(@re);

$i++;

}

$i=0;

print$n,"\n";

}

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

3.

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

#!

/usr/bin/perl-w

#readsomestringsandsorttheminASCII

#confish@ubuntu7.10

chomp(@str=sort);

#@str=sort;willprintthemindiffrentlines

print@str,"\n";

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

chapter4

1.

--------------------------------/home/confish/perl/subr

#!

/usr/bin/perl-w

#asubroutinenamedtotalreturnssumofnumbers

#confish@ubuntu7.10

subtotal

{

foreach$n(0..$#_)

{

$sum+=$_[$n];

}

$sum;

}

my@fred=qw{13579};

my$fred_total=&total(@fred);

print"Thetotalof\@fredis$fred_total.\n";

print"Entersomenumbersonseparatelines:

\n";

my$user_total=&total();

print"Thetotalofthosenumbersis$user_total.\n";

--------------------------------/home/confish/perl/subr

2.

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

#!

/usr/bin/perl-w

#usethesubroutineinlastprogramtogetthesumof1..1000

#confish@ubuntu7.10

subtotal

{

foreach$n(0..$#_)

{

$sum+=$_[$n];

}

$sum;

}

@num=(1..1000);

$sum=&total(@num);

print"Thesumof1..1000is$sum\n";

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

3.

--------------------------------/home/confish/perl/aver

#!

/usr/bin/perl-w

#toprintthenumberwhichislargerthantheaverage

#insomenumbers

#confish@ubuntu7.10

subaverage

{

foreach$n(0..$#_)

{

$sum+=$_[$n];

}

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

}

subabove_average

{

@num=@_;

@aba=();

$av=&average(@num);

foreach$n(0..$#_)

{

if($_[$n]>$av)

{

push(@aba,$_[$n]);

}

}

@aba;

}

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

print"\@fredis@fred\n";

print"(Shuoldbe678910)\n";

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

print"\@barneyis@barney\n";

print"(Shouldbejust100)\n";

--------------------------------/home/confish/perl/aver

chapter5

1.

----------------------------------/home/confish/perl/tac

#!

/usr/bin/perl-w

#aprogsameascatbutreversethestring

#confish@ubuntu7.10

@ARGV=reverse@ARGV;

@a=reverse<>;

print@a;

----------------------------------/home/confish/perl/tac

2.

----------------------------------/home/confish/perl/20str

#!

/usr/bin/perl-w

#aprogthatprintthestringsas20wordsflushright

#confish@ubuntu7.10

@str=;

while($i!

=5)

{

foreach(0..9)

{

print;

}

$i++;

}

print"\n";

foreach(@str)

{

printf"%21s",$_;

}

----------------------------------/home/confish/perl/20str

3.

----------------------------------/home/confish/perl/20strpro

#!

/usr/bin/perl-w

#aprogprintthestringsasnumberusrapiontedwordsflushright

#confish@ubuntu7.10

@str=;

while($i!

=5)

{

foreach(0..9)

{

print;

}

$i++;

}

print"\n";

$num=shift@str;

chomp$num;

$conv="%".++$num."s";

foreach(@str)

{

printf$conv,$_;

}

----------------------------------/home/confish/perl/20strpro

chapter6

1.

-------------------------------------/home/confish/perl/hash

#!

/usr/bin/perl-w

#hashsthatprintthename'sfamilyname

#confish@u

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 初中教育 > 数学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1