如何取出集合或列舉形態的值 |
尚未結案
|
hkstm
一般會員 發表:23 回覆:31 積分:10 註冊:2002-03-15 發送簡訊給我 |
|
hagar
版主 發表:143 回覆:4056 積分:4445 註冊:2002-04-14 發送簡訊給我 |
{
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);
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 發送簡訊給我 |
集合問題最好用集合方式解決, 以樂透彩為例,您可以
寫一些關於集合運算的方法來處理相關需求,以下提供
個人的一些經驗:
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; |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |