type TA = array [0..9, 0..9] of Integer;//这个函数是安全的,但是也是游缺点的 //他只能处理 TA 类型的数组 //也就是说,这种形式的函数定义,只能是有类型的数组作为参数 //不能传入任意维数的数组 procedure ArrAbs(var a: ta); overload; var i, j: Integer; begin for i := 0 to 9 do for j := 0 to 9 do a[i, j] := Abs(a[i, j]); end;//这个函数可以处理任意维数的数组 //因为正常的多维数组时在内存中是连续存放的, //只要知道首地址和数组总长度就可以访问到数组中的内容 //当然,这样的函数,因为没有约束,是很危险的 //如果给的数组长度过长,就会造成越界访问 procedure ArrAbs(P: PInteger; Len: Integer); overload; var i: Integer; begin for i := 0 to Len - 1 do begin p^ := Abs(P^); Inc(P); end; end;procedure TForm1.Button1Click(Sender: TObject); var i, j: Integer; a, b: ta; n: Integer; begin //构造两一样的数组 Randomize; for i := 0 to 9 do for j := 0 to 9 do begin if Random(10) > 5 then a[i, j] := Random(100) else a[i, j] := -Random(100); b[i, j] := a[i, j]; end; //使用两个不同的函数分别处理两个数组 ArrAbs(A); ArrAbs(@b[0, 0], SizeOf(b) div SizeOf(Integer)); //对比处理后的结果 n := 0; for i := 0 to 9 do for j := 0 to 9 do n := n + a[i, j]; Caption := IntToStr(n); n := 0; for i := 0 to 9 do for j := 0 to 9 do n := n + b[i, j]; Caption := Caption + ' ' + IntToStr(n) end;
6楼的算法不错,谢谢啦,现在有另一个问题:unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Grids, StdCtrls;type TForm1 = class(TForm) Button1: TButton; StringGrid1: TStringGrid; Edit1: TEdit; Edit2: TEdit; procedure Button1Click(Sender: TObject); procedure GetData(mStringGrid:TStringGrid;Index:Integer;var Arr:array of Real); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation {$R *.dfm}procedure TForm1.GetData(mStringGrid:TStringGrid;Index:Integer;var Arr:array of Real); var I:Integer; begin For I:=1 to mStringGrid.RowCount-1 do if mStringGrid.Cells[Index,I]<>'' then arr[i]:=StrToFloat(mStringGrid.Cells[Index,I]); end;procedure TForm1.Button1Click(Sender: TObject); var arr:array of Real; begin GetData(StringGrid1,1,arr); Edit1.Text:=FloatToStr(high(arr)); Edit2.Text:=FloatToStr(low(arr)); end; end. 为什么这段程序的代码会报错,在arr[i]:=StrToFloat(mStringGrid.Cells[Index,I])行中,我调试了发现 mStringGrid.Cells[Index,I])是有值的,如'1',但是StrToFloat(mStringGrid.Cells[Index,I])这个却是‘不可接受的值’,老报内存错误,Help!
For I:=1 to mStringGrid.RowCount-1 do,这个I很可能对于arr越界了
TarrReal = array of Real; 最好自己定义个类型 来记录这个数组 那么就可以把他当成一般的变量来处理
同意楼上说的:)要最简单的办法就是自定义一个类型 type myArray = array of Real; function TForm1.ABSArray(oArray: myArray): myArray; var i: Integer; dArray:myArray; begin SetLength(dArray,Length(oArray)); for i := Low(oArray) to High(oArray) do dArray[i] := Abs(oArray[i]); result:= dArray; end;procedure TForm1.btn1Click(Sender: TObject); var p,o:myArray; begin SetLength(o,3); o[0]:=1; o[1]:=-88; o[2]:=99; p:=ABSArray(o); ShowMessage(FloatToStr(p[1])); end;
TA = array [0..9, 0..9] of Integer;//这个函数是安全的,但是也是游缺点的
//他只能处理 TA 类型的数组
//也就是说,这种形式的函数定义,只能是有类型的数组作为参数
//不能传入任意维数的数组
procedure ArrAbs(var a: ta); overload;
var
i, j: Integer;
begin
for i := 0 to 9 do
for j := 0 to 9 do
a[i, j] := Abs(a[i, j]);
end;//这个函数可以处理任意维数的数组
//因为正常的多维数组时在内存中是连续存放的,
//只要知道首地址和数组总长度就可以访问到数组中的内容
//当然,这样的函数,因为没有约束,是很危险的
//如果给的数组长度过长,就会造成越界访问
procedure ArrAbs(P: PInteger; Len: Integer); overload;
var
i: Integer;
begin
for i := 0 to Len - 1 do
begin
p^ := Abs(P^);
Inc(P);
end;
end;procedure TForm1.Button1Click(Sender: TObject);
var
i, j: Integer;
a, b: ta;
n: Integer;
begin
//构造两一样的数组
Randomize;
for i := 0 to 9 do
for j := 0 to 9 do
begin
if Random(10) > 5 then
a[i, j] := Random(100)
else
a[i, j] := -Random(100);
b[i, j] := a[i, j];
end; //使用两个不同的函数分别处理两个数组
ArrAbs(A);
ArrAbs(@b[0, 0], SizeOf(b) div SizeOf(Integer)); //对比处理后的结果
n := 0;
for i := 0 to 9 do
for j := 0 to 9 do
n := n + a[i, j]; Caption := IntToStr(n);
n := 0;
for i := 0 to 9 do
for j := 0 to 9 do
n := n + b[i, j]; Caption := Caption + ' ' + IntToStr(n)
end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
StringGrid1: TStringGrid;
Edit1: TEdit;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
procedure GetData(mStringGrid:TStringGrid;Index:Integer;var Arr:array of Real);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation
{$R *.dfm}procedure TForm1.GetData(mStringGrid:TStringGrid;Index:Integer;var Arr:array of Real);
var
I:Integer;
begin
For I:=1 to mStringGrid.RowCount-1 do
if mStringGrid.Cells[Index,I]<>'' then
arr[i]:=StrToFloat(mStringGrid.Cells[Index,I]);
end;procedure TForm1.Button1Click(Sender: TObject);
var
arr:array of Real;
begin
GetData(StringGrid1,1,arr);
Edit1.Text:=FloatToStr(high(arr));
Edit2.Text:=FloatToStr(low(arr));
end;
end.
为什么这段程序的代码会报错,在arr[i]:=StrToFloat(mStringGrid.Cells[Index,I])行中,我调试了发现
mStringGrid.Cells[Index,I])是有值的,如'1',但是StrToFloat(mStringGrid.Cells[Index,I])这个却是‘不可接受的值’,老报内存错误,Help!
最好自己定义个类型 来记录这个数组
那么就可以把他当成一般的变量来处理
type
myArray = array of Real; function TForm1.ABSArray(oArray: myArray): myArray;
var
i: Integer;
dArray:myArray;
begin
SetLength(dArray,Length(oArray));
for i := Low(oArray) to High(oArray) do
dArray[i] := Abs(oArray[i]);
result:= dArray;
end;procedure TForm1.btn1Click(Sender: TObject);
var p,o:myArray;
begin
SetLength(o,3);
o[0]:=1;
o[1]:=-88;
o[2]:=99;
p:=ABSArray(o);
ShowMessage(FloatToStr(p[1]));
end;
这孩子。
动态数组是需要先分配内存才能使用的。
setlength(arr,100);