Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Отчёт по делфи.docx
Скачиваний:
8
Добавлен:
22.09.2019
Размер:
722.65 Кб
Скачать

4)Тема: « Работа с таблицами. Компонент tStringGrid»

Ввести в компонент StringGrid1 исходные числа, используя функцию InputBox(). В компонент StringGrid2 в соответствующие клетки занести отрицательные числа, если таких нет в числе , то – ноль.. Ввод, формирование и очистка матриц выполняется в соответствии с пунктами меню. По включенным флажкам вывести на форму MAX и MIN элементы

unit Unit1;

procedure Button1Click(Sender: TObject);

procedure Button2Click(Sender: TObject);

procedure Button3Click(Sender: TObject);

procedure N2Click(Sender: TObject);

procedure N1Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);

var n,m,k,w:byte;i,j,max,min,kol,kol1:integer;t:string;

a:array[1..1000]of integer;

begin

n:=strtoint(inputbox('Ввод','введите количество строк','0'));

m:=strtoint(inputbox('Ввод','введите количество столбцов','0'));

sg1.RowCount:=n;

sg1.ColCount:=m;

sg2.RowCount:=n;

sg2.ColCount:=m;

for i:=0 to n-1 do

for j:=0 to m-1 do

sg1.cells[j,i]:=inputbox('Ввод','введите '+inttostr(i)+inttostr(j)+' элемент таблицы','0');

kol:=0;

for i:=0 to n-1 do

for j:=0 to m-1 do begin

kol:=0;

t:=sg1.cells[j,i];

k:=length(t);

for w:=1 to k do

if t[w]<'0' then

inc(kol);

if kol=0 then sg2.cells[j,i]:='0' else

sg2.cells[j,i]:=inttostr(kol);

end;

kol1:=0;

for j:=0 to m-1 do begin

kol1:=0;

for i:=0 to n-1 do

if sg2.cells[j,i]='0' then inc(kol1);

listbox1.items.add(inttostr(kol1));

end;

if RadioButton1.checked=true then begin

max:=strtoint(sg1.cells[0,0]);

for i:=0 to n-1 do

for j:=0 to m-1 do

if strtoint(sg1.cells[j,i])>max then max:=strtoint(sg1.cells[j,i]);

label3.caption:=inttostr(max);

end;

if RadioButton2.checked=true then begin

min:=strtoint(sg1.cells[0,0]);

for i:=0 to n-1 do

for j:=0 to m-1 do

if strtoint(sg1.cells[j,i])<min then min:=strtoint(sg1.cells[j,i]);

label4.Caption:=inttostr(min);

end;

end;

procedure TForm1.Button2Click(Sender: TObject);

var j,i:byte;

begin

label3.caption:='';

label4.caption:='';

for j:=0 to sg1.colcount-1 do

for i:=0 to sg1.rowcount-1 do

sg1.Cells[j,i]:='';

for j:=0 to sg2.colcount-1 do

for i:=0 to sg2.rowcount-1 do

sg2.Cells[j,i]:='';

end;

procedure TForm1.Button3Click(Sender: TObject);

begin

close;

end;

procedure TForm1.N2Click(Sender: TObject);

var i,j,n,m:integer; f:textfile;k,t:string;

begin

if opendialog1.execute=true then

begin

k:=opendialog1.FileName;

assignfile(f,k);

reset(f);

readln(f,n);

sg1.Rowcount:=n;

readln(f,m);

sg1.ColCount:=m;

for i:=0 to n-1 do

for j:=0 to m-1 do begin

readln(f,k);

sg1.Cells[j,i]:=k;end;

closefile(f);

end;

end;

procedure TForm1.N1Click(Sender: TObject);

var i,j,n,m:byte; f:textfile;k,t:string;

begin

if savedialog1.execute=true then

begin

k:=savedialog1.FileName;

assignfile(f,k);

rewrite(f);

writeln(f,sg1.rowcount);

writeln(f,sg1.colcount);

for i:=0 to sg1.rowcount-1 do

for j:=0 to sg1.colcount-1 do

writeln(f,sg1.Cells[j,i]);

closefile(f);

end;end;

end.