Реферат: Изучение работы с файлами на языке Delphi
Министерствообразования и науки Российской Федерации
Курскийгосударственный технический университет
Кафедра ПО ВТ
Изучениеработы с файлами на языке Delphi
Выполнил: студент гр. ПО62 Шиляков И.А.
Проверил: ст.преподаватель Петрик Е.А.
Курск
2006
Цель:
Изучение принципов работыс файлами на языке Delphi, получениенавыков программирования с использованием файлов.
Задание:
Создать каталогпродуктов.
Окно программы:
/>
Используемыекомпоненты:
№ Имя компонента Страница палитры компонентов Настраиваемые компоненты Значения 1 2 3 4 5 1 Form1 - Сaption Лабораторная работа №10 Color clMoneyGreen 2 Bitbtn1 Additional Caption Удалить по наименованию 3 Label1 Standard Caption Введите продукты и их стоимость Color clMoneyGreen Color (Font) clNavy Size (Font) 12 Charset (Font) DEFAULT_CHARSET Height (Font) -16Продолжение таблицы
1 2 3 4 5 4 Label2 Standard Caption Общая сумма Color clMoneyGreen Color (Font) clNavy Size (Font) 10 Charset (Font) DEFAULT_CHARSET Height (Font) -13 5 Button1 Standard Caption Открыть Charset BALTIC_CHARSET Height -11 Size 8 6 Button2 Standard Caption Сохранить Charset BALTIC_CHARSET Height -11 Size 8 7 Button3 Standard Caption Расчет Charset DEFAULT_CHARSET Height -11 Size 8 Name MS Sans Serif 8 Button4 Standard Caption Добавить строку Charset DEFAULT_CHARSET Height -11 Size 8 Name MS Sans Serif 9 Button5 Standard Caption Поиск Charset DEFAULT_CHARSET Height -11 Size 8 Name MS Sans Serif 10 Edit1 Standard Text tt.text 11 Edit2 Standard Text 12 Edit3 Standard Text 13 Edit4 Standard Text 14 StringGrid1 Additional FixesCols FixesRows 1 RowCount 11 ColCount 16 Options [goEditing, goTab] FixedColor clSkyBlueТекстпрограммы:
unit Unit1;
interface
uses
Windows,Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,StdCtrls, Grids, Buttons;
type
s30=string[30];
P = record
tovar:string[30];
money:integer;
kol: integer;
itogo:integer;
end;
TForm1 =class(TForm)
StringGrid1:TStringGrid;
Button1:TButton;
Button2:TButton;
Edit1: TEdit;
Label1:TLabel;
Button3:TButton;
Edit2: TEdit;
Label2:TLabel;
BitBtn1:TBitBtn;
Edit3: TEdit;
Button4:TButton;
Button5:TButton;
Edit4: TEdit;
procedureButton2Click(Sender: TObject);
procedureButton1Click(Sender: TObject);
procedureFormCreate(Sender: TObject);
procedureButton3Click(Sender: TObject);
procedureBitBtn1Click(Sender: TObject);
procedureButton4Click(Sender: TObject);
procedureButton5Click(Sender: TObject);
private
{ Privatedeclarations }
public
{ Publicdeclarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
functionpoisk(istochnik,chto:s30):boolean;
vari,k:integer;
procedurepoisk2(B,M:s30);
var k:integer;b1:boolean;
begin
i:=0;
whilelength(b)>=length(m)+i do
begin
b1:=true;
for k:=1 tolength(m) do begin
if m[k]=b[k+i]then else b1:=false;
end;
if b1=truethen begin poisk:=b1; exit; end; i:=i+1;
end;
end;
begin
iflength(istochnik)>=length(chto) then
poisk2(istochnik,chto)
else
poisk2(chto,istochnik);
end;
procedureTForm1.Button2Click(Sender: TObject);
var i:integer;s:string; z:p;
f: file of p;
begin
s:=edit1.Text;
system.Assign(f,s); // íàçíà÷àåòñÿèìÿ ôàéëîâîéïåðåìåííîé
rewrite(f); // ñîäàíèå ïóñòîãîôàéëà, óêàçàòåëüóñòàí. íà íóëåâîéýë-ò
for i:=1 tostringgrid1.RowCount do if (stringgrid1.Cells[0,i]<>'') then
begin
z.tovar:=stringgrid1.cells[0,i];
z.money:=strtoint(stringgrid1.cells[1,i]);
z.kol:=strtoint(stringgrid1.cells[2,i]);
write(f,z); // çàïèñü â ôàéëf çàïèñè z
end;
system.close(f);
end;
procedureTForm1.Button1Click(Sender: TObject);
var i:integer;z:p;
f: file of p;
begin
i:=1;
system.Assign(f,edit1.Text);
reset(f); // îòêðûòü ôàéë
while noteof(f) do
begin
read(f,z);
stringgrid1.cells[0,i]:=z.tovar;
stringgrid1.cells[1,i]:=inttostr(z.money);
stringgrid1.cells[2,i]:=inttostr(z.kol);
stringgrid1.RowCount:=i+1;
i:=i+1;
end;
system.close(f);
end;
procedureTForm1.FormCreate(Sender: TObject);
begin
stringgrid1.Cells[0,0]:='Òîâàð';
stringgrid1.Cells[1,0]:='Ñòîèìîñü';
stringgrid1.Cells[2,0]:='Êîë-âî';
stringgrid1.Cells[3,0]:='Èòîãî';
end;
procedureTForm1.Button3Click(Sender: TObject);
Var i:integer;
begin
edit2.Text:='0';
for i:=1 tostringgrid1.RowCount-1 do
begin
stringgrid1.cells[3,i]:=Floattostr(strtoint(stringgrid1.Cells[1,i])*(strtoint(stringgrid1.Cells[2,i])));
edit2.Text:=floattostr(strtofloat(stringgrid1.cells[3,i])+strtofloat(edit2.text));
end;
end;
procedureTForm1.BitBtn1Click(Sender: TObject);
vari,k:integer;
begin
i:=1;
repeat
ifstringgrid1.Cells[0,i]=edit3.text then
begin
k:=i;
whilek<stringgrid1.RowCount-1 do
begin
stringgrid1.Cells[0,k]:=stringgrid1.Cells[0,k+1];
stringgrid1.Cells[1,k]:=stringgrid1.Cells[1,k+1];
stringgrid1.Cells[2,k]:=stringgrid1.Cells[2,k+1];
stringgrid1.Cells[3,k]:=stringgrid1.Cells[3,k+1];
k:=k+1;
end;
stringgrid1.RowCount:=stringgrid1.RowCount-1;
end;
i:=i+1;
untilstringgrid1.RowCount-1<i;
end;
procedureTForm1.Button4Click(Sender: TObject);
var i:integer;
begin
stringgrid1.RowCount:=stringgrid1.RowCount+1;
for i:=0 to 4do
stringgrid1.cells[i,stringgrid1.RowCount-1]:='';
end;
procedureTForm1.Button5Click(Sender: TObject);
var i:integer;z:p;
f: file of p;
begin
system.Assign(f,edit1.Text);
reset(f); // îòêðûòü ôàéë
stringgrid1.RowCount:=2;
stringgrid1.Cells[0,1]:='Íåíàéäåíî';
for i:=1 to 3do
stringgrid1.Cells[i,1]:='';
i:=1;
while (noteof(f)) and (edit4.text<>'') do
begin
read(f,z);
ifpoisk(AnsiUpperCase(z.tovar),AnsiUpperCase(edit4.text)) then
begin
stringgrid1.cells[0,i]:=z.tovar;
stringgrid1.cells[1,i]:=inttostr(z.money);
stringgrid1.cells[2,i]:=inttostr(z.kol);
stringgrid1.RowCount:=i+1;
i:=i+1;
end;
end;
system.close(f);
end;
end.
Тестирование:
/>
/>
/>
/>