Pascal 记录
-
记录
Pascal数组允许您定义变量的类型,这些变量可以容纳相同种类的多个数据项,但是记录是Pascal中可用的另一种用户定义的数据类型记录,它允许您组合不同种类的数据项。记录由不同的字段组成。假设您想跟踪图书馆中的书籍,则可能要跟踪有关每本书的以下属性-- 标题
- 作者
- 学科
- 书号
-
定义记录
要定义记录类型,可以使用类型声明语句。记录类型定义为-type record-name = record field-1: field-type1; field-2: field-type2; ... field-n: field-typen; end;
这是您要声明Book记录的方式-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 title : C Programming Book author : Nuha Ali Book subject : C Programming Tutorial Book book_id : 6495407 Book title : Telecom Billing Book author : Zara Ali Book subject : Telecom Billing Tutorial Book 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;