Perl语言入门(第四版)习题标准答案

合集下载

第8章Perl语言在生物信息学中的应用[1]

第8章Perl语言在生物信息学中的应用[1]

数学运算符
字符串运算符
“.”,连接前后两个字符串。如: $DNA1=“ATCG”; $DNA2=“ACTG”


$DNA1.$DNA2=“ATCGACTG”;
其他运算符
例2:两段DNA序列的合并
example4-2.pl $DNA1 = 'ACGGGAGGACGGGAAAATTACTACGGCATTAGC'; $DNA2 = 'ATAGTGCCGTGAGAGTGATGTAGTA';
例1:在屏幕上打印一段DNA序列
example4-1.pl # 首先,我们将一段DNA序列存入变量$DNA中。 $DNA = 'ACGGGAGGACGGGAAAATTACTACGGCATTAGC';
# 然后,我们将 DNA序列打印到屏幕上 print $DNA;
# 最后,我们需要告诉程序,退出 (可省略)。 exit;
Perl编译器的安装
1. ActivePerl
/
/activeperl/downlo
ads/
2. ActivePerl的安装:非常简单,直接双击
程序图标。
ActivePerl下载
文件操作: 打开文件
$filename= “文件名 ”; open(Fh, $ filename); #文件 filename将被打开,它所包含
的东西通过名为filevar的文件句柄进入Perl程序; My @arrays=<Fh>; #将文件句柄传入到数组 arrays中;
print @protein; close PROTEINFILE;
在 unix系统中,如果一个文本文件的头两个字符是“#!”,那么它后面紧跟的就是 执行文件其余部分的程序名。在本例中,这个程序就被保存中文件D:\perl中。

Perl编程题-题目(1)

Perl编程题-题目(1)
编程题
1.输出文件行数。
2.输出FASTA、FASTQ序列文件包含的序列条数。
3.将文件从最后一行一直输出到第一行。
4.FASTA序列反向互补。
5.FASTQ序列转FASTA序列。
6.输入文件:
输出要求:为文件中第一列(基因ID)添加描述信息。
6.根据基因ID将不同文件中该基因的某一统计信息整合输出到一个文件中。
输入文件:
11.将一个文件,按照任意给定的数字(行数),等分成几分,注意最后一份可能数据不足给出的行数。比如:一个文件10行,输入3,给出4个文件:1-3,4-6,7-9,10
输入文件:
12.求一个序列的GC含量,和N的比率。
输入文件:
输入文件:
输出要求:将两个文件中都有的基因输 Nhomakorabea,并输出其在不同文件中的log2值。
7.给出一个read,求其平均质量值。
输入文件:
8.给出两组序列,找出其中的公共序列(按名称处理)。perl
输入文件:
9.给出一条fa序列,提取其中的10-150这部分位点的序列出来(起始位点为1)。
输入文件:
10.统计fa格式的序列长度。

汇编语言程序设计(第四版)第1~4章【课后答案】

汇编语言程序设计(第四版)第1~4章【课后答案】

汇编语言程序设计第四版【课后习题答案】第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〕汇编语言与高级语言相比有什么优缺点?〔解答〕汇编语言与高级语言相比的优点:由于汇编语言本质就是机器语言,它可以直接地、有效地控制计算机硬件,因而容易产生运行速度快,指令序列短小的高效目标程序,可以直接控制计算机硬件部件,可以编写在“时间”和“空间”两方面最有效的程序。

P、V原语操作的几道习题

P、V原语操作的几道习题

begin repeat
P(sb);
P(mutex); 将B产品入库; V(mutex); V(sa);
until false
end
until false
end
习题六
今有三个并发进程R,M,P,它们共享了一个 可循环使用的缓冲区B,缓冲区B共有N个单元。 进程R负责从输入设备读信息,每读一个字符后, 把它存放在缓冲区B的一个单元中;进程M负责 处理读入的字符,若发现读入的字符中有空格符 ,则把它改成“,”;进程P负责把处理后的字符 取出并打印输出。当缓冲区单元中的字符被进程 P取出后,则又可用来存放下一次读入的字符。 请用PV操作为同步机制写出它们能正确并发执 行的程序。 0 1 2 3 4 5 6 … … N-1
mf[0:N] = 0;
表示每个缓冲区是否为满的标志: ff[0:N ]=0;
表示每个缓冲区中的数据:B[0:N ]
PP: PR: PM: Begin Begin Begin P(full) 读一个字符ch; P(SM); P(mutex) P(mutex); P(avail) 找到一个 buffer j,它 找到一个buffer k ,它 P(mutex) (ff[k] true & 取出一个空的缓 (ff[j] true & mf[k] true;) 冲区i; mf[j] false;) Print B[k]; ff[i] true Modifiy B[j]; ff[k] false B[i]ch; mf[j] true mf[k] false; mf[i] false; V(mutex) V(mutex) V(mutex); V(full); V(avail); V(SM); End; End; End;

Perl练习题

Perl练习题

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.写一个程序,读入一串数字(一个数字一行),将和这些数字对应的人名(下面列出的)输出来。

【Lua程序设计第四版练习题答案】ch01Lua语言入门

【Lua程序设计第四版练习题答案】ch01Lua语言入门

【Lua程序设计第四版练习题答案】ch01Lua语⾔⼊门
联系1.1: 运⾏阶乘的⽰例并观察,如果输⼊负数,程序会出现什么问题?试着修改代码来解决问题。

-- 定义⼀个计算阶乘的函数
function fact (n)
if n == 0 then
return 1
else
return n * fact(n-1)
end
end
print("enter a number:")
a = io.read("*n") -- 读取⼀个数字
print(fact(a))
这是本章最开始提到的阶乘⽰例程序,通过实际的运⾏,我们了解到由于程序没有对负数形式进⾏校验的逻辑,因此程序会⼀直迭代递归下去,没有终⽌条件lua编译器报出堆栈溢出的错误才结束。

在进⾏修改之后的代码如下:
opefunction fact(n)
n = n or 0
if n < 0 then
error("Cannot calculate the factorial of a negative number")
elseif n == 0 then
return 1
else
return n * fact(n-1)
end
end
print("Enter a number: ")
a = io.read("*n")
print("Answer is: ", fact(a))
这⾥加⼊对输⼊负数的终⽌条件的判断,因此不会再出现堆栈溢出的错误。

汇编语言程序设计(第四版)第4章【课后答案】【精选】

汇编语言程序设计(第四版)第4章【课后答案】【精选】

汇编语言程序设计 第四版【课后习题答案】--囮裑為檤第4章 基本汇编语言程序设计〔习题4.1〕例题4.2如果要求算术右移8位,如何修改程序。

〔解答〕思路: 首先由最高位字节向次低位字节传送……次低位字节向最低位字节传送(共7次);再判最高位字节符号位,如为0,送00h 到最高位字节;如为1,送ffh 到最高位字节。

传送可参考例题4.2,不过应从第一号字节送第零号字节,……最高位字节向次低位字节传送;也可以用循环来完成: .model small .stack 256 .dataqvar dq 1234567887654321h .code .startup mov cx,7 mov si,1again: mov al, byte ptr qvar[si] mov byte ptr qvar[si-1],al inc siloop again test al,80h jz ezzmov bl,0ffh jmp done ezz: mov bl,0done: mov byte ptr qvar[7],bl .exit 0 end〔习题4.2〕例题4.2如果要求算术左移7位,如何用移位指令实现。

〔解答〕思路:可设计外循环体为8个字节左移一次,方法是:最低位字节算术左移一次, 次低位字节至最高位字节依次带 CF 位循环左移一次(内循环共8次),外循环体控制执行7次即可。

.model small .stack 256 .dataqvar dq 1234567887654321h4 11 201628.code.startupmov dx, 7 ;外循环次数mov ax, byte ptr qvar[0] ;最低位字节送axlpp: shl ax, 1 ;最低位字节左移一次,其d7移入CF 位 mov si, 1mov cx, 7 ;内循环次数again: rcl byte ptr qvar[si], 1 ;高位字节依次左移 P50 inc siloop again dec dx jnz lpp .exit 0 .end〔习题4.3〕将AX 寄存器中的16位数连续4位分成一组,共4组,然后把这4组数分别放在AL 、BL 、CL 和DL 寄存器中。

Perl语言学习练习及参考答案

Perl语言学习练习及参考答案

#题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,若是打印提示信息。

Perl面试题

Perl面试题

Perl语言面试题1.问题:什么是Perl?答案:Perl是一种解释型、功能丰富的编程语言,它具有动态的特性,能够处理文本和数据,非常适合用于文本处理、系统管理、网络编程、数据库操作等任务。

2.问题:Perl的基本语法是什么?答案:Perl的基本语法包括变量声明、控制结构(如if、while等)、循环结构(如for、foreach等)、函数定义和调用等。

3.问题:在Perl中如何声明变量?答案:在Perl中,可以使用$符号来声明变量,例如$var = 10;。

4.问题:在Perl中如何进行字符串操作?答案:Perl提供了丰富的字符串操作函数,如substr、index、length、tr 等,可以进行字符串的切割、查找、替换等操作。

5.问题:在Perl中如何进行数组操作?答案:Perl支持数组操作,可以使用@符号来声明数组,并使用push、pop、shift、unshift等函数进行数组的添加、删除、移除等操作。

6.问题:在Perl中如何进行文件操作?答案:Perl提供了丰富的文件操作函数,如open、readline、readlines 等,可以进行文件的打开、读取、写入等操作。

7.问题:在Perl中如何进行正则表达式匹配?答案:Perl支持正则表达式匹配,可以使用正则表达式函数如qr()和m()来进行模式匹配和替换操作。

8.问题:在Perl中如何使用循环结构?答案:Perl支持多种循环结构,如for、foreach、while等,可以根据需要选择合适的循环结构进行迭代操作。

9.问题:在Perl中如何定义和使用函数?答案:在Perl中,可以使用sub关键字定义函数,并使用&符号调用函数。

函数可以接受参数并返回值。

10.问题:请举一个使用Perl进行文本处理的例子?答案:例如,使用Perl提取一个文本文件中的特定行,可以根据行号或关键字进行提取。

具体实现可以使用open函数打开文件,并使用readline或readlines函数逐行读取文件内容,根据条件筛选出需要的行。

汇编语言程序设计(第四版)第5章【课后答案】

汇编语言程序设计(第四版)第5章【课后答案】

汇编语言程序设计第四版【课后习题答案】--囮裑為檤第五章高级汇编语言程序设计5.1 条件表达式中逻辑与“&&”表示两者都为真,整个条件才为真,对于程序段:.if (X==5) && (ax!=bx)inc ax.endif请用转移指令实现上述分支结构,并比较汇编程序生成的代码序列。

解:cmp X, 5je abcjmp doneabc: cmp ax, bxjne cdejmp donecde: inc axdone:....5.2条件表达式中逻辑与“||”表示两者之一为真,整个条件就为真,对于程序段:.if (X==5) || (ax!=bx)inc ax.endif请用转移指令实现上述分支结构,并比较汇编程序生成的代码序列。

解:cmp X, 5je abcjmp donecmp ax, bxjne abcjmp doneabc: inc axdone: ....5.3、对于程序段:.while ax!=10mov [bx],axinc bxinc bxinc ax请用处理器指令实现上述循环结构,并比较汇编程序生成的代码序列。

5.4、对于程序段:.repeatmov [bx],axinc bxinc bxinc ax请用处理器指令实现上述循环结构,并比较汇编程序生成的代码序列。

5.5、宏是如何定义、调用和展开的?解:宏定义采用一对伪指令实现,格式如下(其中方括号表示可选):宏名macro [形参表]宏定义体endm宏调用时,利用宏名带上实参即可,格式如下:宏名[实参表]5.6、宏定义中的形式参数有什么特点,它是如何进行形参和实参结合的?解:宏的参数功能非常强大,即可以没有参数,也可以带有一个或多个参数;而且参数的形式非常灵活,可以是常量、变量、存储单元、指令或它们的一部分,还可以是表达式等。

宏展开时的形参与实参结合是用位置匹配的实参对形参进行取代。

实参个数与形参个数不同时,多余的实参没有作用,缺少的实参则用空格代替。

《标准C语言基础教程(第四版)》习题答案(一)

《标准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 、确定期望的输出。

C++Primer中文版(第四版)题解整理

C++Primer中文版(第四版)题解整理

查看所用的编译器文档,了解它所用的文件命名规范。

编译并运行本节的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我们的程序使用了一条较长的输出语句。

Perl语言入门习题

Perl语言入门习题

Perl语言入门习题_第四章1.[12]写一个名为&total 的子程序,返回一列数字的和。

提示:子程序不应当有任何的 I/O 操作;它处理调用的参数,返回处理后的值给调用者。

结合下面的程序来练习,它检测此子程序是否正常工作。

第一组数组之和我25。

2.[5]利用上题的子程序,写一个程序计算从 1 到1000的数字的和3.[18]额外的练习:写一个子程序,名为&above_average,将一列数字作为其参数,返回所有大于平均值的数字(提示:另外写一个子程序来计算平均值,总和除以数字的个数)。

#!/usr/bin/perluse strict;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";sub total{my $sum;for(@_){$sum += $_;}return $sum;}#!/usr/bin/perluse strict;my @tmp = 1..1000;my $sum = &total(@tmp);print "total is : $sum.\n";sub total{my $sum;for(@_){$sum += $_;}return $sum;}#!/usr/bin/perluse strict;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";sub above_average{my $sum = &total(@_);my $average = $sum / @_;my @result;foreach(@_){if($_ > $average){push @result , $_ ;}}return @result;}sub total{my $sum;for(@_){$sum += $_;}return $sum;}----------------------------------------------------------------------------------Perl语言入门习题_第五章1.[7] 写一个程序,类似于cat,但保持输出的顺序关系。

C语言入门经典(第4版)课后练习参考答案

C语言入门经典(第4版)课后练习参考答案

目录目录 (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+习题答疑

Perl+习题答疑

Perl 习题(11)1 写一个程序,将一些字符串(不同的行)读入一个列表中,逆向输出它。

如果是从键盘输入的,那在Unix 系统中应当使用CTRL+D 表明end-of-file,在Windows 系统中使用CTRL+Z.(第3章习题)unless(@ARGV) {die "<input file> <output file>\n";}open(IN,"<$ARGV[0]") or die;my @line=<IN>;open(OUT,">$ARGV[1]") or die;foreach(reverse(@line)) {print OUT "$_";}2 写一个子程序,名为&above_average,将一列数字作为其参数,返回所有大于平均值的数字(提示:另外写一个子程序来计算平均值,总和除以数字的个数)。

利用下面的程序进行测试:(第4章习题)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";#!\usr\bin\perl -wuse strict;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";sub above_average{my @in=@_;my $average_value=&average(@in);my @out;foreach(@in) {if($_>$average_value){push @out,$_;}}return @out;}sub average {my @in=@_;my $sum=0;foreach(@in){$sum+=$_;}return $sum;}3 写一个程序,要求用户在不同的行中输入一些字符串,将此字符串打印出来,规则是:每一条占20 个字符宽度,右对齐。

PERL解题

PERL解题

Note that atan2(0, 0) is not well-defined. 2. 单元 1 写一个程序,计算半径为 12.5 的圆的周长。圆周长等于 2л(л约为 3.141592654)乘以 半径。答案是 78.5。 ex2_1:
1
#! /usr/bin/perl $c=2*3.141592654*12.5; print $c."\n";
3 写一个程序,将一些字符串(在不同的行中)读入一个列表中。然后按 ASCII 顺序将它们 输出来。也就是说,当输入的字符串 为 fred,barney,wilma,betty,则输出为 barney betty fred wilma。分别在一行或不同 的行将之输出。 ex3_3: #! /usr/bin/perl @strings=<STDIN>; chomp(@cstring=@string); @cstring1=sort @cstring; print sort @strings; print "@cstring1"; 4. 单元 1 写一个名为&total 的子程序,返回一列数字的和。提示:子程序不应当有任何的 I/O 操 作;它处理调用的参数,返回处理后的值给调用者。结合下面的程序来练习,它检测此子程 序是否正常工作。第一组数组之和为 25。 ex4_1: #! /usr/bin/perl 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:";

语言学教程第四版答案

语言学教程第四版答案

语言学教程第四版答案【篇一:《语言学教程》测试题答案】xt>i.1~5 b a c c c6~10 b a c a cii.11~15 f f t f f 16~20 f f f f fiii.21. verbal 22. productivity / creativity 23. metalingual function24. yo-he-ho25. scientific26. descriptive 27. speech 28. diachronic linguistic29. langue 30. competenceiv.31. design feature: it refers to the defining properties of human language that tell the differencebetween human language and any system of animal communication.32. displacement: it means that human languages enable their users to symbolize objects, events andconcepts, which are not present (in time and space) at the moment of communication.33. competence: it is an essential part of performance. it is the speaker’s knowledge of his or herlanguage; that is, of its sound structure, its words, and its grammatical rules. competence is, in a way, an encyclopedia of language. moreover, the knowledge involved in competence is generally unconscious.a transformational-generative grammar is a model of competence.34. synchronic linguistics: it refers to the study of a language at a given point in time. the timestudied may be either the present or a particular point in the past; synchronic analyses can also be made of dead languages, such as latin. synchronic linguistics is contrasted with diachronic linguistics, the study of a language over a period of time.v.35. duality makes our language productive. a large number of different units can be formed out of asmall number of elements – for instance, tens of thousands of words out of a small set of sounds,around 48 in the case of the english language. and out of the huge number of words, there can beastronomical number of possible sentences and phrases, which in turn can combine to formunlimited number of texts. most animal communication systems do not have this design feature ofhuman language.if language has no such design feature, then it will be like animal communicational system whichwill be highly limited. it cannot produce a very large number of sound combinations, e.g. words,which are distinct in meaning.36. it is difficult to define language, as it is such a general term that covers too many things. thus,definitions for it all have their own special emphasis, and are not totally free from limitations.vi.37. it should be guided by the four principles of science: exhaustiveness, consistency, economy andobjectivity and follow the scientific procedure: form hypothesis – collect data – check against theobservable facts – come to a conclusion.第二章:语音参考答案i1~5 a c d a a6~10 d b a b bii.11~15 t t t f f 16~20 t t t f fiii.21. voiced, voiceless, voiced 22. friction23. tongue 24. height 25. obstruction26. minimal pairs27. diphthongs 28. co-articulation29. phonemes30. air streamiv.31. sound assimilation: speech sounds seldom occur in isolation. in connected speech, under the influenceof their neighbors, are replaced by other sounds. sometimes two neighboring sounds influence eachother and are replaced by a third sound which is different from both original sounds. this process is called sound assimilation.32. suprasegmental feature: the phonetic features that occur above the level of the segments are calledsuprasegmental features; these are the phonological properties of such units as the syllable, the word, and the sentence. the main suprasegmental ones includes stress, intonation, and tone.33. complementary distribution: the different allophones of the same phoneme never occur in the samephonetic context. when two or more allophones of one phoneme never occur in the same linguistic environment they are said to be in complementary distribution.34. distinctive features: it refers to the features that can distinguish one phoneme from another. if we cangroup the phonemes into two categories: one with this feature and the other without, this feature is called a distinctive feature. v.35. acoustic phonetics deals with the transmission of speech sounds through the air. when a speech soundis produced it causes minor air disturbances (sound waves). various instruments are used to measure the characteristics of these sound waves.36. when the vocal cords are spread apart, the air from the lungs passes between them unimpeded. soundsproduced in this way are described as voiceless; consonants [p, s, t] are produced in this way. but when the vocal cords are drawn together, the air from the lungs repeatedly pushes them apart as it passes through, creating a vibration effect. sounds produced in this way are described as voiced. [b, z, d] are voiced consonants.vi. 37.omit.第三章:词汇参考答案i1~5 a a c b b6~10 b c a d bii. 11~15 f t f t t16~20 f t f f fiii.21. initialism, acronym 22. vocabulary 23. solid, hyphenated, open 24. morpheme25. close, open 26. back-formation 27. conversion 28. morpheme29. derivative, compound 30. affix, bound rootiv.31. blending: it is a process of word-formation in which a new word is formed by combining themeanings and sounds of two words, one of which is not in its full form or both of which are not in their full forms, like newscast (news + broadcast), brunch (breakfast + lunch)32. allomorph: it is any of the variant forms of a morpheme as conditioned by position or adjoiningsounds.33. close-class word: it is a word whose membership is fixed or limited. pronouns, prepositions,conjunctions, articles, etc. are all closed-class words.34. morphological rule: it is the rule that governs which affix can be added to what type of base to forma new word, e.g. –ly can be added to a noun to form an adjective.vi .37. (1) c (2) a (3) e (4) d (5) b第四章:句法参考答案i1~5 d c d d d 6~10 a d d b aii. 11~15 t t t t f16~20 f t f t tiii.21. simple 22. sentence 23. subject24. predicate25. complex 26. embedded 27. open28. adjacency29. parameters 30. caseiv.31. syntax: syntax refers to the rules governing the way words are combined to form sentences in alanguage, or simply, the study of the formation of sentences.32. ic analysis: immediate constituent analysis, ic analysis for short, refers to the analysis of a sentence interms of its immediate constituents – word groups (phrases), which are in turn analyzed into theimmediate constituents of their own, and the process goes on until the ultimate sake of convenience.33. hierarchical structure: it is the sentence structure that groups words into structural constituents andshows the syntactic category of each structural constituent, such as np, vp and pp.34. trace theory: after the movement of an element in a sentence there will be a trace left in the originalposition. this is the notion trace in t-g grammar. it’s suggested that if we have the notion trace, all the necessary information for semantic interpretation may come from the surface structure. e.g. thepassive dams are built by beavers. differs from the active beavers built dams. in implying that all dams are built by beavers. if we add a trace element represented by the letter t after built in the passive as dams are built t by beavers, then the deep structure information that the word dams was originally the object of built is also captured by the surface structure. trace theory proves to be not only theoretically significant but also empirically valid.v.35. an endocentric construction is one whose distribution is functionally equivalent, or approachingequivalence, to one of its constituents, which serves as the center, or head, of the whole. a typicalexample is the three small children with children as its head. the exocentric construction, opposite to the first type, is defined negatively as a construction whose distribution is not functionally equivalent to any of its constituents. prepositional phrasal like on the shelf are typical examples of this type.36. (1) more | beautiful flowers(2) more beautiful | flowers第五章:意义参考答案i1~5 a b d d b 6~10 c a c d aii. 11~15 f f t f t 16~20 t f t t tiii.21. semantics 22. direct 23. reference 24. synonyms25.homophones26. relational27. componential 28. selectional 29. argument 30. namingiv.31. entailment: it is basically a semantic relation (or logical implication), and it can be clarified withthe following sentences:a. tom divorced jane.b. jane was tom’s wife.in terms of truth value, the following relationships exist between these two sentences: when a is true,b must be also true; when b is false, a must also be false. when b is true, a may be true or false.therefore we can say a entails b.32. proposition: it is the result of the abstraction of sentences, which are descriptions of states of affairs andwhich some writers see as a basic element of sentence meaning. for example, the two sentences“caesar invaded gaul” and “gaul was invaded by caesar” hol d the same proposition.33. compositional analysis: it defines the meaning of a lexical element in terms of semantic components, orsemantic features. for example, the meaning of the word boy may be analyzed into three components: human, young and male. similarly girl may be analyzed into human, young andfemale.34. reference: it is what a linguistic form refers to in the real world; it is a matter of the relationshipbetween the form and the reality.v.35. hyponymy, metonymy or part-whole relationship36. (omit.)vi.37. (1)the (a) words and (b) words are male.the (a) words are human, while the (b) words are non-human.(2)the (a) words and (b) words are inanimate.the (a) words are instrumental, while the (b) words are edible.(3)the (a) words and (b) words are worldly or conceptual.the (a) words are material, while the (b) words are spiritual.第七章:语言、文化和社会参考答案i1~5 b c a a c 6~10 d a c a dii. 11~15 f t f f f 16~20 t f t f fiii.21. community22. variety 23. dialectal 24.planning25.sociolects26. stylistic 27. official28. superposed29. vernacular 30. inflectionaliv.31. lingua franca: a lingua franca is a variety of language that serves as a common speech for socialcontact among groups of people who speaks different native languages or dialects.32. regional dialect: regional dialect, also social or class dialect, is a speech variety spoken by themembers of a particular group or stratum of a speech community.33. register: register, also situational dialect, refers to the language variety appropriate for use in particularspeech situations on which degrees of formality depends.34. sociolinguistics: defined in its broadest way, sociolinguistics, a subdiscipline of linguistics, is the studyof language in relation to society. it is concerned with language variation, language use, the impact of extra-linguistic factors on language use, etc.v. 35. american english is not superior to african english. as different branches of english, africanenglish and american english are equal. similar as they are, they are influenced by their respective cultural context and thus form respective systems of pronunciation, words and even grammar.36. in china, chinese has a more strict and complex relationship system. so in chinese there are a lot morekinship words than in english.vi. 37. (omit.)第八章:语言的使用参考答案i1~5 d b c b a 6~10 c b c a dii. 11~15 f t t f f 16~20 f f f t tiii.21. context22. utterance 23. abstract 24. constatives 25. performatives26. locutionary 27. illocutionary28. commissive29. expressive30. quantityiv.31. conversational implicature: in our daily life, speakers and listeners involved in conversation aregenerally cooperating with each other. in other words, when people are talking with each other, they must try to conversesmoothly and successfully. in accepting speakers’ presuppositions, listenershave to assume that a speaker is not trying to mislead them. this sense of cooperation is simply one in which people having a conversation are not normally assumed to be trying to confuse, trick, orwithhold relevant information from one another. however, in real communication, the intention of the speaker is often not the literal meaning of what he or she says. the real intention implied in the words is called conversational implicature.32. performative: in speech act theory an utterance which performs an act, such as watch out (= a warning).33. locutionary act: a locutionary act is the saying of something which is meaningful and can beunderstood.34. horn’s q-principle: (1) make your contribution sufficient (cf. quantity); (2) say as much as you can(given r).v.35. pragmatics is the study of the use of language in communication, particularly the relationshipsbetween sentences and the contexts and situations in which they are used. pragmatics includes the study of(1) how the interpretation and use of utterances depends on knowledge of the real world;(2) how speakers use and understand speech acts;(3) how the structure of sentences is influenced by the relationship between the speaker and thehearer.pragmatics is sometimes contrasted with semantics, which deals with meaning without referenceto the users and communicative functions of sentences.36. yes, b is cooperative. on the face of it, b’s statement is not an answer to a’s question. b doesn’t say“when.” however, a will immediately interpret the s tatement as meaning “i don’t know” or “i am not sure.” just assume that b is being “relevant” and “informative.” given that b’s answer contains relevant information, a can work out that “an accident further up the road” conventionally involves “trafficja m,” and “traffic jam” preludes “bus coming.” thus, b’s answer is not simply a statement of “when the bus comes”; it contains an implicature concerning “when the bus comes.”vi.37. it occurs before and / or after a word, a phrase or even a longer utterance or a text. the context oftenhelps in understanding the particular meaning of the word, phrase, etc.the context may also be the broader social situation in which a linguistic item is used.(1)a. a mild criticism of someone who should have cleaned the room.b. in a language class where a student made a mistake, for he intended to say “tidy.”c. the room was wanted for a meeting. (2)a. a mild way to express disagreement with someone who has complimented on a lady’sappearance. b. a regret that the customer had not taken the dress. c. that she wore a red shirt was not in agreement with the custom on the occasion.第十二章:现代语言学理论与流派参考答案i1~5 b a c a a 6~10 a b d c cii. 11~15 f f t t f 16~20 f t t t fiii.21. synchronic22. phonetics23. j. r. firth 24. systemic25. sociologically26. distribution27. bloomfieldian 28. descriptivism29. innateness30. hypothesis-maker iv.31. fsp: it stands for functional sentence perspective. it is a theory of linguistic analysis which refers to ananalysis of utterances (or texts) in terms of the information they contain.32. cohesion: the cohesion shows whether a certain tagmeme is dominating other tagmemes or isdominated by others.33. lad: lad, that is language acquisition device, is posited by chomsky in the 1960s as a deviceeffectively present in the minds of children by which a grammar of their native language is constructed.34. case grammar: it is an approach that stresses the relationship of elements in a sentence. it is a type ofgenerative grammar developed by c. j. fillmore in the late1960s.v. vi. omit.【篇二:语言学教程(胡壮麟版)综合测试题含标准答案】 class=txt>英语语言学试卷(一)第一部分选择题i. directions: read each of the following statements carefully. decide which one of the fourchoices best completes the statement and put the letter a, b, cor d in the brackets.(2%x10=20%)1.saussure’s distinction and chomsky’s are very similar, but they differ in that ____________. a.saussure took a sociological view of language while chomsky took a psychological point of viewb. saussure took a psychological view of language while chomsky took a sociological point ofviewc. saussure took a pragmatic view of language while chomsky took a semantic point of viewd. saussure took a structural view of language while chomsky took a pragmatic point of view2. language is a system of ____________ vocal symbols used for human communication. a.unnatural b. artificialc. superficiald. arbitrary3. we are born with the ability to acquire language,_______________.a. and the details of any language system are genetically transmittedb. therefore, we needn’t learn the details of our mother tonguec. but the details of language have to be learnt.d. and the details are acquired by instinct4. a(n)________ is a phonological unit of distinctive value. it isa collection of distinctivephonetic features. a. phone b. allophonec. phonemed. sound5. the morpheme –ed in the word “worked” is a(n) __________ morpheme. a. derivationalb. inflectionalc. freed. word-forming6. wh-movement is __________ in english which changes a sentence from affirmative tointerrogative. a. obligatoryb. optionalc. selectionald. arbitrary7. naming theory, one of the oldest notions concerning meaning, was proposed by_____________. a. griceb. platoc. saussured. ogden and richards8. “john married a blond heiress.”__________ “john married a blond.” a. is synonymous withb. is inconsistent withc. entailsd. presupposes9. in semantic analysis of a sentence, the basic unit is called ____________, which is theabstraction of the meaning of a sentence. a. utterance b. referencec. predicationd. morpheme10. in austin’s speech act theory, ___________ is the act of expressing the speaker’s intention; itis the act performed in saying something. a. a perlocutionary act b. alocutionary actc. a constative actd. an illocutionary act第二部分非选择题ii. directions: fill in the blank in each of the following statements with one word, the first letter ofwhich is already given as a clue. note that you are to fill in one word only, and you are notallowed to change the letter given. (1%x10=10%)11. p___________ relates the study of language to psychology. it aims to answer such questionsas how the human mind works when people use language.12. a d_________ study of language is a historical study; it studies the historical development oflanguage over a period of time.13. language is a system, which consists of two sets of structures, or two levels. at the lower level,there is a structure of meaningless sounds, which can be combined into a large number ofmeaningful units at the higher level. this design feature is called d___________.14. the articulatory apparatus of a human being is containedin three important areas: thepharyngeal cavity, the o_________ cavity and the nasal cavity.15. the localization of cognitive and perceptual functions in a particular hemisphere of the brain iscalled l_____________.16. s_____________ features such as stress, tone and intonation can influence the interpretationof meaning.17. phrase structure rules can generate an infinite number of sentences, and sentences with infinitelength, due to their r_________ properties.18. h__________ refers to the phenomenon that words having different meanings are identical in sound or spelling, or in both.19. some important missions of historical linguists are to identify and classify families of related languages in a genealogical family tree, and to reconstruct the p____________, the original form of a language family that has ceased to exist.20. in sociolinguistics, speakers are treated as members of social groups. the social group isolated for any given study is called the speech c___________.iii. directions: judge whether each of the following statements is true or false. put a t for true or f for false in the brackets in front of each statement. (2%x10=20%)( ) 21. linguists believe that whatever occurs in the language people use should be described and analyzed in their investigation.( ) 22. language is arbitrary in the sense that there is no intrinsic connection between words and what these words actually refer to.( ) 23. the conclusions we reach about the phonology of one language can be generalized into the study of another language.( ) 24. the meaning-distinctive function of the tone is especially important in english because english, unlike chinese, is a typical tone language.( ) 25. the syntactic rules of any language are finite in number, and yet there is no limit to the number of sentences native speakers of that language are able to produce and comprehend.( ) 26. when we think of a concept, we actually try to see the image of something in our mind’s eye every time we come across a linguistic symbol.( ) 27. all utterances can be restored to complete sentences. for example, “good morning!” can be restored to “i wish you a good morning.”( ) 28. two people who are born and brought up in the same town and speak the same regional dialect may speak differently because of a number of social factors.( ) 29. black english is linguistically inferior to standard english because black english is not as systematic as standard english.( ) 30. any child who is capable of acquiring some particular human language is capable of acquiring any human language spontaneously and effortlessly.iv. directions: explain the following terms. (3%x10=30%)31. parole:32. broad transcription:33.allophones:34.phrase structure rules:35.context36.historical linguistics:37.standard language:38.linguistic taboo:39.acculturation:40.care-taker speech:v. answer the following questions. (10%x2=20%)41. enumerate three causes that lead to the systematic occurrence of errors in second language acquisition and give your examples.42. english has undergone tremendous changes since its anglo-saxon days. identify the major periods in its historicaldevelopment and name major historical events that led to the transition from one period to the next.英语语言学试卷答案(一)第一部分选择题i. directions: read each of the following statements carefully. decide which one of the four choices best completes the statement and put the letter a, b, c or d in the brackets.(2%x10=20%)1. a2. d3. c4. c5.b6. a7. b8. c9. c 10. d第二部分非选择题ii. directions: fill in the blank in each of the following statements with one word, the first letter of which is already given as a clue. note that you are to fill in one word only, and you are not【篇三:语言学教程第四版练习第一章】inguisticsi. mark the choice that best completes the statement.1.all languages’ have three major components: a sound system ,a system of___and a system of semantics.a. morphologyb. lexicogrammarc. syntaxd. meaning2.which of the following words is entirely arbitrary?3.the function of the sentence water boils at 100 degrees centigrade is ___.a.interpersonalb.emotivermatived.performative4.in chinese when someone breaks a bowl or a plate the host or the people present are likely to say 碎碎(岁岁)平安as a means of controlling the forces which they believe might affect their lives. which function does it perform?a.interpersonalb.emotivermatived.performative5.which of the following property of language enables language users to overcome the barriers caused by time and place of speaking (due to this feature of language, speakers of a language are free to talk about anything in any situation)?a. transferabilityb. dualityc. displacementd. arbitrariness6. what language function does the following conversation play?(the two chatters just met and were starting their conversation by the following dialogue.)a:a nice day, isn’t it?b : right! i really enjoy the sunlight.a. emotiveb. phaticc. performatived. interpersonal7.------- refers to the actual realization of the ideal language user’s knowledge of the rules of his language in utterances.8.when a dog is barking, you assume it is barking for something or at someone that exists here and now. it couldn’t be sorrowful for some lost love or lost bone. this indicates that dog’s language does not have the feature of --------- .a. referenceb. productivityc. displacementd.duality9.--------- answers such questions as we as infants acquire our first language.a. psycholinguisticsb. anthropological linguisticsc. sociolinguisticsd. applied linguistics10.-------- deals with the study of dialects in different social classes in a particular region.a. linguistic theoryb. practical linguisticsc. sociolinguisticsd. comparative linguisticsii. mark the following statements with “t” if they are true or “f” if they are false.(10%)1. the widely accepted meaning of arbitrariness was discussed by chomsky first.2. for learners of a foreign language, it is arbitrariness that is more worth noticing than its conventionality.3. displacement benefits human beings by giving them the power to handlegeneralizations and abstractions.4. for jakobson and the prague school structuralists, the purpose of communication is to refer.5. interpersonal function is also called ideational function in the framework of functional grammar.6. emotive function is also discussed under the term expressive function.7. the relationship between competence and performance in chomsky’s theory is that between a language community and an individual language user.8.a study of the features of the english used in shakespeare’s time is an example of the diachronic study of language.9.articulatory phonetics investigates the properties of the sound waves.10.the nature of linguistics as a science determines its preoccupation with prescription instead of description.iii.fill in each of the following blanks with an appropriate word. the first letter of the word is already given(10%)1. nowadays, two kinds of research methods co-exist in linguistic studies, namely,qualitative and q__________ research approaches.2. in any language words can be used in new ways to mean new things and can becombined into innumerable sentences based on limited rules. this feature is usually termed as p__________.nguage has many functions. we can use language to talk about language. this function is m__________function.4.the claim that language originated by primitive man involuntary making vocal noises while performing heavy work has been called the y_theory.5.p________ is often said to be concerned with the organization of speech within specific language, or with the systems and patterns of sounds that occur in particular language.6.modern linguistics is d_ in the sense that linguist tires to discover what language is rather than lay down some rules for people to observe.7.one general principle of linguistics analysis is the primacy of s___________over writing.8.the description of a language as it changes through time is a d___________ linguistic study.9.saussure put forward the concept l__________ to refer to the abstract linguistic system shared by all members of a speech community.10.linguistic potential is similar to saussure’ s langue and chomsky’ s c__________.iv. explain the following concepts or theories.1.design features2.displacement。

算法第四版习题答案解析

算法第四版习题答案解析

1.1.1 给出以下表达式的值:a. ( 0 + 15 ) / 2b. 2.0e-6 * 100000000.1c. true && false || true && true答案:a.7,b.200.0000002 c.ture1.1.2 给出以下表达式的类型和值:a. (1 + 2.236)/2b. 1 + 2 + 3 + 4.0c. 4.1 >= 4d. 1 + 2 + "3"答案:a.1.618 b. 10.0 c.true d.331.1.3 编写一个程序,从命令行得到三个整数参数。

如果它们都相等则打印equal,否则打印not equal。

public class TestUqual{public static void main(String[] args){int a,b,c;a=b=c=0;StdOut.println("Please enter three numbers");a =StdIn.readInt();b=StdIn.readInt();c=StdIn.readInt();if(equals(a,b,c)==1){StdOut.print("equal");}else{StdOut.print("not equal");}}public static int equals(int a ,int b , int c){if(a==b&&b==c){return 1;}else{return 0;}}}1.1.4 下列语句各有什么问题(如果有的话)?a. if (a > b) then c = 0;b. if a > b { c = 0; }c. if (a > b) c = 0;d. if (a > b) c = 0 else b = 0;答案:a. if (a > b) c = 0; b. if (a > b) { c = 0; }1.1.5 编写一段程序,如果double 类型的变量x 和y 都严格位于0 和1 之间则打印true,否则打印false。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

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 个数字(不在同一行)。

输出为这两个数的积。

-----------------------/home/confish/perl/product#!/usr/bin/perl -w#print the two number's product#confish@ubuntu7.10print"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's input#confish@ubuntu7.10print"enter a string and a number:\n";$str=<STDIN>;chomp($num=<STDIN>);print ${str}x$num;-----------------------/home/confish/perl/printer3.9 练习1、写一个程序,将一些字符串(不同的行)读入一个列表中,逆向输出它。

如果是从键盘输入的,那在Unix 系统中应当使用CTRL+D 表明end-of-file,在Windows 系统中使用CTRL+Z.------------------------------------/home/confish/reprint#!/usr/bin/perl -w#read some input and print them in reverse sequence#confish@ubuntu7.10print "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 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_name3、写一个程序,将一些字符串(在不同的行中)读入一个列表中。

然后按ASCII 顺序将它们输出来。

也就是说,当输入的字符串为fred, barney, wilma, betty,则输出为barney betty fred wilma。

分别在一行或不同的行将之输出。

------------------------------------/home/confish/sort_str#!/usr/bin/perl -w#read some strings and sort them in ASCII#confish@ubuntu7.10chomp(@str=sort<STDIN>);#@str=sort<STDIN>; will print them in diffrent linesprint @str,"\n";------------------------------------/home/confish/sort_str4.11练习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 of numbers#confish@ubuntu7.10sub 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 的数字的和。

相关文档
最新文档