现有一个以集合为元素的集合A={{3},{0,0},{2,1},{1,2},{2,1},{2,2,1},{1,2,1},{0,2,1}.....}需要注意两点的是:1.本集合中的元素是有序的,2.同一元素允许重复出现,因为其代表的意思不同。先任意输入一个集合,如B={3,2,1,0,2}要求在输入集合A中找出是B的子集的所有元素。由于我初学Delphi基础不是很好,所以我期望高手在回复时给予适当的注释,小妹有急用,请大家帮帮忙啊!
解决方案 »
- 对象销毁问题
- 用ADO和SQL数据库连接几种现象
- 怎么不能添加数据了啊,
- 急,为什么VCLZIP不能打开从服务端压缩过来的文件?!!!!!!!
- 询救access与sqlserver一条语句的区别(在线等待中......)
- treeview为什么右键先不中
- 请问nicesoft的qllgrid怎么嵌入他的LOOKUPCOMBOBOX控件,我嵌入后无法显示出来,100分
- 聘高人帮我修改程序!高薪!
- 如何把剪切板里面的一幅超过 100M(超大尺寸)的BMP文件保存到磁盘里?
- 如何在Report Machine报表中插入个chart
- 求一个能查找目录下面所有文件的算法!先到的有分!
- 急,关于DBGrid?
Str, StrSub : String;
begin
Str :='{{3},{0,0},{2,1},{1,2},{2,1},{2,2,1},{1,2,1},{0,2,1}.....}';
Str := Stringreplace(Str,' ','',[rfReplaceAll]); //先把空格去掉
StrSub := '{3,2,1,0,2}';
StrSub := Stringreplace(StrSub,' ','',[rfReplaceAll]); //先把空格去掉 if Pos(StrSub, Str)> 0 then
ShowMessage('在集合')
else ShowMessage('不在集合')end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
function ExtractSet(var aSets: string): string;
function ExtractSubSet(const aSubSets: string; var aPos: integer): string;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
lSetA: string;
lSetB: string;
lSetTmp: string;
lSubSet: string;
lPos: integer;
begin
lSetA := '{{3},{0,0},{2,1},{1,2},{2,1},{2,2,1},{1,2,1}}';
lSetB := '{3,2,1,0,2}';
lSetTmp := Copy(lSetA, 2, Length(lSetA) - 1);
while lSetTmp <> '' do
begin
lSubSet := ExtractSet(lSetTmp);
lPos := 1;
while lPos <= Length(Copy(lSubSet, 2, Length(lSubSet) - 2)) do
// 在這裡判斷
showmessage(ExtractSubSet(Copy(lSubSet, 2, Length(lSubSet) - 2), lPos));
end;
end;function TForm1.ExtractSet(var aSets: string): string;
var
i: Integer;
begin
i := pos('}', aSets);
Result := Trim(Copy(aSets, 1, i));
aSets := Copy(aSets, i + 2, Length(aSets) - i - 1);
end;function TForm1.ExtractSubSet(const aSubSets: string; var aPos: integer): string;
var
i: Integer;
begin
i := aPos;
while (i <= Length(aSubSets)) and (aSubSets[i] <> ',') do Inc(i);
Result := Trim(Copy(aSubSets, aPos, i - aPos));
if (i <= Length(aSubSets)) and (aSubSets[i] = ',') then Inc(i);
aPos := i;
end;end.我要下班啦, 星期一幫你寫啦.我已經把數字分解出來啦, 你只要判斷是否是了串就可以啦.
procedure TForm1.Button1Click(Sender: TObject);
type
TSomeInts = 1..250;
TIntSet = set of TSomeInts;
var
A : array of TIntSet;
B : TIntSet;
i : Integer;
begin
//初始化A,B
SetLength(A,2);
A[0] := [88,99];
A[1] := [9,6,77,55];
B := [9,6,77,55,88,66]; //遍历A
for i:=Low(A) to High(A) do
begin
if A[i]<=B then //如果A[i]是B的一个子集
ShowMessage('找到B的一个子集');
end;
end;
再说的详细一些这是Delphi help中对于集合操作符的说明
在索引里输入set operators能够搜到
----------------------------------------------------The following operators take sets as operands.Set operators
Operator Operation Operand types Result type Example
+ union set set Set1 + Set2
- difference set set S - T
* intersection set set S * T
<= subset set Boolean Q <= MySet
>= superset set Boolean S1 >= S2
= equality set Boolean S2 = MySet
<> inequality set Boolean MySet <> S1
in membership ordinal, set Boolean A in Set1
The following rules apply to +, -, and *.An ordinal O is in X + Y if and only if O is in X or Y (or both). O is in X - Y if and only if O is in X but not in Y. O is in X * Y if and only if O is in both X and Y.
The result of a +, -, or * operation is of the type set of A..B, where A is the smallest ordinal value in the result set and B is the largest.The following rules apply to <=, >=, =, <>, and in.X <= Y is True just in case every member of X is a member of Y; Z >= W is equivalent to W <= Z. U = V is True just in case U and V contain exactly the same members; otherwise, U <> V is True.
For an ordinal O and a set S, O in S is True just in case O is a member of S.
//uses
// Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
// Dialogs, StdCtrls, StrUtils;procedure TForm1.Button1Click(Sender: TObject);
Var
All,One:String;
AllStrings,All_OneStrings,OneStrings,ResultStrings:TStringList;
I,J:Integer;
IsRight : Boolean;
begin
ResultStrings := TStringList.Create; //用于返回结果
//主字符串(以下称主串)
All := '{{3,*,*,*,*},{0,*,*,0,*},{0,*,0,*,*},{*,*,1,2,*},{1,1,*,*,2}}';
All := AnsiReplaceStr(All,'{{','');
All := AnsiReplaceStr(All,'}}','');
All := AnsiReplaceStr(All,'},{',';');
//All 被转化成 3,*,*,*,*;0,*,*,0,*;0,*,0,*,*;*,*,1,2,*;1,1,*,*,2
AllStrings := TStringList.Create; //所有规则集合
AllStrings.Delimiter := ';'; //以;号拆分字符串
AllStrings.DelimitedText := All; //将主串分成5个子字符串
//要查找的子字符串(以下称子串)
One :='{3,1,1,2,2}';
One := AnsiReplaceStr(One,'{','');
One := AnsiReplaceStr(One,'}','');
//One 被转化成 3,1,1,2,2
OneStrings := TStringList.Create; //要查找的集合
OneStrings.Delimiter := ','; //以,号拆分字符串
OneStrings.DelimitedText := One; //将子串分成5个单元分别为 3 1 1 2 2 All_OneStrings := TStringList.Create; //用来存放主串中的其中一个子串,以便匹配(下称 主_子串)
For I := 0 to AllStrings.Count -1 do //遍历所有主串元素
begin
All_OneStrings.Clear;
All_OneStrings.Delimiter := ','; //将主_子串以,号分拆成5个单元
All_OneStrings.DelimitedText := AllStrings.Strings[I];
IsRight := True;
For J := 0 to All_OneStrings.Count -1 do //对 主_子串 和 子串 每个元素进行匹配
begin
if IsRight then
begin
//如果 主_子串中对应无素为 * 则匹配成功一位元素 因为了串不论是什么都算匹配
if All_OneStrings.Strings[J]='*' then IsRight := True
//如果 主_子串 与 子串 元素一致 则匹配成功一位元素
else if All_OneStrings.Strings[J]=OneStrings.Strings[J] then IsRight := True
//其它情况则匹配不成功
else IsRight := False;
end else
begin
//只要有一位不匹配则跳出,进行下一个主_子串进行匹配
Break;
end;
end;
// 完成一个主_子串匹配,如果成功则加入结果集里
if IsRight then ResultStrings.Add(AllStrings.Strings[I]); end;
//全部匹配完成后显示结果集内容
ShowMessage(ResultStrings.Text);
end;
返回:
3,*,*,*,*
*,*,1,2,*