Здесь нужно использовать вс типизированный файл и, соответственно, вторую файловую переменную (fr2). Сделать это можно так: while not eof(fr) and (kol=0) do begin read(fr, Marshrut); if (Marshrut.NK.N <> '11') then write(fr2,Marshrut) else begin inc(kol); y:=Marshrut; end; end; if not eof(fr) then begin read(fr, Marshrut); write(fr2,Marshrut); end; if (kol=1) then write(fr2,y); Дальше нужно просто переписать оставшиеся записи из первого файла во второй. Затем оба файла закрыть, первый файл удалить, а второй переименовать (т.е. назвать его так, как назывался первый файл).
Попробуйте ещё вот такой вариант: (здесь k1 и k2 - целые, а y - типа ) if (Marshrut.NK.N = '11') then begin writeln('... '); inc(kol); y:=Marshrut; k1:=FilePos(fr)-1; k2:=FilePos(fr); if not eof(fr) then begin read(fr, Marshrut); seek (fr, k1); write(fr, Marshrut); seek (fr, k2); write (fr, y); end;
// PascalABC.NET 3.1, сборка 1204 от 24.03.2016 begin var F,G:file of integer; Assign(F,'FFile.dat'); Rewrite(F); for var i:=1 to 30 do Write(F,Random(-20,20)); // Файл F создан и заполнен Assign(G,'GFile.dat'); Rewrite(G); F.Seek(0); var e:integer; Print('F:'); while not F.Eof do begin Read(F,e); Print(e); if e>0 then Write(G,e) end; F.Seek(0); while not F.Eof do begin Read(F,e); if e<0 then Write(G,e) end; F.Seek(0); while not F.Eof do begin Read(F,e); if e=0 then Write(G,e) end; F.Close; Writeln; Print('G:'); G.Seek(0); while not G.Eof do begin Read(G,e); Print(e) end; G.Close end.
Данное решение неоптимально по времени, поскольку файл читается трижды. Можно сделать иной вариант, читая данные за один проход и помещая в две вс структуры памяти отрицательные и нулевые элементы.
// PascalABC.NET 3.1, сборка 1204 от 24.03.2016 begin var F,G:file of integer; Assign(F,'FFile.dat'); Rewrite(F); for var i:=1 to 30 do Write(F,Random(-20,20)); // Файл F создан и заполнен Assign(G,'GFile.dat'); Rewrite(G); var n:=F.FileSize; var neg,zer:array of integer; SetLength(neg,n); SetLength(zer,n); F.Seek(0); var e:integer; var ineg:=0; var izer:=0; Print('F:'); while not F.Eof do begin Read(F,e); Print(e); if e>0 then Write(G,e) else if e<0 then begin neg[ineg]:=e; Inc(ineg) end else begin zer[izer]:=e; Inc(izer) end end; F.Close; Writeln; SetLength(neg,ineg); foreach e in neg do Write(G,e); SetLength(zer,izer); foreach e in zer do Write(G,e); Print('G:'); G.Seek(0); while not G.Eof do begin Read(G,e); Print(e) end; G.Close end.
Відповідь:
321=
=
=
Пояснення:
2A=2*16^1+10*16^0=2*16+10=32+10=42 - основание 10
основание 8 2А=42=5*8^1+2*8^0=52
основание 2 2А=42=52=32+8+2=1*2^5+0*2^4+1*2^3+0*2^2+1*2^1+0*2^0=101010
Последння строка имеет вид
101010 52 42 2А
Основание 10
321=256+65=256+64+1=1*16^2+4*16^1+1*16^0=
321=320+1=5*64+1=5*8^2+0*8^1+1*8^0=
321=256+64+1=2^8+2^6+2^0=1*2^8+0*2^7+1*2^6+0*2^5+0*2^4+0*2^3+0*2^2+0*2^1+1*2^0=
321=
=
=
Основание 8
Основание 2