我在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工作得很好,但是一旦我添加了一些记录,我将如何搜索作者?
答案 0 :(得分:2)
由于您的记录似乎没有按作者排序,因此您需要使用线性搜索。调整Show_All例程以实现此目的,遍历查找作者的每条记录。
如果你有一个大型数据库,那么性能将是一个问题,你应该考虑使用一个真正的数据库。