记录
编辑教程记录
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 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;
选择支付方式:
备注:
转账时请填写正确的金额和备注信息,到账由人工处理,可能需要较长时间