【问题标题】:searching an unindexed File-of-Record in pascal (Delphi 7)在 pascal (Delphi 7) 中搜索未索引的 File-of-Record
【发布时间】: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 工作得很好,但是一旦我添加了一些记录,我将如何搜索作者?

【问题讨论】:

  • 你需要更具体。你有什么样的问题?你是如何阅读记录的?等等。
  • 由于您已经对记录进行了排序,因此最快的搜索将是二分搜索。除此之外,我猜。

标签: delphi search pascal


【解决方案1】:

由于您的记录似乎没有按作者排序,您需要使用线性搜索。调整您的 Show_All 例程以实现此目的,遍历每条记录以查找作者。

如果您有一个大型数据库,那么性能将是一个问题,您应该考虑使用真正的数据库。

【讨论】:

  • 谢谢,但是线性搜索不会只显示一个结果吗?我会调查一下,看看我能弄清楚什么!我不擅长“真正的”数据库,它只能容纳 150 本书左右。谢谢!
  • 用那几本书不需要真正的数据库。如果您选择终止搜索,线性搜索只会显示一个匹配项。对于多场比赛,只需继续到最后,并收集所有比赛。
  • 感谢您的帮助!我已经解决了所有问题,现在一切都运行良好!干杯!
  • 矮胖的;在游戏中开始使用 File of Record 有点晚了。考虑学习 XML、INI、JSON(文本比二进制记录更适合小型数据集)和简单的数据库持久性更适合大型数据集,正如 David 所说。随着您继续学习 Delphi,它的数据库功能是 Delphi 强大功能的关键要素。也不要为 BDE 烦恼。在 Delphi 7 中,尝试 ibObjects 和 Firebird。
猜你喜欢
  • 1970-01-01
  • 2011-08-23
  • 1970-01-01
  • 2015-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多