in matrix(StringGrid)NxM按非遞減順序排序每一行的元素?Delphi排序字符串網格
var
Form1: TForm1;
n,m:integer;
I:integer;
implementation
{$R *.dfm}
procedure TForm1.btNapraviClick(Sender: TObject);
begin
with StringGrid1 do
begin
n:=StrToInt(edN.text)+1;
m:=StrToInt(edM.text)+1;
ColCount:=n;
RowCount:=m;
for I:=0 to n-1 do Cells[I,0]:=IntToStr(I);
for I:=1 to m-1 do Cells[0,I]:=IntToStr(I);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var J,P,K:integer;
begin
with StringGrid1 do
begin
for I:=1 to n do
for J:=1 to m-1 do
for K:=J+1 to m do
begin
if StrToInt(Cells[I,J]) <= StrToInt(Cells[I,K]) then
begin
P:=StrToInt(Cells[I,J]);
Cells[I,J]:=(Cells[I,K]);
Cells[I,K]:=IntToStr(P);
end;
end;
end;
end;
不知道你的問題是什麼。你爲什麼要測試'<='而不是'<?如果它們相等,則不必要地交換兩個相等的值。此外,沒有理由讓P成爲一個整數或做一個轉換;只是把它做成一個字符串。你知道你沒有定義一個變量'I:Integer;'?我還會使用調試器來確保m和n不是基於零的,並且在這些內部循環中沒有錯誤的錯誤。 –
你是對的<,我的錯。但是我把declard看作是整數,就像全局變量一樣。 – Malone