发新话题
打印

[原创] Learning Perl练习题解答(第2-11章)

本主题由 System 于 2008-9-1 05:00 解除限时精华

Learning Perl练习题解答(第2-11章)

这是我自己做的..可能会有问题....
你要找标准答案请下载英文原版.......
没有用USE STRICT,没有做输入检测...那些好像是比较高级的话题......
没有注释,因为太简单.....
欢迎讨论....

[ 本帖最后由 zhponly 于 2008-3-9 18:01 编辑 ]
我的每一句话都是在灌水,只是灌水成分多少决定的它们的价值~

TOP

复制内容到剪贴板
代码:
chapter2
1.
-----------------------/home/confish/perl/girth
#!/usr/bin/perl -w
#this program calculate a circle's girth
#confish@ubuntu7.10
$r=12.5;
$g=12.5*2*3.1415;
print "the girth of the circle is $g\n";
-----------------------/home/confish/perl/girth
2.
-----------------------/home/confish/perl/girthpro
#!/usr/bin/perl -w
#a better one to calculate girth
#confish@ubuntu7.10
print"enter the radius of the circle\n";
chomp($r=<STDIN>);
if($r>0)
  {
   print"the girth of the circle is ".$r*2*3.1415."\n";
   }
else
   {
    print"nonavailable!\n";
    }
-----------------------/home/confish/perl/girthpro
3.
-----------------------/home/confish/perl/girthzero
#!/usr/bin/perl -w
#calculate the girth and print 0 when the radius is lower than 0
#confish@ubuntu7.10
print"enter the radius of the line\n";
chomp($r=<STDIN>);
if($r>0)
  {
   print"the girth of the circle is $r*2*3.1415\n";
   }
else
   {
    print"the girth of the circle is 0\n";
    }
-----------------------/home/confish/perl/girthzero
4.
-----------------------/home/confish/perl/product
#!/usr/bin/perl -w
#print the two number's product
#confish@ubuntu7.10
print"enter the two numbers:\n";
chomp($m=<STDIN>);
chomp($n=<STDIN>);
print"the product of the two numbers are ".$m*$n."\n";
-----------------------/home/confish/perl/product
5.
-----------------------/home/confish/perl/printer
#!/usr/bin/perl -w
#print a string certain times depend on the usr's input
#confish@ubuntu7.10
print"enter a string and a number:\n";
$str=<STDIN>;
chomp($num=<STDIN>);
print ${str}x$num;
-----------------------/home/confish/perl/printer
chapter3
1.
------------------------------------/home/confish/reprint
#!/usr/bin/perl -w
#read some input and print them in reverse sequence
#confish@ubuntu7.10
print "enter the string please:\n";
@str=reverse <STDIN>;
print "\nthe reverse strings are:\n@str";
------------------------------------/home/confish/reprint
2.
------------------------------------/home/confish/num_to_name
#!/usr/bin/perl -w
#read some numbers and output the match name
#confish@ubuntu7.10   
$i=0;
@names=qw /fred betty barney dino Wilma pebbles bamm-bamm/;
print"enter the numbers please:\n";
chomp(@nums=<STDIN>);
foreach(@nums)
               
       {
         
        @re=@names;
        while($i ne $_)

            {
                                           $n=shift( @re);
              $i++;
      
              }
                       $i=0;
      
         print $n,"\n";
        }
------------------------------------/home/confish/num_to_name
3.
------------------------------------/home/confish/sort_str
#!/usr/bin/perl -w
#read some strings and sort them in ASCII
#confish@ubuntu7.10
chomp(@str=sort<STDIN>);
#@str=sort<STDIN>; will print them in diffrent lines
print @str,"\n";
------------------------------------/home/confish/sort_str
[ 本帖最后由 zhponly 于 2008-3-9 17:51 编辑 ]
我的每一句话都是在灌水,只是灌水成分多少决定的它们的价值~

TOP

复制内容到剪贴板
代码:
chapter4
1.
--------------------------------/home/confish/perl/subr
#!/usr/bin/perl -w
#a subroutine named total returns sum of numbers
#confish@ubuntu7.10
sub total
    {
     foreach $n(0..$#_)
            {
             $sum+=$_[$n];
             }
   
     $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:\n";
my $user_total=&total(<STDIN>);
print"The total of those numbers is $user_total.\n";
--------------------------------/home/confish/perl/subr
2.
--------------------------------/home/confish/perl/suber
#!/usr/bin/perl -w
#use the subroutine in last program to get the sum of 1..1000
#confish@ubuntu7.10
sub total
    {
     foreach $n(0..$#_)
            {
             $sum+=$_[$n];
             }
      $sum;
     }
@num=(1..1000);
$sum=&total(@num);
print"The sum of 1..1000 is $sum\n";
--------------------------------/home/confish/perl/suber
3.
--------------------------------/home/confish/perl/aver
#!/usr/bin/perl -w
#to print the number which is larger than the average
#in some numbers
#confish@ubuntu7.10
sub 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);
print"\@fred is @fred\n";
print"(Shuold be 6 7 8 9 10)\n";
my @barney=&above_average(100,1..10);
print"\@barney is @barney\n";
print"(Should be just 100)\n";
--------------------------------/home/confish/perl/aver
chapter5
1.
----------------------------------/home/confish/perl/tac
#!/usr/bin/perl -w
#a prog same as cat but reverse the string
#confish@ubuntu7.10
@ARGV=reverse @ARGV;
@a=reverse<>;
print @a;
----------------------------------/home/confish/perl/tac
2.
----------------------------------/home/confish/perl/20str
#!/usr/bin/perl -w
#a prog that print the strings as 20 words flush right
#confish@ubuntu7.10
@str=<STDIN>;
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
#a prog print the strings as number usr apionted words flush right
#confish@ubuntu7.10
@str=<STDIN>;
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
#hashs that print the name's family name
#confish@ubuntu7.10
$family_name{"fred"}="flintstone";
$family_name{"barney"}="rubble";
$family_name{"wilma"}="flintstone";
print "enter the given name please:\n";
chomp($gn=<STDIN>);
print "family name for $gn is $family_name{$gn}\n";
------------------------------------/home/confish/perl/hash
2.
------------------------------------/home/confish/perl/counts
#!/usr/bin/perl -w
#hashs that counts the appearance times of the word
#print sorted
#confish@ubuntu7.10
print "enter the word please:\n";
foreach(<>)
     {
      $counts{$_}++;
      $counts{$_}.="\n";
      }
sort %counts;
print %counts;
------------------------------------/home/confish/perl/counts      
[ 本帖最后由 zhponly 于 2008-3-9 17:55 编辑 ]
我的每一句话都是在灌水,只是灌水成分多少决定的它们的价值~

TOP

复制内容到剪贴板
代码:
chapter7
1.
-------------------------------------------/home/confish/perl/pfred
#!/usr/bin/perl -w
#prog print the line which contains "fred"
#confish@ubuntu7.10
foreach(<>)
     {
      if(/fred/)
        {
         print $_;
         }
      }
-------------------------------------------/home/confish/perl/pfred
2.
-------------------------------------------/home/confish/perl/pffred
#!/usr/bin/perl -w
#prog print the line which contains "fred" or "Fred"
#confish@ubuntu7.10
foreach(<>)
       {
        if(/fred|Fred/)
          {
           print $_;
           }
        }
-------------------------------------------/home/confish/perl/pffred
3.
-------------------------------------------/home/confish/perl/pp
#!/usr/bin/perl -w
#prog print the line which contains a point
#confish@ubuntu7.10
foreach(<>)
       {
        if(/\./)
          {
           print $_;
           }
        }
-------------------------------------------/home/confish/perl/pp
4.
-------------------------------------------/home/confish/perl/plg
#!/usr/bin/perl -w
#print the line which contains not capitals only
#
confish@ubuntu7.10
foreach(<>)
       {
        if(/[a-z][A-Z]|[A-Z]+[a-z]/)
          {
           print $_;
           }
                 }
-------------------------------------------/home/confish/perl/plg
5.
-------------------------------------------/home/confish/perl/pfw
#!/usr/bin/perl -w
#print a line which contains fred and wilma
#confish@ubuntu7.10
foreach(<>)
       {
        if(/wilma.+fred|fred.+wilma/)
          {
           print $_;
           }
         }
------------------------------------------/home/confish/perl/pfw
chapter8
1.
----------------------------------------------/home/confish/perl/mm
#!/usr/bin/perl -w
#match "match"
#confish@ubuntu7.10
while(<>)
     {
      chomp;
      if(/match/)
        {
          print "Matched:|$`<$&>$'|\n";
         }
      else
         {
          print "no match:|$_|\n";
          }
      }
---------------------------------------------/home/confish/perl/mm
2.
---------------------------------------------/home/confish/perl/ma
#!/usr/bin/perl -w
#match "word" ends with a
#confish@ubuntu7.10
while(<>)
     {
      chomp;
      if(/a\b/)
        {
          print "Matched:|$`<$&>$'|\n";
         }
      else
         {
          print "no match:|$_|\n";
          }
      }
---------------------------------------------/home/confish/perl/ma
3.
---------------------------------------------/home/confish/perl/mapro
#!/usr/bin/perl -w
#match word end with a and storage it
#confish@ubuntu7.10
while(<>)
     {
      chomp;
      if(/(a$)/)
        {
          my $temp=$1;
          print "\$1 contains '$`$&$''\n";
         }
      else
         {
          print "no match:|$_|\n";
          }
      }
---------------------------------------------/home/confish/perl/mapro
4.
---------------------------------------------/home/confish/perl/mwa
#!/usr/bin/perl  -w
#match a word end with a and print the next five character
#confish@ubuntu7.10
while(<>)
     {
      if(/a\b/)
        {
          my $temp=$';
          if($temp=~/.{0,5}/)
            {
             my $match=$&;
             print $match;
             }
         }
      else
         {
          print "no match:|$_|\n";
          }
      }
---------------------------------------------/home/confish/perl/mwa
5.
---------------------------------------------/home/confish/perl/ms
#!/usr/bin/perl -w
#match a space
#confish@ubuntu7.10
while(<>)
     {
      if(/ +$/)
        {
         print;
         }
      }
---------------------------------------------/home/confish/perl/ms
chapter9
1.
---------------------------------------------/home/confish/perl/sfb
#!/usr/bin/perl -w

#match and replace fred three times
#confish@ubuntu7.10
my $what="fred|barney";
if("fredbarneybarney"=~/($what){3}/)
{
print $`."/".$&."/".$'."\n";
}
---------------------------------------------/home/confish/perl/sfb
2.
---------------------------------------------/home/confish/perl/sfl
#!/usr/bin/perl -w
#match fred to Larry
#confish@ubuntu7.10
$^I=".out";
while(<>)
     {
      s/fred/Larry/i;
      print;
      }
---------------------------------------------/home/confish/perl/sfl
3.
---------------------------------------------/home/confish/perl/addc
#!/usr/bin/perl -w
#add copyright
#confish@ubuntu7.10
$^I=".out";
$a="/usr/bin/perl -w \n #Copyright(C) 2008 by Yours Truly confish";
while(<>)
     {
      s#/usr/bin/perl\s+-w#$a#i;
      print;
      }
---------------------------------------------/home/confish/perl/addc
[ 本帖最后由 zhponly 于 2008-3-9 17:56 编辑 ]
我的每一句话都是在灌水,只是灌水成分多少决定的它们的价值~

TOP

复制内容到剪贴板
代码:
chapter10
1.
---------------------------------------------/home/confish/perl/gn
#!/usr/bin/perl -w
#a game to guess a number
#confish@ubuntu7.10
my $y=int(1+rand(100));
print"enter a number please:\n";
while(chomp($t=<STDIN>))
     {
      if($t ne "exit"&&$t ne"quit" )
        {
         if($t>$y)
           {
            print"tow hig\n";
            }
         elsif($t<$y)
           {
            print"two low\n";
            }
         else
           {
            print "a\n";last;
            }
         
        }
     else
        {
         last;
         }
     }
--------------------------------------------/home/confish/perl/gn
chapter11
1.
------------------------------------------------/home/confish/perl/crw
#!/usr/bin/perl -w
#to check whether a file is exits have readability and writeable
#confish@ubuntu7.10
foreach(@ARGV)
       {
        if(-e)
          {
           print "$_ exits\n";
           if(-r)
             {
              print "$_ readable\n";
              }
           else
              {
              print"$_ does not be readable\n";
              }
         if(-w)
             {
             print "$_ writeable\n";
             }
         else
            {
             print "$_ does not be writeable\n";
             }
         }
    else
       {
       print "$_ does not exits\n";
        }
    }
------------------------------------------------/home/confish/perl/crw
2.
------------------------------------------------/home/confish/perl/et
#!/usr/bin/perl -w
#to check the longest exits file
#confish@ubuntu7.10
if(@ARGV)
  {
   foreach(@ARGV)
          {
           if(-M>$such)
           {
            $such=-M;
            $file=$_;
            }
          }
    print"$file has exits $such days\n";
   }
else
   {
    print "no input files\n";
   }
------------------------------------------------/home/confish/perl/et
[ 本帖最后由 zhponly 于 2008-3-9 18:00 编辑 ]
我的每一句话都是在灌水,只是灌水成分多少决定的它们的价值~

TOP

不错~~~顶一下
好好学习~

TOP

发新话题