![]() |
|
|
|||||||
| Регистрация | << Правила форума >> | FAQ | Пользователи | Календарь | Поиск | Сообщения за сегодня | Все разделы прочитаны |
![]() |
|
|
Опции темы | Поиск в этой теме | Опции просмотра |
|
#1
|
|||
|
|||
|
Я не прошу писать комментарии к каждой строчке, просто хотя бы в общем...
условие: В произвольном массиве поменяйте местами соседние четные и нечетные по номеру элементы. Дополнительные массивы не использовать. Код:
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;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure StringGrid1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
temp,temp1:string;
begin
if(StringGrid1.ColCount mod 2 = 1) then
temp1 := StringGrid1.Cells[StringGrid1.colcount-1,0];
for i := 0 to stringgrid1.ColCount div 2 do begin
temp := stringgrid1.Cells[2*i,0];
StringGrid1.Cells[2*i,0]:= StringGrid1.Cells[2*i+1,0];
StringGrid1.Cells[2*i+1,0] := temp;
end;
if(StringGrid1.ColCount mod 2 = 1) then
StringGrid1.Cells[StringGrid1.colcount-1,0] := temp1;
end;
procedure TForm1.Button2Click(Sender: TObject);
var i:integer;
begin
randomize;
for i:= 0 to StringGrid1.ColCount-1 do
StringGrid1.Cells[i,0] := inttostr(random(128));
end;
end. |
|
#2
|
|||
|
|||
|
помогите кто нибудь, я знаю это не сложно...
|
|
#3
|
||||
|
||||
|
Как то так...
Код:
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;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure StringGrid1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
temp,temp1:string;
begin
//Если число столбцов не четное то
if(StringGrid1.ColCount mod 2 = 1) then
//переменной приравнивается последняя ячейка первого столбца
temp1 := StringGrid1.Cells[StringGrid1.colcount-1,0];
//Кажый второй столбец
for i := 0 to stringgrid1.ColCount div 2 do begin
//Стандартный алгоритм обмена позициями
//a=1 b=2 -->c:=a, a:=b, b:=c-->a=2, b=1
temp := stringgrid1.Cells[2*i,0];//c:=a
StringGrid1.Cells[2*i,0]:= StringGrid1.Cells[2*i+1,0];//a:=b
StringGrid1.Cells[2*i+1,0] := temp;//b:=c
end;
//Если число столбцов не четное то
if(StringGrid1.ColCount mod 2 = 1) then
//последней ячейке первого столбца приравнять переменную
StringGrid1.Cells[StringGrid1.colcount-1,0] := temp1;
end;
procedure TForm1.Button2Click(Sender: TObject);
var i:integer;
begin
//Генератор случайный чисел
randomize;
//Первый столбец заполнить случайными до 128
for i:= 0 to StringGrid1.ColCount-1 do
StringGrid1.Cells[i,0] := inttostr(random(128));
end;
end. |
|
#4
|
|||
|
|||
|
Описать процедуру MoveLeft (A,N,K) (MoveRight (A,N,K)), осуществляющую циклический сдвиг элементов вещественного массива A размера N на k позиций влево (вправо) (0<k<5,k<N). Массив A- входной и выходнолй параметр, N и k входные параметры. С помощью этой процедуры осуществить сдвиг элементов данного массива размера N на k1 позиций, а затем - сдвиг элементов полученного массива на k2 позиций (k1и k2даны). После каждого вызова процедуры выводить на экран результирующий массив.
Код:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
StringGrid1: TStringGrid;
Button4: TButton;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label2: TLabel;
Edit2: TEdit;
Label3: TLabel;
Edit3: TEdit;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure moveleft(var A:array of real; n,k:integer);
var iq, jq,i,j:integer;
temp, temp2 : array of real;
begin
iq := k mod n; // k
jq := n - iq; // n-k
setlength(temp,iq);
setlength(temp2,jq);
for I := 0 to iq - 1 do
temp[i] := A[i];
for I := 0 to jq - 1 do
temp2[i] := A[i+iq];
for I := 0 to jq - 1 do
A[i] := temp2[i];
for I := 0 to iq - 1 do
A[i+jq] := temp[i];
end;
procedure moveright(var A:array of real; n,k:integer);
var iq, jq,i,j:integer;
temp, temp2 : array of real;
begin
iq := k mod n; // k
jq := n - iq; // n-k
setlength(temp,iq);
setlength(temp2,jq);
for I := 0 to iq - 1 do
temp[i] := A[i+jq];
for I := 0 to jq - 1 do
temp2[i] := A[i];
for I := 0 to jq - 1 do
A[i+iq] := temp2[i];
for I := 0 to iq - 1 do
A[i] := temp[i];
end;
procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
begin
randomize;
StringGrid1.ColCount := strtoint(edit1.text);
for I := 0 to StringGrid1.ColCount - 1 do
StringGrid1.Cells[i,0] := floattostr(round(random*255));
end;
procedure TForm1.Button2Click(Sender: TObject);
var a,b,c: array of real;
a1,b1,c1,i: integer;
begin
setlength(a,StringGrid1.ColCount);
for I := 0 to StringGrid1.ColCount - 1 do
a[i] := strtofloat(StringGrid1.cells[i,0]);
// moveleft(a,StringGrid1.ColCount, strtoint(edit2.text));
moveright(a,StringGrid1.ColCount, strtoint(edit3.text));
for I := 0 to StringGrid1.ColCount - 1 do
stringgrid1.Cells[i,0] := floattostr(a[i]);
end;
procedure TForm1.Button4Click(Sender: TObject);
var a,b,c: array of real;
a1,b1,c1,i: integer;
begin
setlength(a,StringGrid1.ColCount);
for I := 0 to StringGrid1.ColCount - 1 do
a[i] := strtofloat(StringGrid1.cells[i,0]);
moveleft(a,StringGrid1.ColCount, strtoint(edit2.text));
// moveright(a,StringGrid1.ColCount, strtoint(edit3.text));
for I := 0 to StringGrid1.ColCount - 1 do
stringgrid1.Cells[i,0] := floattostr(a[i]);
end;
end. |
|
#5
|
|||
|
|||
завтра сдавать, а у меня полный пипец... |
|
#6
|
|||
|
|||
|
Код:
var
A : Array Of Double;
...
// На одну влево
procedure ShiftArrayLeft1(var A : Array Of Double);
var
Buf : Double;
I : Integer;
begin
Buf := A[Low(A)];
For I := Low(A) To High(A)-1 Do
A[i] := A[I+1];
A[High(A)] := Buf;
end;по приведенной процедуре сама напишешь сбвиг вправо на 1 шаг, ну и еп K шагов - вызов сдвига на 1 шаг K раз. Да, в данном случае размер массива определяется автоматически и указывать его нет необходимости. |