Inno Setup在Pascal Script中获取Min和Max Integer值 [英] Inno Setup get Min and Max Integer values in Pascal Script

查看:184
本文介绍了Inno Setup在Pascal Script中获取Min和Max Integer值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Pascal脚本中的许多情况下返回两个整数的最小值和最大值。但每次我需要创建 TStringList 时,将我的两个或更多整数添加到它并将其转换为 Array Of String 然后使用我的两个函数 ArrayOfStringMax ArrayOfStringMin获取其最小值和最大值。

I need to return min and max values of two Integers in many situations in my Pascal Script. But every time I need to create a TStringList, add my two or more Integers to it and convert it to an Array Of String and then get its min and max values using two of my functions called ArrayOfStringMax and ArrayOfStringMin.

我喜欢有两个函数,比如 Min Max 来做这个像Delphi中的单位数学更容易。

I like to have two functions like Min and Max to make this easier like unit Math in Delphi.

例如,

Log(IntToStr(Min(1000, 26)));

输出应为 26

Log(IntToStr(Max(45, 1989)));

输出应为 1989。

目前我只需要 Min Max 整数类型。如果函数可以返回最小值和最大值,即使 Double 扩展 Int64 类型,这将是一个非常有用的功能。

Currently I only need Min and Max for Integer Type. If a function can be made to return minimum and maximum values even of Single, Double, Extended, Int64 types, it will be a very useful function.

更新

procedure StringListToArrayOfString(StringList: TStringList; var ArrayOfString: Array Of String);
var
  X: Integer;
begin
  SetLength( ArrayOfString, StringList.Count);
  for X := 0  to (StringList.Count - 1)  do ArrayOfString[X] := StringList.Strings[X];
end;

function ArrayOfStringMax(ArrayOfString: Array of String): String;
var
  X, M: Integer;
begin
  M := StrToInt(ArrayOfString[Low(ArrayOfString)]);
  for X := 1 to High(ArrayOfString) do
  if StrToInt(ArrayOfString[X]) > M then M := StrToInt(ArrayOfString[X]);
  Result := IntToStr(M);
end;

function ArrayOfStringMin(ArrayOfString: Array of String): String;
var
  X, M: Integer;
begin
  M := StrToInt(ArrayOfString[Low(ArrayOfString)]);
  for X := 1 to High(ArrayOfString) do
  if StrToInt(ArrayOfString[X]) < M then M := StrToInt(ArrayOfString[X]);
  Result := IntToStr(M);
end;

这是我目前在剧本中使用的三个函数。

Those are the three functions I currently using in the Script.

提前致谢。

推荐答案

function Max(A, B: Integer): Integer;
begin
  if A > B then
    Result := A
  else
    Result := B;
end;

function Min(A, B: Integer): Integer;
begin
  if A < B then
    Result := A
  else
    Result := B;
end;

Inno Setup Pascal Script不支持通用功能,也不支持函数重载。因此,如果您需要为浮点函数实现该函数,则必须使用其他名称,例如 MinFloat MaxFloat 。实现将是相同的(显然除了参数和返回值类型)。

Inno Setup Pascal Script does not support generic function nor function overloading. So if you need to implement the function for floats, you have to use a different name like MinFloat, MaxFloat. The implementation will be identical (except for parameter and return value types obviously).

虽然您可以共享整数和浮点类型的实现。您可以对 Integer 类型使用 LongInt 实现。同样,您可以为 Single 类型使用 Double 实现。

Though you can share implementation for integer and float types. You can use a LongInt implementation for the Integer type. The same way, you can use a Double implementation for the Single type.

如果要实现数字数组的函数,则无需将数组转换为字符串。只需使用数组,整数数组

If you want to implement the functions for array of numbers, there's no need to convert the array to strings. Just use array of numbers, array of Integer:

function Max(N: array of Integer): Integer;
var
  I: Integer;
begin
  if GetArrayLength(N) = 0 then RaiseException('Array is empty');

  Result := N[Low(N)];
  for I := Low(N) + 1 to High(N) do
  begin
    if N[I] > Result then Result := N[I];
  end;
end;

这篇关于Inno Setup在Pascal Script中获取Min和Max Integer值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆