当前位置: 首页 > 文档资料 > Pascal 中文教程 >

记录( Records)

优质
小牛编辑
129浏览
2023-12-01

Pascal数组允许您定义可以包含多个相同类型数据项的变量类型,但是记录是Pascal中可用的另一个用户定义数据类型,它允许您组合不同类型的数据项。

记录包含不同的字段。 假设您想要在库中跟踪您的书籍,您可能希望跟踪每本书的以下属性 -

  • Title
  • Author
  • Subject
  • 书名

定义记录

要定义记录类型,可以使用类型声明语句。 记录类型定义为 -

type
record-name = record
   field-1: field-type1;
   field-2: field-type2;
   ...
   field-n: field-typen;
end;

以下是您宣布图书记录的方式 -

type 
Books = record
   title: packed array [1..50] of char;
   author: packed array [1..50] of char;
   subject: packed array [1..100] of char;
   book_id: integer;
end;

记录变量以通常的方式定义

var
   r1, r2, ... : record-name;

或者,您可以直接将记录类型变量定义为 -

var
Books : record
   title: packed array [1..50] of char;
   author: packed array [1..50] of char;
   subject: packed array [1..100] of char;
   book_id: integer;
end;

访问记录的字段

要访问记录的任何字段,我们使用成员访问运算符(。)。 成员访问运算符被编码为记录变量名称和我们希望访问的字段之间的句点。 以下是解释结构用法的示例 -

program exRecords;
type
Books = record
   title: packed array [1..50] of char;
   author: packed array [1..50] of char;
   subject: packed array [1..100] of char;
   book_id: longint;
end;
var
   Book1, Book2: Books; (* Declare Book1 and Book2 of type Books *)
begin
   (* book 1 specification *)
   Book1.title  := 'C Programming';
   Book1.author := 'Nuha Ali '; 
   Book1.subject := 'C Programming Tutorial';
   Book1.book_id := 6495407;
   (* book 2 specification *)
   Book2.title := 'Telecom Billing';
   Book2.author := 'Zara Ali';
   Book2.subject := 'Telecom Billing Tutorial';
   Book2.book_id := 6495700;
   (* print Book1 info *)
   writeln ('Book 1 title : ', Book1.title);
   writeln('Book 1 author : ', Book1.author);
   writeln( 'Book 1 subject : ', Book1.subject);
   writeln( 'Book 1 book_id : ', Book1.book_id);
   writeln; 
   (* print Book2 info *)
   writeln ('Book 2 title : ', Book2.title);
   writeln('Book 2 author : ', Book2.author);
   writeln( 'Book 2 subject : ', Book2.subject);
   writeln( 'Book 2 book_id : ', Book2.book_id);
end.

编译并执行上述代码时,会产生以下结果 -

Book 1 title : C Programming
Book 1 author : Nuha Ali
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Zara Ali
Book 2 subject : Telecom Billing Tutorial
Book 2 book_id : 6495700

记录为子程序参数

您可以将记录作为子程序参数传递,与传递任何其他变量或指针的方式非常相似。 您可以按照上面示例中访问的方式访问记录字段 -

program exRecords;
type
Books = record
   title: packed array [1..50] of char;
   author: packed array [1..50] of char;
   subject: packed array [1..100] of char;
   book_id: longint;
end;
var
   Book1, Book2: Books; (* Declare Book1 and Book2 of type Books *)
(* procedure declaration *)
procedure printBook( var book: Books );
begin
   (* print Book info *)
   writeln ('Book  title : ', book.title);
   writeln('Book  author : ', book.author);
   writeln( 'Book  subject : ', book.subject);
   writeln( 'Book book_id : ', book.book_id);
end;
begin
   (* book 1 specification *)
   Book1.title  := 'C Programming';
   Book1.author := 'Nuha Ali '; 
   Book1.subject := 'C Programming Tutorial';
   Book1.book_id := 6495407;
   (* book 2 specification *)
   Book2.title := 'Telecom Billing';
   Book2.author := 'Zara Ali';
   Book2.subject := 'Telecom Billing Tutorial';
   Book2.book_id := 6495700;
   (* print Book1 info *)
   printbook(Book1);
   writeln; 
   (* print Book2 info *)
   printbook(Book2);
end.

编译并执行上述代码时,会产生以下结果 -

Book 1 title : C Programming
Book 1 author : Nuha Ali
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Zara Ali
Book 2 subject : Telecom Billing Tutorial
Book 2 book_id : 6495700

指针记录

您可以按照与定义指向任何其他变量的指针非常类似的方式定义指向记录的指针,如下所示 -

type
record-ptr = ^ record-name;
record-name = record
   field-1: field-type1;
   field-2: field-type2;
   ...
   field-n: field-typen;
end;

现在,您可以将记录类型变量的地址存储在上面定义的指针变量中。 要声明已创建指针类型的变量,请使用var关键字 -

var
   r1, r2, ... : record-ptr;

在使用这些指针之前,必须为记录名称类型变量创建存储,这些变量将由这些指针操作。

new(r1);
new(r2);

要使用指向该记录的指针访问记录成员,必须使用^。 运算符如下 -

r1^.feild1 := value1;
r1^.feild2 := value2;
...
r1^fieldn := valuen;

最后,当不再使用存储时,不要忘记将其用于处置 -

dispose(r1);
dispose(r2);

让我们使用指向Books记录的指针重写第一个示例。 希望您能够轻松理解这个概念 -

program exRecords;
type
BooksPtr = ^ Books;
Books = record
   title: packed array [1..50] of char;
   author: packed array [1..50] of char;
   subject: packed array [1..100] of char;
   book_id: longint;
end;
var
  (* Declare Book1 and Book2 of pointer type that refers to Book type *)
   Book1, Book2: BooksPtr; 
begin
   new(Book1);
   new(book2);
   (* book 1 specification *)
   Book1^.title  := 'C Programming';
   Book1^.author := 'Nuha Ali '; 
   Book1^.subject := 'C Programming Tutorial';
   Book1^.book_id := 6495407;
   (* book 2 specification *)
   Book2^.title := 'Telecom Billing';
   Book2^.author := 'Zara Ali';
   Book2^.subject := 'Telecom Billing Tutorial';
   Book2^.book_id := 6495700;
   (* print Book1 info *)
   writeln ('Book 1 title : ', Book1^.title);
   writeln('Book 1 author : ', Book1^.author);
   writeln( 'Book 1 subject : ', Book1^.subject);
   writeln( 'Book 1 book_id : ', Book1^.book_id);
   (* print Book2 info *)
   writeln ('Book 2 title : ', Book2^.title);
   writeln('Book 2 author : ', Book2^.author);
   writeln( 'Book 2 subject : ', Book2^.subject);
   writeln( 'Book 2 book_id : ', Book2^.book_id);
   dispose(Book1); 
   dispose(Book2);
end.

编译并执行上述代码时,会产生以下结果 -

Book 1 title : C Programming
Book 1 author : Nuha Ali
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Zara Ali
Book 2 subject : Telecom Billing Tutorial
Book 2 book_id : 6495700

With 语句

我们已经讨论过可以使用成员访问运算符(。)访问记录的成员。 这样,每次都必须写入记录变量的名称。 With语句提供了另一种方法。

请看我们的第一个示例中的以下代码片段 -

   (* book 1 specification *)
   Book1.title  := 'C Programming';
   Book1.author := 'Nuha Ali '; 
   Book1.subject := 'C Programming Tutorial';
   Book1.book_id := 6495407;

可以使用With语句编写相同的赋值 -

(* book 1 specification *)
With Book1 do
begin
   title  := 'C Programming';
   author := 'Nuha Ali '; 
   subject := 'C Programming Tutorial';
   book_id := 6495407;
end;