Всем привет!
У меня проблема с такой задачей:даны 2 файла упорядоченные по неубыванию, получить новый файл, упорядоченный из элементов этих двух файлов по неубыванию.
Никак не могу сделать чтоб работала для всех вариантов исходных данных...
Client, поиск по форуму по ключевым словам сортировка Неймана слиянием выдает решение задачи.... Но раз ты сам пытался ее решить, лучше (прежде, чем брать готовое из поиска) приведи свою программу и те файлы, на которых она не отрабатывает, посмотрим, где ты ошибся...
procedure sliyanie;Пример
var
t,t1,t2:boolean;
a,b:integer;
begin
reset(f3);
reset(f2);
rewrite(f4);
seek(f2,filesize(f2)-1);
read(f2,a);
seek(f3,filesize(f3)-1);
read(f3,b);
t:=a>=b;
reset(f2);
reset(f3);
read(f2,a);
read(f3,b);
while not eof(f2) and not eof(f3) do begin
if a<b then begin
write(f4,a);
read(f2,a)
end
else begin
write(f4,b);
read(f3,b)
end;
end;
t1:=true;
t2:=true;
if t then begin
while not eof(f2) do begin
if (a>b) and t1 then begin
write(f4,b);
t1:=false
end;
write(f4,a);
read(f2,a);
if eof(f2) and t2 then begin
seek(f2,filesize(f2)-1);
t2:=false
end;
end;
end
else
while not eof(f3) do begin
if (a<b) and t1 then begin
write(f4,a);
t1:=false
end;
write(f4,b);
read(f3,b);
if eof(f3) and t2 then begin
seek(f3,filesize(f3)-1);
t2:=false
end;
end;
end;
procedure sli;На выше указанные примеры отработало, а на
var
t,t1,t2:boolean;
a,b:integer;
begin
reset(f3);
reset(f2);
rewrite(f4);
read(f2,a);
read(f3,b);
if eof(f2) then reset(f2);
if eof(f3) then reset(f3);
while not eof(f2) and not eof(f3) do begin
if a<b then begin
write(f4,a);
read(f2,a)
end
else begin
write(f4,b);
read(f3,b)
end;
end;
t:=eof(f2);
if a<b then write(f4,a)
else write(f4,b);
if t then
while not eof(f3) do begin
read(f3,b);
write(f4,b)
end
else
while not eof(f2) do begin
read(f2,a);
write(f4,a)
end;
end;
Смотри:
procedure sli;
var
a, b: integer;
eof_a, eof_b: boolean;
begin
reset(f2); reset(f3);
rewrite(f4);
eof_a := eof(f2); if not eof_a then read(f2, a); { хранишь признак конца файла ПЕРЕД чтением данных }
eof_b := eof(f3); if not eof_b then read(f3, b);
while (not eof_a) and (not eof_b) do begin
if a < b then begin
write(f4, a);
eof_a := eof(f2); if not eof_a then read(f2, a);
end
else begin
write(f4, b);
eof_b := eof(f3); if not eof_b then read(f3, b);
end;
end;
if eof_a then begin { Если закончился первый файл, то }
write(f4, b); { <--- сбрасываешь последнее прочитанное из второго файла число }
while not eof(f3) do begin { и потом - весь второй файл до конца... }
read(f3, b); write(f4, b);
end;
end
else begin { здесь - все наоборот }
write(f4, a);
while not eof(f2) do begin
read(f2, a); write(f4, a);
end;
end;
close(f4);
end;
eof_a := eof(f2);Логично, но я бы до этого не додумался((