全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1477
推到 Plurk!
推到 Facebook!

如何取出集合或列舉形態的值

尚未結案
hkstm
一般會員


發表:23
回覆:31
積分:10
註冊:2002-03-15

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-09-10 00:13:18 IP:218.165.xxx.xxx 未訂閱
請問一下各位先進… 假如是集合形態的值set of [3,5,8]之類的… 怎麼單單取出3或5或8等值表現出來…列舉形態的也是用同樣的方法嗎? 謝謝。
hagar
版主


發表:143
回覆:4056
積分:4445
註冊:2002-04-14

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-09-10 09:16:14 IP:202.39.xxx.xxx 未訂閱
{   The code in this example was written by Dan Donoghue and may be freely distributed provided the author remains credited. This example demonstrates a procedure for iterating through any set and calling a procedure for each element found in it. The procedure, ForEach, is defined as follows.. procedure ForEach(const SetVar; DoProc: TForEachMethod); Your callback method/procedure, DoProc, must have the following parameter list.. procedure(Element: Integer; var Continue: Boolean); is the integer representation of the option which is currently active in your set. You can typecast this member so that u can use it like your option enumeration type. For example.. TOption(Element) specifies if you want to continue iterating through the set. This member is defaulted to True. This is not a complete example. It merely shows snippits of code to demonstrate how to use the ForEach function to iterate the members of a set. Please Note; the typinfo unit is here only for use within the ForEach callback procedure to quickly look up the name of the option that is in the set for display purposes. } // Example Snippit //
uses                        // additional units required for this example
  typinfo;    type                        // Types used by example and ForEach procedure
  TOption = (opt1, opt2, opt3, opt4, opt5);
  TOptionSet = set of TOption;
  TForEachMethod = procedure(Element: Integer; var Continue: Boolean);    // The foreach procedure
procedure ForEach(const SetVar; DoProc: TForEachMethod);
var
  FContinue: Boolean;
  FSet, i: Byte;    begin
  FSet := Byte(SetVar);
  FContinue := True;
  for i := low(FSet) to high(fset) do
  begin
    if (FSet and 1) = 1 then
    begin
      if Assigned(DoProc) then DoProc(i,FContinue);
      if not FContinue then break;
    end;
    FSet := FSet shr 1;
  end;
end;    // example callback procedure for ForEach
procedure MyTestProc(Element: Integer; var Continue: Boolean);
begin
  Continue := (Element < 2);
  ShowMessage(IntToStr(Element)  ': '   GetEnumName(TypeInfo(TOption),Element));
end;    // procedure showing how to initiate ForEach
procedure SomeTestProc;
var
  FOptionSet: TOptionSet;
begin
  FOptionSet := [opt2, opt3, opt4];
  foreach(FOptionSet,MyTestProc);
end;
--- 歡迎光臨 KTop 研究院--<-<-<@
jow
尊榮會員


發表:66
回覆:751
積分:1253
註冊:2002-03-13

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-09-10 10:49:27 IP:211.74.xxx.xxx 未訂閱
集合問題最好用集合方式解決, 以樂透彩為例,您可以 寫一些關於集合運算的方法來處理相關需求,以下提供 個人的一些經驗:    
 
type
  TLotto = 1..42;
  TLottoSet = set of TLotto;
  TLottos = array of TLotto;      TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  public
    function ElementCount(ASet: TLottoSet): Integer;
    function Elements(ASet: TLottoSet): TLottos;
    function RandomLotto: TLotto;
    function RandomLottoSet(EC: Integer): TLottoSet;
    function LottoSetToString(ASet: TLottoSet): string;
    function LottoSetToLottos(ASet: TLottoSet): TLottos;
    function LottosToLottoSet(LS: TLottos): TLottoSet;
  end;    var
  Form1: TForm1;    function TForm1.ElementCount(ASet: TLottoSet): Integer;
var
  L: TLotto;
begin
  Result := 0;
  for L := Low(L) to High(L) do
  begin
    if L in ASet then
    begin
      Inc(Result);
      ASet := ASet - [L];
    end;
    if ASet = [] then Break;
  end;
end;    function TForm1.Elements(ASet: TLottoSet): TLottos;
var
  I, EC: Integer;
  L: TLotto;
begin
  EC := ElementCount(ASet);
  if EC = 0 then Result := nil
  else begin
    SetLength(Result, EC);
    I := 0;
    for L := Low(L) to High(L) do
    if L in ASet then
    begin
      Result[I] := L;
      Inc(I);
      if I = EC then Break;
    end;
  end;
end;    function TForm1.RandomLotto: TLotto;
begin
  Result := Random(42)   1;        //Simple random procedure
end;    function TForm1.RandomLottoSet(EC: Integer): TLottoSet;
begin
  Result := [];
  if EC > 0 then
    repeat
      Result := Result   [RandomLotto];
    until ElementCount(Result) = EC;
end;    function TForm1.LottoSetToString(ASet: TLottoSet): string;
var
  I: Integer;
  Lottos: TLottos;
begin
  Lottos := LottoSetToLottos(ASet);
  if Lottos = nil then Result := '[]'
  else begin
    Result := '[';
    for I := 0 to Length(Lottos)-1 do
      Result := Result   Format('%.2d,', [Lottos[I]]);
    Result[Length(Result)] := ']';
  end;
end;    function TForm1.LottoSetToLottos(ASet: TLottoSet): TLottos;
begin
  Result := Elements(ASet);
end;    function TForm1.LottosToLottoSet(LS: TLottos): TLottoSet;
var
  I: Integer;
begin
  Result := [];
  if LS <> nil then
    for I := 0 to Length(LS)-1 do
      Result := Result   [LS[I]];
end;    procedure TForm1.Button1Click(Sender: TObject);
var
  ASet: TLottoSet;
begin
  ASet := RandomLottoSet(6);
  ShowMessage(LottoSetToString(ASet));
end;    
系統時間:2024-05-08 19:12:32
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!