1 / 17

记录与文件类型

记录与文件类型. 例如: 有关一个学生的数据包含下列项目: 学号 字符串类型 姓名 字符串类型 年龄 整型 性别 字符串类型 成绩 实型数组. 一.记录类型的定义. 例: type studata=record num:string[6]; name:string[8]; sex:boolean; s:array[1..5] of real; end; var student:studata; students:array[1..10] of studata;.

maya
Télécharger la présentation

记录与文件类型

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 记录与文件类型

  2. 例如: 有关一个学生的数据包含下列项目: 学号 字符串类型 姓名 字符串类型 年龄 整型 性别 字符串类型 成绩 实型数组

  3. 一.记录类型的定义 例:typestudata=recordnum:string[6];name:string[8];sex:boolean;s:array[1..5] of real;end;varstudent:studata;students:array[1..10] of studata; type 类型标识符=record<域名1>:<类型1>;<域名2>:<类型2> ;...<域名n>:<类型n> ;end; 说明(1)域名也称域变量标识符,应符合标识符的语法规则。在同一个记录中类型中,各个域不能取相同的名,但在不同的记录类型中,两个类型中的域名可以相同。

  4. 一.记录类型的定义 例:type date=record year:1900..1999; month:1..12; day:1..31; end; var x:date; 可以合并为: var x:record year:1900..1999; month:1..12; day:1..31; end; type 类型标识符=record<域名1>:<类型1>;<域名2>:<类型2> ;...<域名n>:<类型n> ;end; 说明(2)记录类型的定义和记录变量可以合并为一个定义。

  5. 二.记录的运用: 1、对记录中各个域的引用,要写出记录名和域名,如:student.num 2、开域语句:with。with 记录名 do 语句; 或with 记录名1,记录名2,... do 语句;  注意:1. 在do后面语句中使用的记录的域时,只要简单地写出域名就可以了, 域名前的记录变量和"."均可省略。2. 在关键字with后面,语句可以是一个简单语句,了可以是一个复合语句。3. 虽然在with后可以有多个记录变量名,但一般在with后只使用一个记录变量名。

  6. 例1:利用记录类型将N个数由大到小排序,输出排序结果并显示每个数原来的位置序号。例1:利用记录类型将N个数由大到小排序,输出排序结果并显示每个数原来的位置序号。 Program ex01; type dd=record ii:integer; id:integer; var a:array[1..100] of dd; I,j,k,n:integer; tt:dd; begin write(‘please input number of elements:’) read(n); writeln(‘input elements:’); for i=1 to n do begin read(a[i].ii); a[i].id:=i; for j:=1 to i-1 do if a[j].ii<a[i].ii then begin t:=a[i]; for k:=i-1 downto j do a[k+1]:=a[k]; a[j]=:t; end; end; for i:=1 to n do begin write(a[i].ii:5, ‘(‘,a[I].id:2’)’); if I mod 10 =0 then writeln; end; readln;writeln; end.

  7. 三.文件 文件是一种构造型的数据类型。在程序中都需要产生一些输出,也需要接受若干个输入。这些输入、输出实际上是用文件的方法来实现的,在Pascal中用标准文件“input”和“output”来实现,它们分别对应标准输入设备和标准输出设备(可省略不写)这也就是一些程序的程序书写如下的原因了:program ex(input,output);...  但有时大量数据的读入和输出都是来是磁盘文件,这就要求我们必须熟练掌握对磁盘文件的操作。

  8. 三.文件 1、文件的特点:  (1)顺序性 (2)永久性 (3)容量大 2、文件的分类 (1)按文件的结构形式:文本文件、类型文件、无类型文件。     (2)按文件的存取方式:顺序存取文件、随机存取文件。

  9. 三.文件 3、文件操作的常用函数和过程 (1)适用于所有文件类型的标准过程和函数

  10. 三.文件 3、文件操作的常用函数和过程 (2)只适合于文本文件的标准过程和函数

  11. 三.文件 3、文件操作的常用函数和过程 (3)只适合于文本文件的标准过程和函数

  12. 三.文件 4、文件操作的一般步骤 (1)在使用文件前,必须对文件类型和变量进行说明; (2)把磁盘上的实际文件(外部文件)和Pascal程序当中的文件(内部文件)建立关联; (3)打开文件,将文件指针指向开始位置,为文件读写作准备; (4)对文件进行读、写操作; (5)在使用完文件后,一定要记住关闭文件,确保文件的完整性和可靠性,否则会引起文件处理错误。

  13. 三.文件 5、文本文件的操作步骤 (1)文本文件的变量在使用前必须说明其类型为text; 如:var f1,f2:text; (2)使用文本文件前,要先调用assign过程,把外部文件名赋予文本文件变量; 如:assign(filevar,filename); (3)当将数据写入(输出)到文件时,应先调用rewrite或append过程打开该文件,再用write或writeln将实际数据写入到该文件中; 如:rewirte(filevar); append(filevar); write(filevar,var1,var2,…,varn); writeln(filevar,var1,var2,…,varn); 当需要从文件中读取数据(输入)到内存时,应先调用reset过程打开该文件,再用read或readln将数据读入到内存变量中,且只能从文件的开头读数据; 如:reset(filevar); read(filevar,var1,var2,…,varn); readln(filevar,var1,var2,…,varn);

  14. 三.文件 5、文本文件的操作步骤 (4)对文件的输入、输出操作必须以行为单位进行,读写完毕要用close 命令关闭文件。   如:close(filevar); 6、行结束和文件结束函数 (1)Eoln函数 (2)Eof函数

  15. 例2:从键盘输入一段正文,将它复制到指定的磁盘文件中,然后再在显示器上输出。例2:从键盘输入一段正文,将它复制到指定的磁盘文件中,然后再在显示器上输出。 Close(file1); writeln; reset(file1); while not eof(file1) do begin while not eoln(file1) do begin read(file1,ch); write(ch:3); end; readln; writeln; end; close(file1); End. program px2(input,output) Var ch:char; str1:string[15]; file1:text; Begin write(‘please input a file name:’); readln (str1); assign(file1,str1); rewrite(file1); while not eof do begin while not eoln do begin read(ch); write(file1,ch); end; readln; writeln(file1); end;

  16. 例3:将文本文件input.txt中的内容复制到一个新的文本文件output.txt中。例3:将文本文件input.txt中的内容复制到一个新的文本文件output.txt中。 Program px3(input,output); Var f,g:text; ch:char; Begin assign(f,’input.txt’); assign(g,’output.txt’); reset(f); rewrite(g); while not eof(f) do begin while not eoln(f) do begin read(f,ch); write(g,ch); end; readln(f); writeln(g); end; close(f); close(g); End.

  17. 练习:输入5位学生的数据记录(包含学号、姓名、性别、年龄、成绩五个域),按成绩从高到低排序输出。练习:输入5位学生的数据记录(包含学号、姓名、性别、年龄、成绩五个域),按成绩从高到低排序输出。 Program ex; type studata=record       num:string[6]; name:string[8]; sex:string[1]; age:integer;        score:real;        end; Var s:array[1..5] of studata; t:studata; i,j,k:integer; Begin for i:=1 to 5 do begin readln(s[i].num); readln(s[i].name); readln(s[i].sex); readln(s[i].age); readln(s[i].score); for j:=1 to i-1 do if s[j].score<s[i].score then begin t: =s[i]; for k:=i-1 downto j do s[k+1]:=s[k]; s[j]:=t; end; end; for i:=1 to 5 do begin write(s [i].num); write(s [i].name); write(s [i].sex); write(s [i].age); write(s[i].score); writeln; end; End.

More Related