如何对一个类型进行递归运算,多指教

baxp 2001-07-20 12:43:24
比如
类型 名称 父类型 等级
1 卡 0 0
2 ic卡 1 1
3 北京电信ic卡 2 2
4 ip卡 1 1
5 北京电信ip卡 4 2
6 北京电信ic卡50元 3 3
7 北京电信ic卡30元 3 3
8 北京电信ip卡50元 5 3
9 北京电信ip卡30元 5 3

我如何输入一个类型代号,就能得到所有属于该类型以及它的子类型的名称,请帮忙
...全文
136 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
baxp 2001-07-20
  • 打赏
  • 举报
回复
leeyansheng(风妖精)
gz_xjf(thinker)
两位给我一个例子看看好吗?
马上给分
直接贴出来或者发到
baxp@yeah.net
chechy 2001-07-20
  • 打赏
  • 举报
回复
唉,又看错了。不过递归的思路都是差不多的。
chechy 2001-07-20
  • 打赏
  • 举报
回复
procedure a(id: Integer)
var
pid: Integer;
begin
Locate(id);
pid := it's 父类型;
if pid = 0 then
exit; // 根类型
write pid; // 显示父类型
a(pid);
end;
gz_xjf 2001-07-20
  • 打赏
  • 举报
回复
树的遍历我有源码例子
gz_xjf 2001-07-20
  • 打赏
  • 举报
回复
如果是一棵树的话,遍历一下不就行了吗
leeyansheng 2001-07-20
  • 打赏
  • 举报
回复
可以用存储过程吗?
如果需要我可以送上代码。
baxp 2001-07-20
  • 打赏
  • 举报
回复
最好有个例子
baxp 2001-07-20
  • 打赏
  • 举报
回复
kkkkkkkk
guig 2001-07-20
  • 打赏
  • 举报
回复
具体问题具体分析,这还不用递归。
看看下面的例子:

//********* Unit1.dfm ***********
object Form1: TForm1
Left = 165
Top = 155
Width = 579
Height = 415
Caption = 'Form1'
Color = clBtnFace
Font.Charset = GB2312_CHARSET
Font.Color = clWindowText
Font.Height = -13
Font.Name = '宋体'
Font.Style = []
OldCreateOrder = False
Position = poScreenCenter
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 149
Top = 13
Width = 73
Height = 13
Caption = '<==先点这里'
end
object Label2: TLabel
Left = 181
Top = 53
Width = 164
Height = 13
Caption = '<==然后在这里输入一个类型'
end
object Button1: TButton
Left = 24
Top = 8
Width = 113
Height = 25
Caption = 'Create Table'
TabOrder = 0
OnClick = Button1Click
end
object DBGrid1: TDBGrid
Left = 0
Top = 192
Width = 571
Height = 196
Align = alBottom
DataSource = DataSource1
TabOrder = 1
TitleFont.Charset = GB2312_CHARSET
TitleFont.Color = clWindowText
TitleFont.Height = -13
TitleFont.Name = '宋体'
TitleFont.Style = []
end
object Memo1: TMemo
Left = 379
Top = 0
Width = 192
Height = 192
Align = alRight
ScrollBars = ssBoth
TabOrder = 2
end
object Edit1: TEdit
Left = 24
Top = 48
Width = 153
Height = 21
TabOrder = 3
end
object Button2: TButton
Left = 24
Top = 88
Width = 75
Height = 25
Caption = 'FindCard'
TabOrder = 4
OnClick = Button2Click
end
object Button3: TButton
Left = 128
Top = 88
Width = 129
Height = 25
Caption = 'FindCard Depth'
TabOrder = 5
OnClick = Button3Click
end
object Table1: TTable
DatabaseName = '.\'
TableName = 'test'
Left = 440
Top = 8
object Table1type: TIntegerField
FieldName = '类型'
end
object Table1name: TStringField
FieldName = '名称'
Size = 40
end
object Table1parent: TIntegerField
FieldName = '父类型'
end
object Table1level: TIntegerField
FieldName = '等级'
end
end
object DataSource1: TDataSource
DataSet = Table1
Left = 480
Top = 8
end
end

//********* Unit1.pas ***********
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, DBTables, StdCtrls, Grids, DBGrids;

type
TForm1 = class(TForm)
Table1: TTable;
Table1type: TIntegerField;
Table1name: TStringField;
Table1parent: TIntegerField;
Table1level: TIntegerField;
Button1: TButton;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Memo1: TMemo;
Edit1: TEdit;
Button2: TButton;
Button3: TButton;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

function GetCard(Ds:TDataset;Cardid:integer;ts:TStrings):Boolean;
var
ind,NowParentId:integer;
begin
ts.Clear;
ds.DisableControls;
try
ds.First;
Result := ds.Locate('类型',CardId,[]);
if Result then
begin
ts.AddObject(Ds.fieldbyname('名称').asstring,TObject(Cardid));
ind := 0;
NowParentId := Cardid;

repeat
Ds.Filter := Format('[父类型] = %d',[NowParentId]);
Ds.Filtered := true;

Ds.first;
while not Ds.eof do
begin
ts.AddObject(Ds.fieldbyname('名称').asstring,
TObject(Ds.fieldbyname('类型').asinteger));
Ds.Next;
end;

inc(ind);
if ind >= ts.Count then break;

NowParentId := Integer(ts.Objects[ind]);

until (false);

end;

finally
ds.Filtered := false;
ds.EnableControls;
end;

end;

function GetCardDepth(Ds:TDataset;Cardid:integer;ts:TStrings):Boolean;
var
ind,NowParentId:integer;
begin
ts.Clear;
ds.DisableControls;
try
ds.First;
Result := ds.Locate('类型',CardId,[]);
if Result then
begin
ts.AddObject(Ds.fieldbyname('名称').asstring,TObject(Cardid));
ind := 0;
NowParentId := Cardid;

repeat
Ds.Filter := Format('[父类型] = %d',[NowParentId]);
Ds.Filtered := true;

Ds.first;

inc(ind);
while not Ds.eof do
begin
ts.InsertObject(ind,Ds.fieldbyname('名称').asstring,
TObject(Ds.fieldbyname('类型').asinteger));
Ds.Next;
end;

if ind >= ts.Count then break;

NowParentId := Integer(ts.Objects[ind]);

until (false);

end;

finally
ds.Filtered := false;
ds.EnableControls;
end;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Table1.DatabaseName := ExtractFilePath(ParamStr(0));

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Table1.close;
Table1.CreateTable;
Table1.open;
with Table1 do
begin
AppendRecord([1,'卡',0,0]);
AppendRecord([2,'ic卡',1,1]);
AppendRecord([3,'北京电信ic卡',2,2]);
AppendRecord([4,'ip卡',1,1]);
AppendRecord([5,'北京电信ip卡',4,2]);
AppendRecord([6,'北京电信ic卡50元',3,3]);
AppendRecord([7,'北京电信ic卡30元',3,3]);
AppendRecord([8,'北京电信ip卡50元',5,3]);
AppendRecord([9,'北京电信ip卡30元',5,3]);
end;

end;

procedure TForm1.Button2Click(Sender: TObject);
var
Ts:TStrings;
CardId:Integer;
begin
if edit1.Text = '' then exit;
Cardid := StrToInt(Trim(Edit1.text));
ts := TStringList.Create;
try
GetCard(Table1,Cardid,ts);
Memo1.Lines.Assign(ts);
finally
ts.free;
end;

end;

procedure TForm1.Button3Click(Sender: TObject);
var
Ts:TStrings;
CardId:Integer;
begin
if edit1.Text = '' then exit;
Cardid := StrToInt(Trim(Edit1.text));
ts := TStringList.Create;
try
GetCardDepth(Table1,Cardid,ts);
Memo1.Lines.Assign(ts);
finally
ts.free;
end;

end;

end.


baxp 2001-07-20
  • 打赏
  • 举报
回复
help me
chechy 2001-07-20
  • 打赏
  • 举报
回复
呵呵,我老看错题目。如果用Dataset查子类型,比查父类型要麻烦。父类型一级一级递归上去就好查了,子类型的话,必须形成一颗树,然后遍历这个节点下的所有节点。譬如在Delphi下遍历,可以用如下代码:
procedure traveltreeNode(aTreeNode: TTreeNode);
var
i: Integer;
begin
// do treeNode thing
for i := 0 to aTreeNode.Count - 1 do
traveltreeNode(aTreeNode.Item[i])
end;
gz_xjf 2001-07-20
  • 打赏
  • 举报
回复
递归运算可参见
http://www.csdn.net/expert/topic/194/194827.shtm
gz_xjf 2001-07-20
  • 打赏
  • 举报
回复
只是一个概念,自己该吧该吧
gz_xjf 2001-07-20
  • 打赏
  • 举报
回复
用它来遍历,不过这是我很久前瞎写的一个,没有优化,但能遍历
function TForm1.FFindLastChild(TreeNode1:TTreeNode):TTreeNode;
begin
if TreeNode1.getLastChild=nil then
result:=TreeNode1
else
result:=FFindLastChild(TreeNode1);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
TreeNode1:TTreeNode;
i:integer;
begin
TreeNode1:=TreeView1.Items[0];
While TreeNode1.getNextSibling<>nil do
TreeNode1:=TreeNode1.getNextSibling;
TreeNode1:=FFindLastChild(TreeNode1);
for i:=0 to TreeNode1.AbsoluteIndex do
ShowMessage(TreeView1.Items[i].Text);
end;
inprises 2001-07-20
  • 打赏
  • 举报
回复
?
baxp 2001-07-20
  • 打赏
  • 举报
回复
多谢 chechy(chechy)
不过好像不太对 

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧