Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
otchet__fortunovoy_moy.doc
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
10.62 Mб
Скачать

Практическая работа №9.Работа с файлами

Цель работы: Разработать программы для работы с различными файлами.

Краткие теоретические сведения: программы созданы для более удобной работы с файлами. По способу доступа к информации, записанной в файл, различают файлы прямого и последовательного доступа. Файлом последовательного доступа называется файл, к элементам которго обеспечивается доступ в такой же последовательности, в какой они записывались. Как правило- это текстовые файлы.

Файлом прямого доступа называется файл, доступ к элементам которого осуществляется по адресу элемента. Как правило – это файлы баз данных.

Формулировка задания: Написать программ, для работы с файлами с помощью компанентов Button(командная кнопка),Label(метка для записи), Memo(многострочный текстовый редактор),StringGrid(ввод массива),OpenDialog(выбор файла с целью последующего открытия),GroupBox.

Задание №1.Дан текст, содержащий английский текст (текст считать из файла).Найти количество слов,начинающихся с буквы b (результат записать в файл).

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls;

type

TForm1 = class(TForm)

Label1: TLabel;

Button1: TButton;

Button2: TButton;

Button3: TButton;

Label2: TLabel;

Edit1: TEdit;

Label3: TLabel;

Button4: TButton;

Memo1: TMemo;

Memo2: TMemo;

procedure Button1Click(Sender: TObject);

procedure Button2Click(Sender: TObject);

procedure Button3Click(Sender: TObject);

procedure Button4Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

f:textfile;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

var m:array[0..100] of string[100];

i,j,n:integer;

s:string;

begin

j:=0;

with Memo1 do

begin

for n:=0 to lines.Count-1 do

begin

s:=lines[n]+' ';

for i:=1 to length (s) do

begin

m[j]:=m[j]+copy(s,i,1);

if (copy(s,i,1)=' ') then j:=j+1;

end;

end;

end;

Assignfile(f,'Текст.txt');

Rewrite(f);

for i:=0 to j do

writeln(f,m[i]);

closefile(f);

end;

procedure TForm1.Button2Click(Sender: TObject);

var s:string;

begin

reset(f,'Текст.txt');

readln(f,s);

Memo2.Lines[0]:=s;

while not eof(f) do

begin

readln(f,s);

Memo2.lines.Add(s);

end;

closefile(f);

end;

procedure TForm1.Button3Click(Sender: TObject);

var i,n,kol:integer; s:string;

begin

kol:=0;

with Memo2 do

begin

for n:=0 to lines.Count-1 do

begin

s:=lines[n];

for i:=1 to length(s) do

if copy(s,i,1)='b' then kol:=kol+1;

end;

end;

Edit1.Text:=inttostr(kol);

end;

procedure TForm1.Button4Click(Sender: TObject);

var F:textfile;

begin

Assignfile(f,'Количество.txt');

rewrite(f);

writeln(f,Edit1.Text);

closefile(f);

end;

end.

Задание №2. Дан двумерный массивразмером n*m.

1.Заменить минимальный по модулю элемент каждого столбца.

2.Вставить после каждой строки, содержащей минимальное значение строку из модулей.

3.Удалить все столбцы, в которых первый элемент больше последнего.

4.Поменять местами первый и последний столбцы.

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls, Grids

type

TForm1 = class(TForm)

StringGrid1: TStringGrid;

Button1: TButton;

Button2: TButton;

GroupBox1: TGroupBox;

RadioButton1: TRadioButton;

RadioButton2: TRadioButton;

RadioButton3: TRadioButton;

RadioButton4: TRadioButton;

OpenDialog1: TOpenDialog;

procedure Button1Click(Sender: TObject);

procedure Button2Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

a:array of array of integer;

n,m:integer;

nam, nam2 : string;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

var i,j:integer;

f:textfile;

begin

if OpenDialog1.Execute then

begin

AssignFile(f,OpenDialog1.FileName);

Reset(f);

readln(f,n);

readln(f,m);

SetLength(a,n,m);

Randomize;

SetLength(a,n,m);

for i:=0 to n-1 do

begin

for j:=0 to m-1 do

read(f,a[i,j]);

readln(f);

end;

closefile(f);

StringGrid1.RowCount:=n+1;

StringGrid1.ColCount:=m+1;

with StringGrid1 do

begin

i:=0;

for j:=1 to RowCount do

Cells[i,j]:=Inttostr(j);

j:=0;

for i:=1 to Colcount do

Cells[i,j]:=inttostr(i);

end;

with stringGrid1 do

for i:=1 to n do

for j:=1 to m do

cells[j,i]:=inttostr(a[i-1,j-1]);

end;

end;

procedure TForm1.Button2Click(Sender: TObject);

var i,j,k,min:integer;

b:boolean;

tmp:array of integer;

begin

if RadioButton1.Checked then

begin

for j:=0 to m-1 do

begin

min:=abs(a[0,j]);

for i := 1 to n-1 do

if (abs(a[i,j])<min) then min:=abs(a[i,j]);

for i := 0 to n-1 do

if (abs(a[i,j])=min) then a[i,j]:=0;

end;

with stringGrid1 do

for i:=1 to n do

for j:=1 to m do

cells[j,i]:=inttostr(a[i-1,j-1]);

end;

if (RadioButton2.Checked) then with StringGrid1 do

begin

min := a[0,0];

for i := 0 to n-1 do

for j := 0 to m-1 do

if (a[i,j]<min) then min:=a[i,j];

i:=1;

while (i<=n) do

begin

b:=false;

for j:=1 to m do

if (strtoint(cells[j,i])=min) then b:=true;

if b then

begin

n:=n+1;

StringGrid1.RowCount:=n+1;

for k:=n downto i do

Rows[k+1]:=Rows[k];

for j:=1 to m do

StringGrid1.cells[j,i+1]:='0';

i:=i+1;

end;

i:=i+1;

end;

end;

if RadioButton3.Checked then with StringGrid1 do

begin

j:=1;

while (j<=m) do

begin

b:=false;

if (cells[j,1]>cells[j,n]) then b:=true;

if b then

begin

m:=m-1;

ColCount:=m+1;

for k:=j to m do

Cols[k]:=Cols[k+1];

j:=j-1;

end;

j:=j+1;

end;

for i:=0 to n-1 do

for j:=0 to m-1 do

a[i,j]:=StrToInt(Cells[j+1,i+1]);

end;

if RadioButton4.Checked then with StringGrid1 do

begin

SetLength(tmp,n);

for i := 0 to n-1 do

begin

tmp[i] := a[i,0];

a[i,0] := a[i,m-1];

a[i,m-1] := tmp[i];

end;

with stringGrid1 do

for i:=1 to n do

for j:=1 to m do

cells[j,i]:=inttostr(a[i-1,j-1]);

end;

end;

end.

Задание №3. В проект, выполненный во втором задании, добавить вывод результатов программы в текстовый файл с комментариями.

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls, Grids,mmsystem;

type

TForm1 = class(TForm)

StringGrid1: TStringGrid;

Button1: TButton;

Button2: TButton;

GroupBox1: TGroupBox;

RadioButton1: TRadioButton;

RadioButton2: TRadioButton;

RadioButton3: TRadioButton;

RadioButton4: TRadioButton;

procedure Button1Click(Sender: TObject);

procedure FormCreate(Sender: TObject);

procedure Button2Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

a:array of array of integer;

n,m:integer;

nam, nam2 : string;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

var i,j:integer;

f:textfile;

s:string;

ch:char;

begin

{$I-}

AssignFile(f,nam);

Reset(f);

readln(f,n);

readln(f,m);

SetLength(a,n,m);

Randomize;

SetLength(a,n,m);

for i:=0 to n-1 do

begin

for j:=0 to m-1 do

read(f,a[i,j]);

readln(f);

end;

closefile(f);

StringGrid1.RowCount:=n+1;

StringGrid1.ColCount:=m+1;

with StringGrid1 do

begin

i:=0;

for j:=1 to RowCount do

Cells[i,j]:=Inttostr(j);

j:=0;

for i:=1 to Colcount do

Cells[i,j]:=inttostr(i);

end;

with stringGrid1 do

for i:=1 to n do

for j:=1 to m do

cells[j,i]:=inttostr(a[i-1,j-1]);

Closefile(f);

end;

procedure TForm1.FormCreate(Sender: TObject);

begin

nam:='a.txt';

nam2:='n.txt';

end;

procedure TForm1.Button2Click(Sender: TObject);

var i,j,k,min:integer;

b:boolean;

tmp:array of integer;

g:textfile;

begin

if RadioButton1.Checked then

begin

AssignFile(g,nam2);

Rewrite(g);

for j:=0 to m-1 do

begin

min:=abs(a[0][j]);

for i:=1 to n-1 do

if (abs(a[i,j])<min) then min:=abs(a[i,j]);

for i := 0 to n-1 do

if (abs(a[i,j])=min) then a[i,j]:=0;

end;

with stringGrid1 do

for i:=1 to n do

for j:=1 to m do

cells[j,i]:=inttostr(a[i-1,j-1]);

writeln(g,'Задание : Заменить минимальный по модулю элемент каждого столбца нулем');

for i:=0 to n-1 do

begin

for j:=0 to m-1 do

write(g,IntToStr(a[i,j])+' ');

writeln(g);

end;

closefile(g);

end;

if (RadioButton2.Checked) then with StringGrid1 do

begin

min := a[0,0];

for i := 0 to n-1 do

for j := 0 to m-1 do

if (a[i,j]<min) then min:=a[i,j];

i:=1;

while (i<=n) do

begin

b:=false;

for j:=1 to m do

if (strtoint(cells[j,i])=min) then b:=true;

if b then

begin

n:=n+1;

StringGrid1.RowCount:=n+1;

for k:=n downto i do

Rows[k+1]:=Rows[k];

for j:=1 to m do

StringGrid1.cells[j,i+1]:='0';

i:=i+1;

end;

i:=i+1;

end;

end;

if RadioButton3.Checked then with StringGrid1 do

begin

j:=1;

while (j<=m) do

begin

b:=false;

if (cells[j,1]>cells[j,n]) then b:=true;

if b then

begin

m:=m-1;

ColCount:=m+1;

for k:=j to m do

Cols[k]:=Cols[k+1];

j:=j-1;

end;

j:=j+1;

end;

for i:=0 to n-1 do

for j:=0 to m-1 do

a[i,j]:=StrToInt(Cells[j+1,i+1]);

end;

if RadioButton4.Checked then with StringGrid1 do

begin

AssignFile(g,nam2);

Rewrite(g);

SetLength(tmp,n);

for i:=0 to n-1 do

begin

tmp[i]:=a[i,0];

a[i,0]:=a[i,m-1];

a[i,m-1]:=tmp[i];

end;

with stringGrid1 do

for i:=1 to n do

for j:=1 to m do

cells[j,i]:=inttostr(a[i-1,j-1]);

writeln(g,'задание : Поменять местами первый и последний столбцы');

for i:=0 to n-1 do

begin

for j:=0 to m-1 do

write(g,IntToStr(a[i,j])+' ');

writeln(g);

end;

end;

closefile(g);

end;

end.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]