1、Perl练习题DOC2.12 练习写一个程序,计算半径为12.5的圆的周长。圆周长等于2(约为3.1415926)乘以半径。答案为78.5。 #!/usr/bin/perl$r=12.5;$pai=3.1415926;$C=2*$pai*$r;Print “$Cn”;修改上述程序,用户可以在程序运行时输入半径。如果,用户输入12.5,则应得到和上题一样的结果。 #!/usr/bin/perl$r=;$pai=3.1415926;$C=2*$pai*$r;Print “$Cn”;修改上述程序,当用户输入小于0 的数字时,程序输出的周长为0,而非负数。 #!/usr/bin/perl$r=;$pa
2、i=3.1415926;if($r=0)$C=2*$pai*$r;If($r0)$C=0;Print “$Cn”;写一个程序,用户能输入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
3、$string=;$int=;$output=$string x $intprint $output;3.9练习写一个程序,将一些字符串(不同的行)读入一个列表中,逆向输出它。如果是从键盘输入的,那在Unix 系统中应当使用CTRL+D 表明end-of-file,在Windows 系统中使用CTRL+Z. 写一个程序,读入一串数字(一个数字一行),将和这些数字对应的人名(下面列出的)输出来。(将下面的人名列表写入代码中)。fred betty barney dino Wilma pebbles bamm-bamm例如,当输入为1,2,4 和2,则输出的为fred, betty, dino,
4、和betty 写一个程序,将一些字符串(在不同的行中)读入一个列表中。然后按ASCII 顺序将它们输出来。也就是说,当输入的字符串为fred, barney, wilma, betty,则输出为barney betty fred wilma。分别在一行或不同的行将之输出。 1:#!/usr/bin/perl -wmichael=reverse();print michael;或:#!/usr/bin/perl -wuserinput=;foreach (userinput)unshift (array,$_);print array is arrayn;2:#!/usr/bin/perlnam
5、e=qw(fred betty barney dino Wilma pebbles bamm-bamm);number=;foreach (number)print $name$_-1n;3:#!/usr/bin/perlarray=;array=sort array;print array;4.11练习写一个名为&total 的子程序,返回一列数字的和。提示:子程序不应当有任何的I/O 操作;它处理调用的参数,返回处理后的值给调用者。结合下面的程序来练习,它检测此子程序是否正常工作。第一组数组之和我25。my fred = qw 1 3 5 7 9 ;my $fred_total = &to
6、tal(fred);print The total of fred is $fred_total.n;print Enter some numbers on separate lines: ;my $user_total = &total();print The total of those numbers is $user_total.n; 利用上题的子程序,写一个程序计算从1 到1000 的数字的和。 额外的练习:写一个子程序,名为&above_average,将一列数字作为其参数,返回所有大于平均值的数字(提示:另外写一个子程序来计算平均值,总和除以数字的个数)。利用下面的程序进行测试:
7、my fred = &above_average(1.10);print fred is fredn;print (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; 1:#!/usr/bin/perl -w#Date:2009-6-12# Exercise 4-1use strict;sub total my $sum=shift _; foreach (_) $sum=$sum+$_; $sum;my fred=qw
8、 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();print The Total of those numbers is $user_total.n;2:#!/usr/bin/perl -w#Date:2009-6-12# Exercise 4-2use strict;sub total my $sum=shift _; foreach (_)
9、$sum=$sum+$_; $sum;my array=1 . 1000;my $array_total=&total(array);print The sum of 1 to 1000 is $array_total.n;3:#!/usr/bin/perl -w#Date:2009-6-12# Exercise 4-3use strict;sub average my $number=_; my $sum=shift _; foreach (_) $sum=$sum+$_; my $array_average=$sum/$number;sub above_average my above;
10、foreach (_) if ($_&average(_) push above,$_; above;my above_fred = &above_average(1 . 10);print The above average of fred is above_fredn;my above_barney = &above_average(100, 1 . 10);print The above average of barney is above_barneyn;5.11练习写一个程序,类似于cat,但保持输出的顺序关系。(某些系统的名字可能是tac。)如果运行此程序:./tac fred b
11、arney betty, 输出将是文件betty 的内容,从最后一行到第一行,然后是barney, 最后是fred, 同样是从最后一行到第一行。(注意使用./确保调用的是你自己的程序,而非系统提供的) 写一个程序,要求用户在不同的行中输入一些字符串,将此字符串打印出来,规则是:每一条占20 个字符宽度,右对齐。为了确保正确的输出,在开头打印出一串数字作为比较(帮助调试)。注意,不要犯19 个字符宽度的错误。例如,如果输入,hello, good-bye,则输出为:123456789012345678901234567890123456789012345678901234567890 hello
12、 good-bye 修改上一个程序,允许用户选择宽度,如,用户输入30,hello, good-bye(在不同的行中),则每一行的宽度为30。(提示:参阅第二章相应部分)。提示,如果选择的宽度太长,可以增加比较行的长度。 1:#!/usr/bin/perl -w#Date:2009-6-18# Exercise 5-1print reverse ;2-3:#!/usr/bin/perl -w#Date:2009-6-18# Exercise 5-2print What column width would you like?;chomp (my $width = );print Enter s
13、ome lines, then press Ctrl+D:n;chomp (my line = );print 1234567890 x ($width+9)/10), n;foreach (line) printf %$widthsn, $_;chapter21.-/home/confish/perl/girth#!/usr/bin/perl -w#this program calculate a circles girth#confishubuntu7.10$r=12.5;$g=12.5*2*3.1415;print the girth of the circle is $gn;-/hom
14、e/confish/perl/girth2.-/home/confish/perl/girthpro#!/usr/bin/perl -w#a better one to calculate girth#confishubuntu7.10printenter the radius of the circlen;chomp($r=);if($r0) printthe girth of the circle is .$r*2*3.1415.n; else printnonavailable!n; -/home/confish/perl/girthpro3.-/home/confish/perl/gi
15、rthzero#!/usr/bin/perl -w#calculate the girth and print 0 when the radius is lower than 0#confishubuntu7.10printenter the radius of the linen;chomp($r=);if($r0) printthe girth of the circle is $r*2*3.1415n; else printthe girth of the circle is 0n; -/home/confish/perl/girthzero4.-/home/confish/perl/p
16、roduct#!/usr/bin/perl -w#print the two numbers product#confishubuntu7.10printenter the two numbers:n;chomp($m=);chomp($n=);printthe product of the two numbers are .$m*$n.n;-/home/confish/perl/product5.-/home/confish/perl/printer#!/usr/bin/perl -w#print a string certain times depend on the usrs input
17、#confishubuntu7.10printenter a string and a number:n;$str=;chomp($num=);print $strx$num;-/home/confish/perl/printerchapter31.-/home/confish/reprint#!/usr/bin/perl -w#read some input and print them in reverse sequence#confishubuntu7.10print enter the string please:n;str=reverse ;print nthe reverse st
18、rings are:nstr;-/home/confish/reprint2.-/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/;printenter the numbers please:n;chomp(nums=);foreach(nums) re=names; while($i ne $_) $n=shi
19、ft( re); $i+; $i=0; print $n,n; -/home/confish/num_to_name3.-/home/confish/sort_str#!/usr/bin/perl -w#read some strings and sort them in ASCII#confishubuntu7.10chomp(str=sort);#str=sort; will print them in diffrent linesprint str,n;-/home/confish/sort_strchapter41.-/home/confish/perl/subr#!/usr/bin/
20、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);printThe total of fred is $fred_total.n;printEnter some numbers on separate lines:n;my $user_total=&total();printThe total of those nu
21、mbers is $user_total.n;-/home/confish/perl/subr2.-/home/confish/perl/suber#!/usr/bin/perl -w#use the subroutine in last program to get the sum of 1.1000#confishubuntu7.10sub total foreach $n(0.$#_) $sum+=$_$n; $sum; num=(1.1000);$sum=&total(num);printThe sum of 1.1000 is $sumn;-/home/confish/perl/su
22、ber3.-/home/confish/perl/aver#!/usr/bin/perl -w#to print the number which is larger than the average#in some numbers#confishubuntu7.10sub average foreach $n(0.$#_) $sum+=$_$n; $average=$sum/($#_+1); sub above_average num=_; aba=(); $av=&average(num); foreach $n(0.$#_) if($_$n$av) push ( aba,$_$n); a
23、ba; my fred=&above_average(1.10);printfred is fredn;print(Shuold be 6 7 8 9 10)n;my barney=&above_average(100,1.10);printbarney is barneyn;print(Should be just 100)n;-/home/confish/perl/averchapter51.-/home/confish/perl/tac#!/usr/bin/perl -w#a prog same as cat but reverse the string#confishubuntu7.1
24、0ARGV=reverse ARGV;a=reverse;print a;-/home/confish/perl/tac2.-/home/confish/perl/20str#!/usr/bin/perl -w#a prog that print the strings as 20 words flush right#confishubuntu7.10str=;while($i!=5) foreach(0.9) print; $i+; printn;foreach(str) printf %21s,$_; -/home/confish/perl/20str3.-/home/confish/pe
25、rl/20strpro#!/usr/bin/perl -w#a prog print the strings as number usr apionted words flush right#confishubuntu7.10str=;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/20strprochapter61.-/home/confish/perl/hash#!/usr/bin/perl -w#hashs that print the names family name#confishu
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1