
лабораторная работа / лабораторные работы по LAZARUS / Отчет спо лазарус лаба 6 вариант 8
.docxЦель работы – изучить принцип работы с файлами и создать приложение, в котором используются файлы с помощью объектно-ориентированного языка Lazarus.
ЗАДАНИЕ: В текстовом файле храниться список служащих. Для каждого служащего указаны фамилия и инициалы, название занимаемой должности, год поступления на работу и оклад. Написать программу, выполняющую следующие действия:
-корректировку или дополнение списка с клавиатуры;
-сортировку по фамилии, окладу или году поступления;
-вывод на экран информации о служащем, фамилия которого введена с клавиатуры;
-запись списка в файл под тем же или новым именем.
ЛИСТИНГ ПРОГРАММЫ:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Grids,
Buttons, Spin, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
BitBtnSort2: TBitBtn;
BitBtnSort3: TBitBtn;
BitBtnNew: TBitBtn;
BitBtnOpen: TBitBtn;
BitBtnSort: TBitBtn;
BitBtnSave: TBitBtn;
BitBtn5: TBitBtn;
Button1: TButton;
Edit1: TEdit;
Label1: TLabel;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
SpinEdit1: TSpinEdit;
StringGrid1: TStringGrid;
procedure BitBtnNewClick(Sender: TObject);
procedure BitBtnOpenClick(Sender: TObject);
procedure BitBtnSaveClick(Sender: TObject);
procedure BitBtnSort2Click(Sender: TObject);
procedure BitBtnSort3Click(Sender: TObject);
procedure BitBtnSortClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure SpinEdit1Change(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{ TForm1 }
type
zap=record
fio,dol: string[20];
year,oklad: integer;
end;
var
MZap: array[1..25] of zap;
FileZap: file of zap;
FileText: TextFile;
FileNameZap,FileNameText: string;
n: integer;
procedure TForm1.FormCreate(Sender: TObject);
begin
with StringGrid1 do
begin
Cells[0,0]:='ФИО';
Cells[1,0]:='Должность';
Cells[2,0]:='Год поступления';
Cells[3,0]:='Оклад';
end;
BitBtnSort.Hide;
BitBtnSort2.Hide;
BitBtnSort3.Hide;
BitBtnSave.Hide;
end;
procedure TForm1.BitBtnSortClick(Sender: TObject);
var
i,j :integer;
vper:zap;
begin
for i:=1 to n do
with StringGrid1,MZap[i] do
begin
fio:=Cells[0,i];
dol:=Cells[1,i];
year:=StrToInt(Cells[2,i]);
oklad:=StrToInt(Cells[3,i]);
end;
for i:=1 to n-1 do
for j:=i+1 to n do
if MZap[i].dol>MZap[j].dol then
begin
vper:=MZap[i];
MZap[i]:=MZap[j];
MZap[j]:=vper;
end;
for i:=1 to n do
with StringGrid1,MZap[i] do
begin
Cells[0,i]:=fio;
Cells[1,i]:=dol;
Cells[2,i]:=IntToStr(year);
Cells[3,i]:=IntToStr(oklad);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var i,a,n: integer;
x: string;
begin
x:=Edit1.Text;
a:=0;
for i:=1 to n do
with StringGrid1,MZap[i] do
begin
if x=MZap[i].fio
then
begin
Label1.Caption:=('Должность:'+#32+StringGrid1.Cells[1,i]+#13+'Год поступления:'+#32+StringGrid1.Cells[2,i]+#13+'Размер оклада:'+#32+StringGrid1.Cells[3,i]);
a:=1
end;
end;
if a<>1
then
begin
MessageDlg('Такого служащего нет в списке', mtInformation, [mbOk], 0)
end;
end;
procedure TForm1.BitBtnNewClick(Sender: TObject);
var
i:integer;
begin
if MessageDlg(Содержимое существующего файла будет уничтожено. Вы уверены?',mtConfirmation, mbYesNoCancel, 0)=mrYes then
begin
for i:=1 to n do
with StringGrid1,MZap[i] do
begin
fio:=Cells[0,i];
dol:=Cells[1,i];
year:=StrToInt(Cells[2,i]);
oklad:=StrToInt(Cells[3,i]);
end;
with OpenDialog1 do
begin
Title:='Nicaaiea oaeea';
if Execute then
begin
FileNameZap:=FileName;
AssignFile(FileZap,FileNameZap);
ReWrite(FileZap);
for i:=1 to n do
write(FileZap,MZap[i]);
CloseFile(FileZap);
end;
end;
end;
end;
procedure TForm1.BitBtnOpenClick(Sender: TObject);
var
i:integer;
begin
with OpenDialog1 do
begin
Title:='Ioe?uoea oaeea';
if Execute then
begin
FileNameZap:=FileName;
AssignFile(FileZap,FileNameZap);
ReSet(FileZap);
n:=0;
while not EoF(FileZap) do
begin
n:=n+1;
read(FileZap,MZap[n]);
end;
SpinEdit1.Text:=IntToStr(n);
StringGrid1.RowCount:=n+1;
for i:=1 to n do
with StringGrid1,MZap[i] do
begin
Cells[0,i]:=fio;
Cells[1,i]:=dol;
Cells[2,i]:=IntToStr(year);
Cells[3,i]:=IntToStr(oklad);
end;
CloseFile(FileZap);
end;
end;
BitBtnSort.Show;
BitBtnSave.Show;
BitBtnSort2.Show;
BitBtnSort3.Show;
end;
procedure TForm1.BitBtnSaveClick(Sender: TObject);
var
i: integer;
begin
with SaveDialog1 do
if Execute then
begin
FileNameText:=FileName;
AssignFile(FileText,FileNameText);
ReWrite(FileText);
for i:=1 to n do
with MZap[i] do
writeln(FileText,fio:20,dol:15,year:4,oklad:7);
CloseFile(FileText);
end;
BitBtnSort.Hide;
end;
procedure TForm1.BitBtnSort2Click(Sender: TObject);
var
i,j :integer;
vper:zap;
begin
for i:=1 to n do
with StringGrid1,MZap[i] do
begin
fio:=Cells[0,i];
dol:=Cells[1,i];
year:=StrToInt(Cells[2,i]);
oklad:=StrToInt(Cells[3,i]);
end;
for i:=1 to n-1 do
for j:=i+1 to n do
if MZap[i].year>MZap[j].year then
begin
vper:=MZap[i];
MZap[i]:=MZap[j];
MZap[j]:=vper;
end;
for i:=1 to n do
with StringGrid1,MZap[i] do
begin
Cells[0,i]:=fio;
Cells[1,i]:=dol;
Cells[2,i]:=IntToStr(year);
Cells[3,i]:=IntToStr(oklad);
end;
end;
procedure TForm1.BitBtnSort3Click(Sender: TObject);
var
i,j :integer;
vper:zap;
begin
for i:=1 to n do
with StringGrid1,MZap[i] do
begin
fio:=Cells[0,i];
dol:=Cells[1,i];
year:=StrToInt(Cells[2,i]);
oklad:=StrToInt(Cells[3,i]);
end;
for i:=1 to n-1 do
for j:=i+1 to n do
if MZap[i].oklad>MZap[j].oklad then
begin
vper:=MZap[i];
MZap[i]:=MZap[j];
MZap[j]:=vper;
end;
for i:=1 to n do
with StringGrid1,MZap[i] do
begin
Cells[0,i]:=fio;
Cells[1,i]:=dol;
Cells[2,i]:=IntToStr(year);
Cells[3,i]:=IntToStr(oklad);
end;
end;
procedure TForm1.SpinEdit1Change(Sender: TObject);
var i,m: integer;
begin
m:=StrToInt(SpinEdit1.Text);
with StringGrid1 do
begin
RowCount:=m+1;
if m>n then
for i:=n+1 to m do
begin
Cells[0,i]:=IntToStr(i);
Cells[1,i]:='';
Cells[2,i]:='';
Cells[3,i]:='';
end;
end;
n:=m;
end;
initialization
{$I unit1.lrs}
end.