11
Министерство образования и науки Российской Федерации
Курский государственный технический университет
Кафедра ПО ВТ
Изучение работы с файлами на языке Delphi
Выполнил: студент гр. ПО 62 Шиляков И.А.
Проверил: ст. преподаватель Петрик Е.А.
Курск
2006
Цель:
Изучение принципов работы с файлами на языке Delphi, получение навыков программирования с использованием файлов.
Задание:
Создать каталог продуктов.
Окно программы:
Используемые компоненты:
№
Имя компонента
Страница палитры компонентов
Настраиваемые компоненты
Значения
1
2
3
4
5
Form1
-
Сaption
Лабораторная работа №10
Color
clMoneyGreen
Bitbtn1
Additional
Caption
Удалить по наименованию
Label1
Standard
Введите продукты и их стоимость
Color (Font)
clNavy
Size (Font)
12
Charset (Font)
DEFAULT_CHARSET
Height (Font)
-16
Продолжение таблицы
Label2
Общая сумма
10
-13
Button1
Открыть
Charset
BALTIC_CHARSET
Height
-11
Size
8
6
Button2
Сохранить
7
Button3
Расчет
Name
MS Sans Serif
Button4
Добавить строку
9
Button5
Поиск
Edit1
Text
tt.text
Edit2
Edit3
13
Edit4
14
StringGrid1
FixesCols
0
FixesRows
RowCount
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;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
var
Form1: TForm1;
implementation
{$R *.dfm}
function poisk(istochnik,chto:s30):boolean;
var i,k:integer;
procedure poisk2(B,M:s30);
var k:integer; b1:boolean;
begin
i:=0;
while length(b)>=length(m)+i do
b1:=true;
for k:=1 to length(m) do begin
if m[k]=b[k+i] then else b1:=false;
if b1=true then begin poisk:=b1; exit; end; i:=i+1;
if length(istochnik)>=length(chto) then
poisk2(istochnik,chto)
else
poisk2(chto,istochnik);
procedure TForm1.Button2Click(Sender: TObject);
var i:integer; s:string; z:p;
f: file of p;
s:=edit1.Text;
system.Assign(f,s); // iacia?aaony eiy oaeeiaie ia?aiaiiie
rewrite(f); // niaaiea ionoiai oaeea, oeacaoaeu onoai. ia ioeaaie ye-o
for i:=1 to stringgrid1.RowCount do if (stringgrid1.Cells[0,i]<>'') then
z.tovar:=stringgrid1.cells[0,i];
z.money:=strtoint(stringgrid1.cells[1,i]);
z.kol:=strtoint(stringgrid1.cells[2,i]);
write(f,z); // caienu a oaee f caiene z
system.close(f);
procedure TForm1.Button1Click(Sender: TObject);
var i:integer; z:p;
i:=1;
system.Assign(f,edit1.Text);
reset(f); // ioe?uou oaee
while not eof(f) do
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;
procedure TForm1.FormCreate(Sender: TObject);
stringgrid1.Cells[0,0]:='Oiaa?';
stringgrid1.Cells[1,0]:='Noieiinu';
stringgrid1.Cells[2,0]:='Eie-ai';
stringgrid1.Cells[3,0]:='Eoiai';
procedure TForm1.Button3Click(Sender: TObject);
Var i:integer;
edit2.Text:='0';
for i:=1 to stringgrid1.RowCount-1 do
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));
procedure TForm1.BitBtn1Click(Sender: TObject);
repeat
if stringgrid1.Cells[0,i]=edit3.text then
k:=i;
while k<stringgrid1.RowCount-1 do
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;
stringgrid1.RowCount:=stringgrid1.RowCount-1;
until stringgrid1.RowCount-1<i;
procedure TForm1.Button4Click(Sender: TObject);
var i:integer;
stringgrid1.RowCount:=stringgrid1.RowCount+1;
for i:=0 to 4 do
stringgrid1.cells[i,stringgrid1.RowCount-1]:='';
procedure TForm1.Button5Click(Sender: TObject);
stringgrid1.RowCount:=2;
stringgrid1.Cells[0,1]:='Ia iaeaaii';
for i:=1 to 3 do
stringgrid1.Cells[i,1]:='';
while (not eof(f)) and (edit4.text<>'') do
if poisk(AnsiUpperCase(z.tovar),AnsiUpperCase(edit4.text)) then
end.
Тестирование: