Perl练习题.doc_第1页
Perl练习题.doc_第2页
Perl练习题.doc_第3页
Perl练习题.doc_第4页
Perl练习题.doc_第5页
已阅读5页,还剩21页未读 继续免费阅读

下载本文档

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

文档简介

2.12 练习写一个程序,计算半径为12.5的圆的周长。圆周长等于2(约为3.1415926)乘以半径。答案为78.5。 修改上述程序,用户可以在程序运行时输入半径。如果,用户输入12.5,则应得到和上题一样的结果。 修改上述程序,当用户输入小于0 的数字时,程序输出的周长为0,而非负数。 写一个程序,用户能输入2 个数字(不在同一行)。输出为这两个数的积。 写一个程序,用户能输入1 个字符串和一个数字(n)(不在同一行)。输出为,n 行这个字符串,1 次1 行(提示,使用“x”操作符)。例如,如果用户输入的是“fred”和“3”,则输出为:3 行,每一行均为fred。如果输入为“fred”和“299792”,则输出为299792 行,每一行均为fred。 1、2、3:#!/usr/bin/perl -w$pai=3.141592654;print Please Input Radius:;$r=;if ( $r lt 0 ) print The circumference is 0n; else $l=$r*2*$pai; printf The circumference is %.1fn,$l;4:#!/usr/bin/perl -wprint Please Input Number1:;$num1=;print Please Input Number2:;$num2=;$product=$num1*$num2;print The Product Is: $productn;5:#!/usr/bin/perl -wprint Please Input a character string:;$character=;print Please Input a Number:;$number=;$output=$character x $number;print $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, 和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/perlname=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 = &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; 利用上题的子程序,写一个程序计算从1 到1000 的数字的和。 额外的练习:写一个子程序,名为&above_average,将一列数字作为其参数,返回所有大于平均值的数字(提示:另外写一个子程序来计算平均值,总和除以数字的个数)。利用下面的程序进行测试: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 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 (_) $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; 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 barney betty, 输出将是文件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# 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 some 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;-/home/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/girthzero#!/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/product#!/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#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 strings 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=shift( 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/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 numbers 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/suber3.-/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); aba; 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.10ARGV=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/perl/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#confishubuntu7.10$family_namefred=flintstone;$family_namebarney=rubble;$family_namewilma=flintstone;print enter the given name please:n;chomp($gn=);print family name for $gn is $family_name$gnn;-/home/confish/perl/hash2.-/home/confish/perl/counts#!/usr/bin/perl -w#hashs 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 chapter71.-/home/confish/perl/pfred#!/usr/bin/perl -w#prog print the line which contains fred#confishubuntu7.10foreach() if(/fred/) print $_; -/home/confish/perl/pfred2.-/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.-/home/confish/perl/pp#!/usr/bin/perl -w#prog print the line which contains a point#confishubuntu7.10foreach() if(/./) print $_; -/home/confish/perl/pp4.-/home/confish/perl/plg#!/usr/bin/perl -w#print the line which contains not capitals only#confishubuntu7.10foreach() if(/a-zA-Z|A-Z+a-z/) print $_; -/home/confish/perl/plg5.-/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/confish/perl/pfwchapter81.-/home/confish/perl/mm#!/usr/bin/perl -w#match match#confishubuntu7.10while() chomp; if(/match/) print Matched:|$|n; else print no match:|$_|n; -/home/confish/perl/mm2.-/home/confish/perl/ma#!/usr/bin/perl -w#match word ends with a#confishubuntu7.10while() chomp; if(/ab/) print Matched:|$|n; else print no match:|$_|n; -/home/confish/perl/ma3.-/home/confish/perl/mapro#!/usr/bin/perl -w #match word end with a and storage it#confishubuntu7.10while() chomp; if(/(a$)/) my $temp=$1; print $1 contains $&$n; else print no match:|$_|n; -/home/confish/perl/mapro4.-/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.-/home/confish/perl/ms#!/usr/bin/perl -w#match a space#confishubuntu7.10while() if(/ +$/) print; -/home/confish/perl/mschapter91.-/home/confish/perl/sfb#!/usr/bin/perl -w #match and replace fred three times#confishubuntu7.10my $what=fred|barney;if(fredbarneybarney=/($what)3/)print $./.$&./.$.n;-/home/confish/perl/sfb2.-/home/confish/perl/sfl#!/usr/bin/perl -w #match fred to Larry#confishubuntu7.10$I=.out;while() s/fred/Larry/i; print; -/home/confish/perl/sfl3.-/home/confish/perl/addc#!/usr/bin/perl -w#add copyright#confishubuntu7.10$I=.out;$a=/usr/bin/perl -w n #Copyright(C) 2008 by Yours Truly confish;while() s#/usr/bin/perls+-w#$a#i; print; -/home/confish/perl/addcchapter101.-/home/confish/perl/gn#!/usr/bin/perl -w#a game to guess a number#confishubuntu7.10my $y=int(1+rand(100);printenter a number please:n;while(chomp($t=) if($t ne exit&$t nequit ) if($t$y) printtow hign; elsif($t$such) $such=-M; $file=$_; print$file has exits $such daysn; else print no input filesn; -/home/confish/perl/et14.13 bash shell的习题习题54 第一个bash shell脚本1. 编写一个名为greetme的脚本,它包括以下内容。a) 包含一段注释,列出您的姓名、脚本的名称和编写这个脚本的目的。b) 问候用户。c) 显示日期和时间。d) 显示这个月的日历。e) 显示您的机器名。f) 显示当前这个操作系统的名称和版本(cat /etc/motd)。g) 显示父目录中的

温馨提示

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

评论

0/150

提交评论