排序记录错误文件

时间:2013-05-08 14:02:50

标签: delphi record pascal bubble-sort

我需要对记录文件进行排序,但我不太清楚如何做到这一点。我有一个记录文件,我试图使用简单的冒泡排序对它们进行排序,但我被困住了,需要一些帮助。谁能帮助我?我将列出以下代码。

HighScoreFile = file of HighScoreRecord;


var
frmEnterDetails: TfrmEnterDetails;
HighScoreMasterFile: HighScoreFile;
HighScore:HighScoreRecord;
FileArray:array[1..20] of HighScoreRecord;
i:integer;

Procedure Sort
var
  i,j,temp:integer;

AssignFile(HighScoreMasterFile, 'HighScores.dat');
Reset(HighScoreMasterFile);
while not eof(HighScoreMasterFile) do
begin    
  i:=i+1;
  read(HighScoreMasterFile, HighScore);
  FileArray[i].Name:=HighScore.Name;
  FileArray[i].Date:=HighScore.Date;
  FileArray[i].FinalScore:=HighScore.FinalScore;

  for i:=0 19 do
    for j:=0 to 18 do
      if FileArray[j].Score > FileArray[j+1].Score then
        begin
          FileArray[temp]:=FileArray[j];
          FileArray[j]:=FileArray[j+1];
          FileArray[j+1]:=FileArray[temp];
       end;
end;

任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

有可能100%不正确,我还没有在IDE中测试过。您可以使用调试器来查看此算法如何逐步工作。此链接还可以很好地解释冒泡排序的工作原理http://delphi.wikia.com/wiki/Bubble_sort

    HighScoreFile = file of HighScoreRecord;


    var
    frmEnterDetails: TfrmEnterDetails;
    HighScoreMasterFile: HighScoreFile;
    HighScore:HighScoreRecord;
    FileArray:array[1..20] of HighScoreRecord;
    i:integer;

    Procedure Sort
    var
      i,j,temp:integer;
    begin
    AssignFile(HighScoreMasterFile, 'HighScores.dat');
    Reset(HighScoreMasterFile);
    i := 0;
    while not eof(HighScoreMasterFile) do
    begin    
      i:=i+1;
      read(HighScoreMasterFile, HighScore);
      FileArray[i].Name:=HighScore.Name;
      FileArray[i].Date:=HighScore.Date;
      FileArray[i].FinalScore:=HighScore.FinalScore;
    end;

      for i:=1 to 19 do
        for j:=1 to 18 do
          if FileArray[i].Score > FileArray[j+1].Score then
            begin
              temp := i;
              HighScore := FileArray[temp];
              FileArray[temp]:=FileArray[j];
              FileArray[j]:=HighScore;
           end;
  end;