Perl语言入门(第四版)习题答案
- 格式:docx
- 大小:38.18 KB
- 文档页数:22
Perl语言入门(第四版)习题答案《Perl语言入门习题答案》2.12 练习1、写一个程序,计算半径为12.5的圆的周长。
圆周长等于2π(π约为3.1415926)乘以半径。
答案为78.5。
-----------------------/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/girth2、修改上述程序,用户可以在程序运行时输入半径。
如果,用户输入12.5,则应得到和上题一样的结果。
-----------------------/home/confish/perl/girthpro #!/usr/bin/perl -w#a better one to calculate girth#confish@ubuntu7.10print"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/girthpro3、修改上述程序,当用户输入小于0 的数字时,程序输出的周长为0,而非负数。
-----------------------/home/confish/perl/girthzero #!/usr/bin/perl -w#calculate the girth and print 0 when the radius is lower than 0#confish@ubuntu7.10print"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/girthzero1、2、3:(一起实现的)#!/usr/bin/perl -w$pai=3.141592654;print "Please Input Radius:";$r=<STDIN>;if ( $r lt 0 ){print "The circumference is 0\n";}else{$l=$r*2*$pai;printf "The circumference is %.1f\n",$l;}4、写一个程序,用户能输入2 个数字(不在同一行)。
汇编语言程序设计第四版【课后习题答案】第1章汇编语言基础知识〔习题1.1〕简述计算机系统的硬件组成及各部分作用。
〔解答〕CPU:包括运算器、控制器和寄存器组。
运算器执行所有的算术和逻辑运算;控制器负责把指指令逐条从存储器中取出,经译码分析后向机器发出各种控制命令,并正确完成程序所要求的功能;寄存器组为处理单元提供所需要的数据。
存储器:是计算机的记忆部件,它用来存放程序以及程序中所涉及的数据。
外部设备:实现人机交换和机间的通信。
〔习题1.2〕明确下列概念或符号:主存和辅存,RAM和ROM,存储器地址和I/O端口,KB、MB、GB和TB。
〔解答〕主存又称内存是主存储器的简称,主存储器存放当前正在执行的程序和使用的数据,CPU可以直接存取,它由半导体存储器芯片构成其成本高、容量小、但速度快。
辅存是辅助存储器的简称,辅存可用来长期保存大量程序和数据,CPU需要通过I/O接口访问,它由磁盘或光盘构成,其成本低、容量大,但速度慢。
RAM是随机存取存储器的英语简写,由于CPU可以从RAM读信息,也可以向RAM写入信息,所以RAM也被称为读写存储器,RAM型半导体存储器可以按地址随机读写,但这类存储器在断电后不能保存信息;而ROM中的信息只能被读出,不能被修改,ROM型半导体通常只能被读出,但这类存储器断电后能保存信息。
存储器由大量存储单元组成。
为了区别每个单元,我们将它们编号,于是,每个存储单元就有了一个存储地址,I/O接口是由一组寄存器组成,为了区别它们,各个寄存器进行了编号,形成I/O地址,通常称做I/O端口。
KB是千字节、MB是兆字节、GB是吉字节和TB是太字节,它们都是表示存储器存储单元的单位。
〔习题1.3〕什么是汇编语言源程序、汇编程序、目标程序?〔解答〕用汇编语言书写的程序就称为汇编语言源程序;完成汇编工作的程序就是汇编程序;由汇编程序编译通过的程序就是目标程序。
〔习题1.4〕汇编语言与高级语言相比有什么优缺点?〔解答〕汇编语言与高级语言相比的优点:由于汇编语言本质就是机器语言,它可以直接地、有效地控制计算机硬件,因而容易产生运行速度快,指令序列短小的高效目标程序,可以直接控制计算机硬件部件,可以编写在“时间”和“空间”两方面最有效的程序。
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=<STDIN>;$pai=3.1415926 ;$C=2*$pai*$r;Print “$C\n”;修改上述程序,当用户输入小于0 的数字时,程序输出的周长为0,而非负数。
#!/usr/bin/perl$r=<STDIN>;$pai=3.1415926 ;if($r>=0){$C=2*$pai*$r;}If($r<0){$C=0;}Print “$C\n”;写一个程序,用户能输入2 个数字(不在同一行)。
输出为这两个数的积。
#!/usr/bim/perl$a=<STDIN>;$b=<STDIN>;$c=$a*$b;Print”$c”;写一个程序,用户能输入1 个字符串和一个数字(n)(不在同一行)。
输出为,n 行这个字符串,1 次1 行(提示,使用“x”操作符)。
例如,如果用户输入的是“fred”和“3”,则输出为:3 行,每一行均为fred。
如果输入为“fred”和“299792”,则输出为299792 行,每一行均为fred。
#!/usr.bin/perl$string=<STDIN>;$int=<STDIN>;$output=$string x $intprint $output;3.9练习写一个程序,将一些字符串(不同的行)读入一个列表中,逆向输出它。
如果是从键盘输入的,那在Unix 系统中应当使用CTRL+D 表明end-of-file,在Windows 系统中使用CTRL+Z.写一个程序,读入一串数字(一个数字一行),将和这些数字对应的人名(下面列出的)输出来。
PERL复习题(答案不是标准答案,仅供参考)一、选择题1. What is the simplest type of data that Perl can work with?A elementB scalarC vectorD component2. Which operator can be used to take the bottom item from an array?A popB pushC pullD plant3. Which operator is used to arrange items in character order?A ascendB sortC arrangeD descend4. Rather than using print, what is often used in Perl when formatting is important?A printfB formatC alignD show5. Which modifier can be used when matching in Perl to ignore case?A sB vC iD c6. Which operator can be used to break up a string into more than one part based upon a separator?A chopB splitC divideD parse7. What option do you use when starting Perl to tell it to run in warning mode? _-w \ use warnings (Fill in the blank.)8. Which control structure can be used to execute only a condition is false?A untilB unlessC whileD without9. Which of the following commands should be used to open a filehandle named KAREN to an existing file named sw?A open KAREN “>sw”;B open KAREN, “>sw”;C open “sw” KAREN;D open “>sw”, KAREN;10. Within Perl, which operator is used to change the working directory?A cdB chdirC pwdD wd11. Which operator can be used to access the first item in an array?A shiftB startC right$D left$12. Within a loop, which operator immediately ends execution of the loop?A nextB lastC redoD leave13. Which function can be used to make sure your program exits with a nonzero status even if there a standard error?A hashB noexitC nozeroD die14. Which of the following operators work most like simple searching/pattern matching?A /fB /gC s///D m//15. Which keyword is used in Perl to define a subroutine?A branchB subC splitD make16. You want to close the directory handle EV AN. Which of the following should you use to accomplish this?A close EV AN;B closedir EV AN;C close_directory EV AN;D none of the above17. Which of the following lines of code accomplishes the same thing as $price = $price + 9;?A $price +9;B $price =+9;C $price +=9;D 9 + $price18. What value do variables have before they are first assigned?A undefB nullC 0D nil19. Which Perl function can be used to launch a child process to run a program?A splitB spinC forkD system20. Which Perl function can be used to identify the first found occurrence of a substring?A findB indexC locateD allocate21. Which control structure can be used to loop as long as a conditional expression returns true?A untilB unlessC whileD without22. Which operator can be used to create private variables within a subroutine?A nativeB limitedC myD regional23. Which of the following operators is used to return a value in a subroutine?A returnB sendC giveD show24. Your script has just read in a variable from the keyboard and you want to strip that variable of the newline character that came in. What operator should you use to perform this operation?A tidyB trimC chompD slim25. What is the default sort order in Perl?A alphabeticB numericC ASCIID none of the above26. Within a loop, which operator jumps to the end of the loop but does not exit the loop?A nextB lastC redoD leave27. (不确定)Which of the following should be used to print values in an array that do not contain newlines and add them to the end of each entry?A print @array;B print @array\n;C print “@array\n”;D print “$@array \\”;28. Which string comparison operator would be equivalent to the numeric > operator?A neB eqC geD gt29. Which function can be thought of as the opposite of split?A linkB joinC uniteD bond30. Which operator can be used to add an item to the bottom of an array?A popB pushC pullD plant31. Which of the following mathematical operations has the highest precedence in Perl?A **B ++C +D -32. 以下哪一个字符串直接量的定义方式是错误的()(1)'thank you'(2)" "(3)"a "friend" of yours"(4)"a \"friend\" of yours"33. 以下哪一条语句是错误的()(1)$_= 'hello world';(2)$a='hello world';(3)my $b,$a='hello world';(4)my ($a,$b)=(0,'hello world');34. 要使下面的程序正常运行,while后的表达式应选()。
Perl语言入门(第四版)习题答案————————————————————————————————作者:————————————————————————————————日期:《Perl语言入门习题答案》2.12 练习1、写一个程序,计算半径为12.5的圆的周长。
圆周长等于2π(π约为3.1415926)乘以半径。
答案为78.5。
-----------------------/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/girth2、修改上述程序,用户可以在程序运行时输入半径。
如果,用户输入12.5,则应得到和上题一样的结果。
-----------------------/home/confish/perl/girthpro#!/usr/bin/perl -w#a better one to calculate girth#confish@ubuntu7.10print"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/girthpro3、修改上述程序,当用户输入小于0 的数字时,程序输出的周长为0,而非负数。
#题2:#使用for循环打印出如下的字符。
# 1# 12# 123# 12345#*******************#Fw_Print_Step ($step++,"使用for循环打印出如下的字符。
11212312345");my $str= "";for (1..4) {$str= $str.$_;if ($_==4) {$str= $str.$_+1;}print " $str\n";}#*******************##题3:my $str1 = "abc";my $str2 = "efg";#将上述2个字符串连接起来,并输出合并后的字符串长度#*******************#Fw_Print_Step ($step++,"将上述2个字符串\"$str1\"和\"$str2\"连接起来,并输出合并后的字符串长度");my $str =$str1.$str2;my $str_length=length($str);print "新字串$str的长度为:$str_length";#*******************##题4:#以逆序方式打印出字符串包含的各个字符,如变量为"123456789"则输出为"9","8",..."2","1". my $str1="abc123def456";#*******************#Fw_Print_Step ($step++,"以逆序方式打印出字符串包含的各个字符,如变量为\"123456789\"则输出为\"9\",\"8\",...\"2\",\"1\".");my $str=$str1;print "以逆序方式打印出字符串\"$str1\"包含的各个字符:\n";for($length=length($str1); $length>0; $length--) {$sub_str=chop($str);if ($length>1) {print "\"$sub_str\",";} else {print "\"$sub_str\".";}}#*******************##题5:#分别使用for与while循环来计算1+2+3+...+100的值#*******************#Fw_Print_Step ($step++,"分别使用for与while循环来计算1+2+3+...+100的值"); print "用for循环计算1+2+3+...+100的值:\n ";my $result=0;for (1..100) {$result=$result+$_;}print "1+2+3+...+100=$result";print "\n用while循环计算1+2+3+...+100的值:\n ";my $result=0;my $num=1;while ($num<=100) {$result=$result+$num;$num++;}print "1+2+3+...+100=$result";#*******************##题6:#以逆序的方式打印出端口列表包含的成员口my @cmdArray = ("config", "int fa 0/1", "no shutdonw", "end");#*******************#Fw_Print_Step ($step++,"以逆序的方式打印出端口列表包含的成员口");for (my $start=$#cmdArray; $start>=0; $start--) {my $array=$cmdArray[$start];print "$array\n";}#*******************##题7:#使用foreach打印出Hash表的所有下标与值my %map = ('red', 0xff0000,'green', 0x00ff00,'blue',0x0000ff);#*******************#Fw_Print_Step ($step++,"使用foreach打印出Hash表的所有下标与值");foreach $capword (sort keys(%map)) {print ("$capword: $map{$capword}\n");}#while( ($key, $value) = Each (%Map)) {# Print "\N$key=$value;"}#*******************##题8:#使用正则匹配判断字符串是否包含error,若是打印提示信息。
2.9 根据柯西分布有当x 很大时, 所以有 (b) 当当0v ≤, 因此有:2.10 (a)(b)(c)因为n →∞, 不是高斯分布,因此中心极限定理不适用,原因是柯西分布没有有限的差异。
2.11 假定 是实值随机过程。
复值过程的处理也类似。
(a)(b)当x (t ), y (t )不相关时, 同理因此(3)当x (t ), y (t )不相关并且零均值时:0v ≥2.12 随机过程x(t)的功率谱密度为:滤波器输出功率谱密度为:因此滤波器输出总功率为:2.14令因此2.16 滤波器的传递函数为:(a)(b)令a = RC, v =2πf. 那么2.19 因为输出序列的自相关:这里的最后等式来自于X(n)的自相关函数:因此离散时间系统的频率响应为:综上,系统输出的功率密度谱为:2.20 已知功率密度谱为:2.21 本题中引用下标d表示离散过程,下标a表示连续时间过程,同样,f表示模拟频率。
f d表示离散频率。
(a)因此取样信号的自相关函数等于X(t)的取样自相关函数。
(b)令fd = fT,则有:又因为离散时间的自相关函数是它的功率谱密度的反变换,于是有:比较(1),(2):得(c) 从(3)式可以得出:否则出现混叠。
2.22 (a)(b) 如果 那么:K=0其他因此序列X (n )是白噪声序列,T 的最小值可以从下图的取样过程的功率谱密度得到为了得到一个谱平坦序列,最大的抽样速率应满足:可由得到。
(c)因此2211sin w ()()()w πτφτφτπτ==,2.23 假设 那么:这里Y(f)是y (t )的傅里叶变换,因为:又有:当f=0时:2.24由于G=1,有对低通滤波器,可得到:将H(f)代入可得下式3.4 要证而利用不等式当且仅当可得因为3.6 通过定义,差熵为:对均匀分布随机变量(a) a=1,H(X)=0(b) a=4,H(X)=log4=2log2(c) a=1/4,H(X)=log 14=-2log23.7 (a)(b)每信源字符的平均二进制个数为:(c)信源熵为:通过比较,信源熵要少于每个码字的平均长度。
练习3.6编程练习1~3题#include<stdio.h>int main(){float x,y;x=(3+10)/2.0;y=(4+12)/2.0;printf("%f\n",x);printf("%f\n",y);return 0;}4题#include<stdio.h>#define ER 2.0int main(){int x1,y1,x2,y2;float x3,y3;printf("请输入数据:");scanf("%d %d %d %d",&x1,&y1,&x2,&y2);x3=(x1+x2)/ER; /*注意应先输入数据后在计算*/ y3=(y1+y2)/ER;printf("(%5.2f,%5.2f)\n",x3,y3);return 0;}5题#include<stdio.h>#include<math.h>int main(){int num1;float num2;printf(“请输入数据:”);scanf(“%d”,&num1);num2 = pow(num1,0.25);printf(“输入数据的4次方根为:%f\n”,num2);return 0;}6题#include<stdio.h>#include<math.h>int main(){int X,N,R;float A;printf(“输入初始存款金额:”);scanf(“%d”,&X);printf(“输入存期:”);scanf(“%d”,&N);printf(“输入年利率:”);scanf(“%d”,&R);A = X*pow((1.0+R/100.0),N);printf(“获得的货币量:%f\n”,A);return 0;}7题1、分析问题a 、确定期望的输出。
查看所用的编译器文档,了解它所用的文件命名规范。
编译并运行本节的main 程序。
【解答】一般而言,C++编译器要求待编译的程序保存在文件中。
C++程序中一般涉及两类文件:头文件和源文件。
大多数系统中,文件的名字由文件名和文件后缀(又称扩展名)组成。
文件后缀通常表明文件的类型,如头文件的后缀可以是.h 或.hpp 等;源文件的后缀可以是.cc 或.cpp 等,具体的后缀与使用的编译器有关。
通常可以通过编译器所提供的联机帮助文档了解其文件命名规范。
习题1.2修改程序使其返回-1。
返回值-1 通常作为程序运行失败的指示器。
然而,系统不同,如何(甚至是否)报告main 函数运行失败也不同。
重新编译并再次运行程序,看看你的系统如何处理main 函数的运行失败指示器。
【解答】笔者所使用的Windows操作系统并不报告main 函数的运行失败,因此,程序返回-1 或返回0 在运行效果上没有什么区别。
但是,如果在DOS 命令提示符方式下运行程序,然后再键入echo %ERRORLEVEL%命令,则系统会显示返回值-1。
习题1.3编一个程序,在标准输出上打印“Hello, World”。
#include <iostream>#include "windows.h"using namespace std;int main(){ system("CLS");cout<<"Hello,World!"<<endl;return 0;}习题1.4我们的程序利用内置的加法操作符“+”来产生两个数的和。
编写程序,使用乘法操作符“*”产生两个数的积。
#include <iostream>#include "windows.h"using namespace std;int main(){ system("CLS");cout<<"Enter two numbers:"<<endl;int v1,v2;cin>>v1>>v2;cout<<"The product of "<<v1<<" and "<<v2<<" is "<<v1*v2<<endl;return 0;}习题1.5我们的程序使用了一条较长的输出语句。
目录目录 (1)第1章C语言编程 (4)练习1.1 (4)练习1.2 (5)练习1.3 (5)第2章编程初步 (6)习题2.1 (6)习题2.2 (7)习题2.3 (9)习题2.4 (10)第3章条件判断 (12)习题3.1 (12)习题3.2 (14)习题3.3 (19)习题3.4 (21)第4章循环 (24)习题4.1 (24)习题4.2 (26)习题4.4 (27)习题4.5 (29)第5章数组 (31)习题5.1 (31)习题5.2 (33)习题5.3 (35)习题5.4 (36)习题5.5 (39)第6章字符串和文本的应用 (41)习题6.1 (41)习题6.2 (50)习题6.3 (53)习题6.4 (53)第7章指针 (57)习题7.1 (57)习题7.2 (59)习题7.3 (61)习题7.4 (63)习题8.1 (65)习题8.2 (67)习题8.3 (69)习题8.4 (73)第9章函数再探 (79)习题9.1 (79)习题9.2 (80)习题9.3 (83)习题9.4 (85)第10章基本输入输出操作 (87)习题10.1 (87)习题10.2 (89)习题10.3 (91)习题10.4 (92)第11章结构化数据 (95)习题11.1 (95)习题11.2 (99)习题11.3 (103)习题11.5 (114)第12章处理文件 (119)习题12.1 (120)习题12.2 (121)习题12.3 (125)习题12.4 (127)第13章支持功能 (132)习题13.1 (133)习题13.2 (133)习题13.3 (135)《C语言入门经典(第4版)》课后练习参考答案第1章C语言编程练习1.1 编写一个程序,用两个printf()语句别离输出自己的名字和地址。
练习1.2将上一个练习修改成所有的输出只用一个printf()语句。
练习1.3编写一个程序,输出下列文本,格式如下所示:"It's freezing in here," he said coldly.第2章编程初步习题2.1 编写一个程序,提示用户用英寸输入一个距离,然后将该距离值输出为码、英尺和英寸的形式。
《Perl语言入门习题答案》练习1、写一个程序,计算半径为的圆的周长。
圆周长等于2π(π约为)乘以半径。
答案为。
-----------------------/home/confish/perl/girth#!/usr/bin/perl -w#this program calculate a circle's girth$r=;$g=*2*;print "the girth of the circle is $g\n";-----------------------/home/confish/perl/girth2、修改上述程序,用户可以在程序运行时输入半径。
如果,用户输入,则应得到和上题一样的结果。
-----------------------/home/confish/perl/girthpro#!/usr/bin/perl -w#a better one to calculate girthprint"enter the radius of the circle\n";chomp($r=<STDIN>);if($r>0){print"the girth of the circle is ".$r*2*."\n";}else{print"nonavailable!\n";}-----------------------/home/confish/perl/girthpro3、修改上述程序,当用户输入小于0 的数字时,程序输出的周长为0,而非负数。
-----------------------/home/confish/perl/girthzero#!/usr/bin/perl -w#calculate the girth and print 0 when the radius is lower than 0print"enter the radius of the line\n";chomp($r=<STDIN>);if($r>0){print"the girth of the circle is $r*2*\n";}else{print"the girth of the circle is 0\n";}-----------------------/home/confish/perl/girthzero1、2、3:(一起实现的)#!/usr/bin/perl -w$pai=;print "Please Input Radius:";$r=<STDIN>;if ( $r lt 0 ){print "The circumference is 0\n";}else{$l=$r*2*$pai;printf "The circumference is %.1f\n",$l;}4、写一个程序,用户能输入2 个数字(不在同一行)。
输出为这两个数的积。
-----------------------/home/confish/perl/product#!/usr/bin/perl -w#print the two number'sprint"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/product5、写一个程序,用户能输入1 个字符串和一个数字(n)(不在同一行)。
输出为,n 行这个字符串,1 次1 行(提示,使用“x”操作符)。
例如,如果用户输入的是“fred”和“3”,则输出为:3 行,每一行均为fred。
如果输入为“fred”和“299792”,则输出为299792 行,每一行均为fred-----------------------/home/confish/perl/printer#!/usr/bin/perl -w#print a string certain times depend on the usr'sprint"enter a string and a number:\n";$str=<STDIN>;chomp($num=<STDIN>);print ${str}x$num;-----------------------/home/confish/perl/printer练习1、写一个程序,将一些字符串(不同的行)读入一个列表中,逆向输出它。
如果是从键盘输入的,那在Unix 系统中应当使用CTRL+D 表明end-of-file,在Windows 系统中使用CTRL+Z.------------------------------------/home/confish/reprint#!/usr/bin/perl -w#read some input and print them in reverseprint "enter the string please:\n";@str=reverse <STDIN>;print "\nthe reverse strings are:\n@str";------------------------------------/home/confish/reprint2、写一个程序,读入一串数字(一个数字一行),将和这些数字对应的人名(下面列出的)输出来。
(将下面的人名列表写入代码中)。
fred betty barney dino Wilma pebbles bamm-bamm 例如,当输入为1,2,4 和2,则输出的为fred, betty, dino, 和betty------------------------------------/home/confish/num_to_name#!/usr/bin/perl -w#read some numbers and output the match$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_name3、写一个程序,将一些字符串(在不同的行中)读入一个列表中。
然后按ASCII 顺序将它们输出来。
也就是说,当输入的字符串为fred, barney, wilma, betty,则输出为barney betty fred wilma。
分别在一行或不同的行将之输出。
------------------------------------/home/confish/sort_str#!/usr/bin/perl -w#read some strings and sort them inchomp(@str=sort<STDIN>);#@str=sort<STDIN>; will print them in diffrent linesprint @str,"\n";------------------------------------/home/confish/sort_str练习1、写一个名为&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";--------------------------------/home/confish/perl/subr#!/usr/bin/perl -w#a subroutine named total returns sum ofsub 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/subr2、利用上题的子程序,写一个程序计算从1 到1000 的数字的和。
--------------------------------/home/confish/perl/suber#!/usr/bin/perl -w#use the subroutine in last program to get the sum ofsub 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/suber3、额外的练习:写一个子程序,名为&above_average,将一列数字作为其参数,返回所有大于平均值的数字(提示:另外写一个子程序来计算平均值,总和除以数字的个数)。