2013-04-27 65 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; 
相关问题