Екі лшемді массивтерді деу программалары. StringGrid компоненті.

 

Задание :В целочисленной матрице А(4,4) поменять местами первую и последнюю строку.

Решение:

1.Создать новую папку в папке Мои документы.

2.Для создания нового проекта file/new/application.

3.Из страницы Standart палитры компонентов поместим на форму Form1 следующие компоненты: Button1, Button2, Button3. StringGrid1, StringGrid2.

 

.4. На форме Form1в Caption введем «Решение массива».К кнопкам Button1, Button2, Button3 в свойстве Caption присвоем соответственно «Решение1», «Решение2» «Очистить».

 

В инспекторе обьекта для общих таблиц выберем свойства Options. Войдем в него двойным щелчком. Значение флага doEditing=true

Для StringGrid1, StringGrid2 FixedCols=0

FixedRows=0

ColCount=4

RowCount=4

Форма программы.

 

 


 

5.Активизируем двойным щелчком на каждой кнопке и допищим следующие строки для каждой из них.

Button1:

procedure TForm1.Button1Click(Sender: TObject); var i:integer;

begin

for i:=0 to 3 do begin

StringGrid2.Cells[i,0]:=StringGrid1.Cells[i,3];

StringGrid2.Cells[i,3]:=StringGrid1.Cells[i,0];

StringGrid2.Cells[i,1]:=StringGrid1.Cells[i,1];

StringGrid2.Cells[i,2]:=StringGrid1.Cells[i,2]; end

end;

Button2:

procedure TForm1.Button2Click(Sender: TObject); begin StringGrid2.Rows[0]:=StringGrid1.Rows[3]; StringGrid2.Rows[1]:=StringGrid1.Rows[1]; StringGrid2.Rows[2]:=StringGrid1.Rows[2]; StringGrid2.Rows[3]:=StringGrid1.Rows[0];

 

end;

Button3

procedure TForm1.Button3Click(Sender: TObject); var i,j:integer;

begin

for i:=0 to 3 do for j:=0 to 3do

StringGrid2.Cells[j,i]:=' ' end;

6.Опищем массив unit Gulk; interface

 

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Grids;

type

 


TForm1 = class(TForm) StringGrid1: TStringGrid; StringGrid2: TStringGrid; Button1: TButton; Button2: TButton; Button3: TButton;

procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); private

{ Private declarations } public

{ Public declarations } end;

var

Form1: TForm1; implementation {$R *.dfm}

 

procedure TForm1.Button1Click(Sender: TObject); var i:integer;

begin

for i:=0 to 3 do begin

StringGrid2.Cells[i,0]:=StringGrid1.Cells[i,3];

StringGrid2.Cells[i,3]:=StringGrid1.Cells[i,0];

StringGrid2.Cells[i,1]:=StringGrid1.Cells[i,1];

StringGrid2.Cells[i,2]:=StringGrid1.Cells[i,2]; end

end;

procedure TForm1.Button2Click(Sender: TObject); begin StringGrid2.Rows[0]:=StringGrid1.Rows[3]; StringGrid2.Rows[1]:=StringGrid1.Rows[1]; StringGrid2.Rows[2]:=StringGrid1.Rows[2]; StringGrid2.Rows[3]:=StringGrid1.Rows[0];

 

end;

procedure TForm1.Button3Click(Sender: TObject); var i,j:integer;

begin

for i:=0 to 3 do for j:=0 to 3do

StringGrid2.Cells[j,i]:=' ' end;

end.

7. Сохраняем праграмму и запускаем командой Run.