【发布时间】:2020-07-30 20:13:52
【问题描述】:
我正在为在 Delphi 7 上使用 Pascal 的朋友创建一个控制台应用程序。我已经对添加记录和查看它们进行了排序,但我在搜索它们时遇到了问题。记录存储在 .dat 文件中。任何帮助都会很棒!
谢谢!
到目前为止我的代码...
Type
BookRecord = Record
Number : Integer;
Title : String[50];
Author : String[50];
ISBN : String[13];
end;
Var
Book : BookRecord;
f : file of BookRecord ;
Procedure Add_Book;
Var
Title, Author, ISBN : String;
i : integer;
Begin
Assign (f, 'Books.dat');
reset (f);
Seek (f, filesize(f));
Book.Number := (filepos(f)+1);
Write ('Title: ');
Readln (Title);
For i := 1 to Length(Title) do
Title[i] := UpCase(Title[i]);
Book.Title := Title;
Write ('Author: ');
Readln (Author);
For i := 1 to Length(Author) do
Author[i] := UpCase(Author[i]);
Book.Author := Author;
Write ('ISBN: ');
readln (ISBN);
For i := 1 to Length(ISBN) do
ISBN[i] := UpCase(ISBN[i]);
Book.ISBN := ISBN;
write (f, Book);
Close (f);
End;
Procedure Show_All;
Begin
Assign (f, 'Books.dat');
Reset (f);
while FilePos(f) <> FileSize(f) do
Begin
Read (f,book);
Writeln ('File: ' , Book.Number);
Writeln ('Title: ' , Book.Title);
Writeln ('Author: ' , Book.Author);
Writeln ('ISBN: ' , Book.ISBN);
Writeln;
end;
Writeln; Writeln;
Center ('END OF FILE!');
readln;
Close (f);
end;
Procedure Delete_All;
Begin
Assign (f, 'Books.Dat');
Reset (f);
Seek (f,0);
Truncate (f);
Close (f);
end;
到目前为止,这基本上是我的代码...... Add_Book、Show_All 和 Delete_All Procs 工作得很好,但是一旦我添加了一些记录,我将如何搜索作者?
【问题讨论】:
-
你需要更具体。你有什么样的问题?你是如何阅读记录的?等等。
-
由于您已经对记录进行了排序,因此最快的搜索将是二分搜索。除此之外,我猜。