{整数求和函数,使用指定类型的开放数组}
function Fun1(arr: array of Integer): Integer;
var
  n: Integer;
begin
  Result := 0;
  for n in arr do Inc(Result, n);
end;

{整数、浮点数或布尔值的求和函数,使用无类型的开放数组}
function Fun2(arr: array of const): Double;
var
  i: Integer;
begin
  Result := 0;
  for i := Low(arr) to High(arr) do
  begin
    case arr[i].VType of
      vtInteger: Result := Result + arr[i].VInteger;
      vtExtended: Result := Result + arr[i].VExtended^;
      vtBoolean: Result := Result + Integer(arr[i].VBoolean);
    end;
  end;
end;

{测试}
procedure TForm1.FormCreate(Sender: TObject);
var
  n1,n2: Integer;
  f1,f2: Double;
begin
  n1 := Fun1([1, 2, 3]);                   //6
  n2 := Fun1([1, 2, 3, 4, 5, 6, 7, 8, 9]); //45

  f1 := Fun2([1, 2, 3]);                   //6.00
  f2 := Fun2([True, 1.1, 2.2, 3.3]);       //7.60

  ShowMessageFmt('%d, %d, %f, %f', [n1, n2, f1, f2]);
end;

posted on 2011-04-14 10:50  万一  阅读(2379)  评论(0编辑  收藏  举报