Специалист
Регистрация: 27.08.2008
Адрес: Санкт-Петербург
Сообщений: 27,807
Сказал(а) спасибо: 340
Поблагодарили 583 раз(а) в 208 сообщениях
Репутация: 113184
|
Цитата:
Сообщение от virginia
подскажите,пожалуйста,как создать текстовый файл с данными поездов,а из него уже сформировать новый текстовый файл,где поезда отсортированы и выведены данные о поездах,удовлетворяющих критериям?
|
Ну, кажется, довел я Вашу программу до ума. Все данные выводятся в файлы, исправлен ввод времени, формат вывода, улучшена процедура сортировки (строковые переменные можно сравнивать непосредственно, не прибегая к функции Ord, она автоматически вызовется; сам недавно узнал про такое), добавлена процедура чтения файла и т.п.
Код:
uses
SysUtils,
Windows;
const
n=3;
type
telem=record
numbertrain:integer;
destination:string;
timeofdeparture_h:integer;
timeofdeparture_m:integer;
sv:Integer;
compartment:integer;
plackart:integer;
end;
tmas=array [1..n] of telem;
var
train:tmas;
i,t_h,t_m:Integer;
time1,time2:real;
destination:string;
//-------------------------------------------------------------------
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: telem);
Var m:telem;
begin
m := k;
k := l;
l := m;
end;
// --------------------------------------------------------------------------
Procedure sortirovka;
var
x:telem;
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;
begin
SetConsoleOutputCP(1251);
vvoddannuh;
read_data(train);
sortirovka;
vuvoddannuh(train);
Writeln('введи пункт назначения:');
Readln(destination);
writeln('введи период времени(формат ввода чч мм) с');
readln(t_h,t_m);
time1:=t_h+t_m/100;
writeln('по');
Readln(t_h,t_m);
time2:=t_h+t_m/100;
search_train(train,destination,time1,time2);
end.
|