type TheMas = array [1..10] of array [1..10] of Integer; TForm1 = class(TForm) StringGrid1: TStringGrid; Edit1: TEdit; UpDown1: TUpDown; Label1: TLabel; Edit2: TEdit; UpDown2: TUpDown; Label2: TLabel; Button1: TButton; procedure GridChange(Sender: TObject); procedure Button1Click(Sender: TObject); procedure zap(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.GridChange(Sender: TObject); begin StringGrid1.ColCount:=UpDown1.Position; StringGrid1.RowCount:=UpDown2.Position; end;
procedure TForm1.Button1Click(Sender: TObject); var M:TheMas; i, j, k, p:Integer; begin for i:=1 to UpDown1.Position do for j:=1 to UpDown2.Position do M[i, j]:=StrToInt(StringGrid1.Cells[i-1, j-1]); k:=1; p:=M[1, 1]; for i:=1 to UpDown1.Position do for j:=2 to UpDown2.Position do if p>M[i, j] then begin k:=j; p:=M[i, j]; end; for i:=1 to UpDown1.Position do
begin
p:=M[i, 1]; M[i, k+1]:=M[i, 1];
M[i, j+1]:=p;
end;
for i:=1 to UpDown1.Position do for j:=1 to UpDown2.Position do StringGrid1.Cells[i-1, j-1]:=IntToStr(M[i, j]); end;
procedure TForm1.zap(Sender: TObject); var x,y:integer; begin randomize; for y:=0 to form1.StringGrid1.rowcount do for x:=0 to form1.StringGrid1.ColCount do form1.StringGrid1.Cells[x,y]:=inttostr(random(100)); end;
end.
7777
29.05.2008 5:41
Помогите, что не так сделал?
klem4
29.05.2008 13:03
Ну во-первых мне показалось очень странным вот это :
Цитата
for i:=1 to UpDown1.Position do for j:=2 to UpDown2.Position do
Во вторых зачем вообще массив ? Разьве что только для того чтобы постоянно не юзать StrToInt... В третьих как-то странно выглядит алгоритм вставки, особо правда не вникал, я бы сделал так:
Нашли jMin - номер строки в гриде в которой находится минимальный элемент.
Увеличили число строк в гриде на 1
Смещаем строки начиная с RowsCount - 1 (бывшей последней) до jMin + 1 включительно на одну строку в низ
Заполняем строку jMin + 1 элементами из строки 1
т.е.
было
1 2 3 4 5 0 7 8 9
стало
1 2 3 4 5 0 1 2 3 7 8 9
Вот так примерно.
Это текстовая версия — только основной контент. Для просмотра полной версии этой страницы, пожалуйста, нажмите сюда.