Скачиваний:
91
Добавлен:
04.03.2014
Размер:
4.64 Кб
Скачать
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, Grids;

type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
MainMenu1: TMainMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
N4: TMenuItem;
N5: TMenuItem;
StringGrid2: TStringGrid;
N6: TMenuItem;
procedure N6Click(Sender: TObject);
procedure N5Click(Sender: TObject);
procedure N4Click(Sender: TObject);
procedure N3Click(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation


Type A=record //описание типа A(automobile)
Mark:string[20];
FIO:string[20];
S:real;
end;
var sf:string;
//переменная, хранящая имя файла, с которым будем работать
{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
//задание параметров таблицы при создании формы
Begin
with Form1.Stringgrid1 do Begin
ColCount:=3;
FixedCols:=0;
RowCount:=50;
FixedRows:=1;
Colwidths[0]:=150;
Colwidths[1]:=150;
Colwidths[2]:=80;

cells[0,0]:='Марка автомобиля';
cells[1,0]:='Ф.И.О Владельца';
cells[2,0]:='Пробег (км)';
end;
with Form1.Stringgrid2 do Begin
ColCount:=2;
FixedCols:=0;
RowCount:=6;
FixedRows:=1;
Colwidths[0]:=150;
Colwidths[1]:=150;
cells[0,0]:='Марка автомобиля';
cells[1,0]:='Средний пробег';

end;
with Stringgrid1 do options:=options+[goEditing];
//разрешить редактирование
end;







procedure TForm1.N2Click(Sender: TObject);
{открытие файла}
var i,j:integer; z:A; f:file of A;
begin

with Form1.StringGrid1 do
for i := 1 to RowCount - 1 do
for j := 0 to ColCount do cells[j,i]:='';

if opendialog1.execute then
begin
sf:=opendialog1.FileName;
end;

AssignFile(f,sf);
Reset(f);
i:=0;
while not Eof(f) do begin Read(f,z); i:=i+1;
with Form1.StringGrid1 do begin
cells[0,i]:=z.mark;
cells[1,i]:=z.fio; cells[2,i]:=floattostr(z.s);
end;
end; closefile(f); end;




procedure TForm1.N3Click(Sender: TObject);
{сохранение файла}
var i:integer; z:A; f:file of A;
begin
if savedialog1.execute then
begin
sf:=savedialog1.FileName;
end;

AssignFile(f,sf);
Rewrite(f);
with Form1.StringGrid1 do
for i := 1 to RowCount - 1 do
if cells[0,i]<>'' then begin
z.mark:=cells[0,i];
z.fio:=cells[1,i]; z.s:=strtofloat(cells[2,i]);
write(f,z);
end; Closefile(f); end;




procedure TForm1.N4Click(Sender: TObject);
{oчистка таблицы}
var i,j:integer;
begin
with Form1.StringGrid1 do
for i := 1 to RowCount - 1 do
for j := 0 to ColCount do cells[j,i]:='';
end;



procedure TForm1.N5Click(Sender: TObject);
begin
close;
end;


procedure TForm1.N6Click(Sender: TObject); // рассчитывает среднее и выводит в файл
var i,j,k,t:integer; sr: real; ye:boolean; n:textfile; s:string;
begin
i:=0;
k:=1;

repeat i:=i+1; //записываем в ячейки суммы пробегов

j:=0;
ye:=false;
repeat j:=j+1;
//если марка уже попадалась
if Form1.StringGrid1.cells[0,i]=Form1.StringGrid2.cells[0,j] then begin
ye:=true;
sr:=strtofloat(Form1.StringGrid2.cells[1,j])+strtofloat(Form1.StringGrid1.cells[2,i]);
Form1.StringGrid2.cells[1,j]:=floattostr(sr);//пока суммируем
end; until (ye=true) or (j=5);

if ye=false then
//если не попадалась, заносим её в список
begin
Form1.StringGrid2.cells[0,k]:=Form1.StringGrid1.cells[0,i];
Form1.StringGrid2.cells[1,k]:=Form1.StringGrid1.cells[2,i];
k:=k+1;
end;
until (Form1.StringGrid1.cells[0,i+1]='');

j:=0; //нахождение среднего пробега
repeat j:=j+1;
t:=0; i:=0;
repeat i:=i+1;
if Form1.StringGrid2.cells[0,j]=Form1.StringGrid1.cells[0,i] then
t:=t+1;
until (Form1.StringGrid1.cells[0,i+1]='');
Form1.StringGrid2.cells[1,j]:=floattostr(strtofloat(Form1.StringGrid2.cells[1,j])/t);
until (Form1.StringGrid2.cells[0,j+1]='');


//созраняем в текстовый документ
AssignFile(n,'Результат.txt');
Rewrite(n);
writeln(n,'Список автомобилей:');
j:=0;
repeat j:=j+1;
s:='Марка : '+Form1.StringGrid1.cells[0,j]+' Хозяин:'+Form1.StringGrid1.cells[1,j]+' Пробег : '+Form1.StringGrid1.cells[2,j];
writeln(n,s);
until (Form1.StringGrid1.cells[0,j+1]='');

writeln(n,'Суммарный средний пробег:');

j:=0;
repeat j:=j+1;
s:='Марка : '+Form1.StringGrid2.cells[0,j]+' Пробег:'+Form1.StringGrid2.cells[1,j];
writeln(n,s);
until (Form1.StringGrid2.cells[0,j+1]=''); Closefile(n);
end;



end.
Соседние файлы в папке Типизированный файл. Автомобили