2013-04-27 99 views

回答

0

將文件讀入記錄數組中,將每條記錄插入到正確的位置。然後將數組寫回到文件中。

以下是未經測試的代碼,寫在我頭上。關鍵線被標記爲** ** - 第一行爲新讀取的記錄找到正確的位置,第二行從該位置向前碰撞所有記錄。

const 
maxrec = 50; 

type 
MyRecord = record 
      name: string[63]; 
      date: tdatetime; 
      score: integer 
      end; 

var 
myfile: file of myrecord; 
rec: myrecord; 
data: array [1..maxrec] of myrecord; 
i, j, count: integer; 

begin 
fillchar (data, sizeof (data), 0); 
assignfile (myfile, '.....'); 
reset (myfile); 
read (myfile, rec); 
count:= 0; 
while not eof (myfile) do 
    begin 
    i:= 1; 
    while (i <= count) and (rec.score > data[i].score) do inc (i); //** 
    for j:= i to count do data[j+1]:= data[j];      //** 
    data[i]:= rec; 
    inc (count); 
    end; 

closefile (myfile); 
assignfile (myfile, '.......'); 
rewrite (myfile); 
for i:= 1 to count do write (myfile, data[i]); 
closefile (myfile); 
end; 
相關問題