-
2009-06-18
《Perl语言入门》第四版习题(四) - [Perl]
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
#<Leanning Perl 4th Edition> Exercise 5-1
print reverse <>;
2-3:
#!/usr/bin/perl -w
#Date:2009-6-18
#<Leanning Perl 4th Edition> Exercise 5-2
print "What column width would you like?";
chomp (my $width = <STDIN>);
print "Enter some lines, then press Ctrl+D:\n";
chomp (my @line = <STDIN>);
print "1234567890" x (($width+9)/10), "\n";
foreach (@line) {
printf "%${width}s\n", $_;
} -
2009-06-12
《Perl语言入门》第四版习题(三) - [Perl]
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(<STDIN>);
print "The total of those numbers is $user_total.\n"; - 利用上题的子程序,写一个程序计算从1 到1000 的数字的和。
- 额外的练习:写一个子程序,名为&above_average,将一列数字作为其参数,返回所有大于平均值的数字(提示:另外写一个子程序来计算平均值,总和除以数字的个数)。利用下面的程序进行测试:
my @fred = &above_average(1..10);
print "\@fred is @fred\n";
print "(Should 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";
1:
#!/usr/bin/perl -w
#Date:2009-6-12
#<Leanning Perl 4th Edition> Exercise 4-1
use 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(<STDIN>);
print "The Total of those numbers is $user_total.\n";
2:
#!/usr/bin/perl -w
#Date:2009-6-12
#<Leanning Perl 4th Edition> Exercise 4-2
use 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
#<Leanning Perl 4th Edition> Exercise 4-3
use 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_fred\n";
my @above_barney = &above_average(100, 1 .. 10);
print "The above average of \@barney is @above_barney\n"; - 写一个名为&total 的子程序,返回一列数字的和。提示:子程序不应当有任何的I/O 操作;它处理调用的参数,返回处理后的值给调用者。结合下面的程序来练习,它检测此子程序是否正常工作。第一组数组之和我25。
-
2009-06-09
《Perl语言入门》第四版习题(二) - [Perl]
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 -w
@michael=reverse(<>);
print "@michael";
或:
#!/usr/bin/perl -w
@userinput=<STDIN>;
foreach (@userinput)
{
unshift (@array,$_);
}
print "array is @array\n";2:
#!/usr/bin/perl
@name=qw(fred betty barney dino Wilma pebbles bamm-bamm);
@number=<>;
foreach (@number)
{
print "$name[$_-1]\n";
}3:
#!/usr/bin/perl
@array=<>;
@array=sort @array;
print "@array";







