请教各位高手,现有固定格式文件文件例:
123345 姓名 22
234234 年龄 23
828232 姓名 33
第一个字段长度为10;
第二个字段长度为10;
第三个字段长度为2;
我想插入第四条记录
125555 年龄 44
插入后的记录为
123345 姓名 22
125555 年龄 44
234234 年龄 23
828232 姓名 33
按第一字段进行排序,我想用折半插入方法,可是不知道怎么实现?
请教各位高手,如何实现!!!???
注:用PASCAL实现,不用DELPHI,是DOS下编程实现!!!!
123345 姓名 22
234234 年龄 23
828232 姓名 33
第一个字段长度为10;
第二个字段长度为10;
第三个字段长度为2;
我想插入第四条记录
125555 年龄 44
插入后的记录为
123345 姓名 22
125555 年龄 44
234234 年龄 23
828232 姓名 33
按第一字段进行排序,我想用折半插入方法,可是不知道怎么实现?
请教各位高手,如何实现!!!???
注:用PASCAL实现,不用DELPHI,是DOS下编程实现!!!!
program Project1;{$APPTYPE CONSOLE}type
TMyRecord = record
rId: string;
rName: string;
rValue: string;
end;var
Text: TextFile;
S: string;
A: array of TMyRecord;
R: TMyRecord;
I: Integer;
B: Boolean;
begin
///////Begin 读文件
AssignFile(Text, 'c:\temp\temp.txt');
Reset(Text);
while not Eof(Text) do
begin
SetLength(A, Length(A) + 1);
Readln(Text, S);
A[High(A)].rId := Copy(S, 1, 10);
A[High(A)].rName := Copy(S, 11, 10);
A[High(A)].rValue := Copy(S, 21, 2);
Writeln(Output, A[High(A)].rId, A[High(A)].rName, A[High(A)].rValue);
end;
CloseFile(Text);
///////End 读文件 R.rId := '125555';
R.rName := '年龄';
R.rValue := '44'; ///////Begin 写文件
AssignFile(Text, 'c:\temp\temp.txt');
Rewrite(Text);
B := True; // 是否需要插入
for I := Low(A) to High(A) do
begin
if B and (A[I].rId > R.rId) then // 判断插入的位置
begin
Writeln(Text,
R.rId, StringOfChar(#32, 10 - Length(R.rId)), // 补空白
R.rName, StringOfChar(#32, 10 - Length(R.rName)),
R.rValue, StringOfChar(#32, 2 - Length(R.rValue)));
B := False; // 已经插入
end;
Writeln(Text, A[I].rId, A[I].rName, A[I].rValue);
end;
CloseFile(Text);
///////End 写文件 Write('输入回车结束程序。');
Readln(Input);
end.
program Project1;{$APPTYPE CONSOLE}type
TMyRecord = record
rId: string;
rName: string;
rValue: string;
end;var
Text: TextFile;
S: string;
A: array[0..255]of TMyRecord; // <<<<<<<<<<<缓冲256条记录
R: TMyRecord;
I: Integer;
B: Boolean;
Count: Integer;
begin
///////Begin 读文件
AssignFile(Text, 'c:\temp\temp.txt');
Reset(Text);
Count := 0;
while not Eof(Text) do
begin
Readln(Text, S);
A[Count].rId := Copy(S, 1, 10);
A[Count].rName := Copy(S, 11, 10);
A[Count].rValue := Copy(S, 21, 2);
Writeln(Output, A[Count].rId, A[Count].rName, A[Count].rValue);
Inc(Count);
end;
CloseFile(Text);
///////End 读文件 R.rId := '125555';
R.rName := '年龄';
R.rValue := '44'; ///////Begin 写文件
AssignFile(Text, 'c:\temp\temp1.txt');
Rewrite(Text);
B := True; // 是否需要插入
for I := 0 to Count - 1 do
begin
if B and (A[I].rId > R.rId) then // 判断插入的位置
begin
Writeln(Text,
R.rId, StringOfChar(#32, 10 - Length(R.rId)), // 补空白
R.rName, StringOfChar(#32, 10 - Length(R.rName)),
R.rValue, StringOfChar(#32, 2 - Length(R.rValue)));
B := False; // 已经插入
end;
Writeln(Text, A[I].rId, A[I].rName, A[I].rValue);
end;
CloseFile(Text);
///////End 写文件 Write('输入回车结束程序。');
Readln(Input);
end.
用数组的方式是比较简单,可是如果文本内容较大的情况下,则非常占用内存资源(注:程序所应用的地方不是PC机,是仿DOS的数据采集器)
请问有没有别的方法可以实现?首先不占用太多内存资源!!!!
这么经典的算法+语法。