Описать процедуру 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.
|