Специалист
Регистрация: 27.08.2008
Адрес: Санкт-Петербург
Сообщений: 27,807
Сказал(а) спасибо: 340
Поблагодарили 583 раз(а) в 208 сообщениях
Репутация: 113184
|
Ну вроде как добил. Получите. Основная ошибка в том, что Вы кое о чем забыли, а именно о том, что после поиска в текстовый файл добавляются строки, содержащие информацию об отобранных поездах, и на них-то процедура преобразования и спотыкается. Кстати, и сама эта процедура у Вас излишне усложнена: совершенно нет никакой необходимости вычленять числовые форматы из строкового: достаточно лишь правильно организовать считывание пункта назначения, а остальные данные так и читать, как числа.
Функцию FileExists (она закомментарена) можете удалить. Просто у меня такой среди стандартных нету, вот и пришлось добавить самодельную.
Код:
program Project2a;
{$APPTYPE CONSOLE}
uses
SysUtils,
Windows;
const
n=20;
type
traininfo=record
numbertrain:integer;
destination:string[15];
timeofdeparture_h:integer;
timeofdeparture_m:integer;
sv:Integer;
compartment:integer;
plackart:integer;
end;
tmas=array [1..n] of traininfo;
var
train:tmas;
i,t_h,t_m:Integer;
time1,time2:real;
destination,s:string;
f,f1:Text;
f3:file of traininfo ;
(*
//-----------------------------------------------------------------
function FileExists(Name:String):Boolean;
Var
ffe:Text;
IOres:Integer;
begin
{$I-}
Assign(ffe,Name);
Reset(ffe);
IOres:=IOResult;
If IOres=0 then Close(ffe);
FileExists:=IOres=0;
{$I+}
end; *)
//-----------------------------------------------------------------
procedure vvoddannuh; //заполнение файла данными
var
i,numbertrain,timeofdeparture_h,timeofdeparture_m,
sv,compartment,plackart:Integer;
destination:String;
f:Text;
begin
Assign(f,'train_1.txt');
Rewrite(f);
writeln (f,'------------------------------------------------------------');
writeln (f,'номер / пункт назначения / время отпр. / св / купе / плацк. ');
writeln (f,'------------------------------------------------------------');
for i:=1 to n do
begin
Write('Введите номер поезда: ');
Readln(numbertrain);
Write('Введите пункт назначения: ');
Readln(destination);
Write('Введите время отправления (чч мм): ');
Readln(timeofdeparture_h,timeofdeparture_m);
Write('Введите количество свободных мест в СВ: ');
Readln(sv);
Write('Введите количество свободных мест в купе: ');
Readln(compartment);
Write('Введите количество свободных мест в плацкарте: ');
Readln(plackart);
writeln(f,numbertrain:5,destination:19,timeofdeparture_h:8,timeofdeparture_m:3,sv:8,compartment:7,plackart:8);
writeln
end;
Close(f)
end;
//-----------------------------------------------------------------
Procedure vuvoddannuh(a:tmas); //вывод данных в файл
var
i:Integer;
f:Text;
begin
Assign(f,'train_2.txt');
Rewrite(f);
writeln (f,'------------------------------------------------------------');
writeln (f,'номер / пункт назначения / время отпр. / св / купе / плацк. ');
writeln (f,'------------------------------------------------------------');
for i:=1 to n do
writeln(f,a[i].numbertrain:5,a[i].destination:19,a[i].timeofdeparture_h:8,a[i].timeofdeparture_m:3,a[i].sv:8,a[i].compartment:7,a[i].plackart:8);
Close(f)
end;
//------------------------------------------------------------------------------------------
Procedure Read_data(Var a: tmas); //считывание информации
Var
i,j:Integer;
f:Text;
C:Char;
begin
Assign(f,'train_1.txt');
Reset(f);
readln(f);
readln(f);
readln(f);
for i:=1 to n do
begin
read(f,a[i].numbertrain);
repeat
read(f,C);
until C<>' ';
a[i].destination:=C;
repeat
read(f,C);
if C<>' ' then a[i].destination:=a[i].destination+C;
until C=' ';
readln(f,a[i].timeofdeparture_h,a[i].timeofdeparture_m,a[i].sv,a[i].compartment,a[i].plackart);
end;
Close(f)
end;
//------------------------------------------------------------------------------------------
Procedure Swap(Var k, l: traininfo); //метод пузырька
Var m:traininfo;
begin
m:=k;
k:=l;
l:=m;
end;
// --------------------------------------------------------------------------
Procedure sortirovka; //процедура сортировки
var
x:traininfo;
i,j:Integer;
begin
for i:=1 to n-1 do
for j:=1 to n-i do
if train[j].destination>train[j+1].destination then
Swap(train[j],train[j+1]);
end;
//--------------------------------------------------------------------------------
Procedure search_train(a:tmas;destination:string;tm1,tm2:real); //поиск по заданным параметрам
var
i,S:Integer;
f:Text;
begin
Assign(f,'train_2.txt');
Append(f);
Writeln(f);
S:=0;
for i:=1 to n do
if (a[i].destination=destination) and
(a[i].timeofdeparture_h+a[i].timeofdeparture_m/100>=tm1) and
(a[i].timeofdeparture_h+a[i].timeofdeparture_m/100<=tm2) then
begin
Writeln(f,'Номер поезда: ',a[i].numbertrain);
Writeln(f,'Пункт назначения: ',a[i].destination);
Writeln(f,'Время отправления: ',a[i].timeofdeparture_h,'-',a[i].timeofdeparture_m);
Writeln(f,'Количество мест в св: ',a[i].sv) ;
Writeln(f,'Количество мест в купе: ',a[i].compartment);
Writeln(f,'Количество мест в плацкарте: ',a[i].plackart);
Writeln(f);
S:=S+1
end;
if S=0 then writeln(f,'Таких поездов нет');
Close(f);
end;
//--------------------------------------------------------------------------------
procedure vvoddannuhtype;
var
s:string;
s19:string[19];
z:traininfo;
{k:integer;}
begin
Assign(f1,'train_2.txt');
Reset(f1);
Assign(f3,'train_3');
Rewrite(f3);
for i:=1 to 3 do readln(f1);
for i:=1 to n do
begin
read(f1,z.numbertrain);
read(f1,s19);
s:=s19;
readln(f1,z.timeofdeparture_h,z.timeofdeparture_m,z.sv,z.compartment,z.plackart);
while pos(' ',s)>0 do
delete(s,pos(' ',s),1);
z.destination:=s;
write(f3,z);
end;
Close(f1);
close(f3);
end;
Procedure vuvoddannuhtype;
var z:traininfo;
begin
reset(f3);
writeln ('------------------------------------------------------------');
writeln ('номер/ пункт назначения / время отпр. / св /купе / плац. ');
writeln ('------------------------------------------------------------');
while not eof(f3) do
begin
read(f3,z);
writeln(z.numbertrain:5,z.destination:19,z.timeofdeparture_h:8,z.timeofdeparture_m:3,z.sv:8,z.compartment:7,z.plackart:8);
end;
close(f3);
end;
begin
SetConsoleOutputCP(1251);
if FileExists('train_1.txt')then
begin
read_data(train);
assign(f,'train_1.txt');
reset(f);
while not eof(f) do
begin
readln(f,s);
writeln(s);
end;
close(f);
sortirovka;
writeln;
vuvoddannuh(train);
writeln;
Writeln('Введите пункт назначения:');
Readln(destination);
write('Введите период времени (формат ввода чч мм) с ');
readln(t_h,t_m);
time1:=t_h+t_m/100;
write('по ');
Readln(t_h,t_m);
time2:=t_h+t_m/100;
search_train(train,destination,time1,time2);
assign(f1,'train_2.txt');
reset(f1);
while not eof(f1) do
begin
readln(f1,s);
writeln(s)
end;
close(f1);
vvoddannuhtype;
vuvoddannuhtype
end
else vvoddannuh;
Writeln('Для выхода нажмите "Enter"');
readln
end.
|