我有一段代码,有edit1、edit2和button组件;edit1是用来输入一串以逗号分隔的整形字符串;当按下button时,把edit1中的整数生成一个平衡二叉树显示在edit2中,然后释放这个二叉树。
       出现的问题:
             1.edit1里面的健壮性——如果输入的是非整形字串
                     (如:1.5、afdffsdfd.......)该如何把小数取整、
                      把字符变为“0”使用?
             2.二叉树释放问题:
                       我的代码中二叉树好象没有释放,                           请各位给出相关代码????代码如下:
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  binary_tree = ^tree_node;
     tree_node = record
        lchild,rchild:binary_tree;
        value:integer;
  end;  
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    Edit2: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure crt_tree(var root:binary_tree;i:integer);
    procedure crt_node(val:integer;var p:binary_tree);
    procedure insert_node(p:binary_tree;var q:binary_tree);    procedure inorder_tree(root:binary_tree;var s:string);
    procedure dispose_tree(var root:binary_tree);
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}
//-----------------------------------------------------------------
{构造一个树}
procedure TForm1.crt_tree(var root:binary_tree;i:integer);
var
  pt,pr:binary_tree;
begin
  if root=nil then
  begin
    crt_node(i,root);  //CRT ROOT NODE
    pt:=root;
  end
  else
  begin
     crt_node(i,pr);    //CRT SUB NODE
     pt:=root;           //PT IS ROOT
     insert_node(pr,pt);    //INSERT A NODE TO BINARY TREE;
  end;
end;
//----------------------------------------------------------------------------
{构造一个结点}
procedure TForm1.crt_node(val:integer;var p:binary_tree);
begin
   new(p);
   p^.value:=val;
   p^.lchild:=nil;
   p^.rchild:=nil;
end;
//------------------------------------------------------------------------
{在平衡二插树中插入一个节点}
procedure TForm1.insert_node(p:binary_tree;var q:binary_tree);
begin
    if q=nil then q:=p
    else if q^.value > p^.value then //
         begin
             insert_node(p,q^.lchild); //INSERT LEFT CHILD TREE
         end
         else insert_node(p,q^.rchild); //INSERT RIGHT CHILD TREE
end;
//---------------------------------------------------------------------
{中序遍历 平衡二插树}
procedure TForm1.inorder_tree(root:binary_tree;var s:string);
begin
    if root <> nil then
    begin
        inorder_tree(root^.lchild,s);
        s:=s+','+inttostr(root^.value);
        inorder_tree(root^.rchild,s);
    end;
end;
//--------------------------------------------------------------------procedure TForm1.dispose_tree(var root:binary_tree);
begin
   if root<>nil then
   begin
       dispose_tree(root^.lchild);
       dispose_tree(root^.rchild);
       dispose(root);
   end;
end;//------------------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
var
   s,stemp,sShow:string;         //sshow is string, will be show in edit2
   itemp,iIndex,strlen:integer;     //iIndex is postion of ','
   proot:binary_tree;              //root of the tree
begin
    sshow:='';                 //init sshow
    s:=edit1.Text+',';          //get thevalue from edit1.text
    strlen:=length(s);    //从edit1d.text中解析出int变量,生成平衡二插树
    while strlen>0 do
    begin
       iindex:=pos(',',s);
       stemp:=copy(s,1,iIndex-1);
       itemp:=strtoint(stemp);
       delete(s,1,iIndex);
       strlen:=length(s);
       crt_tree(proot,itemp);   //crt binary tree
   end;
    //中序显始,
   inorder_tree(proot,sShow);   //inorder the tree
   delete(sShow,1,1);           //delete ',' from sShow
   edit2.Text:=sshow;
   // 后序释放
   dispose_tree(proot);          //dispose the tree   后序遍历
   if proot<>nil then beep;        //test proot is be dispose;
end;
 //-------------------------------------------------------------------------
end.

解决方案 »

  1.   

    if Key in ['A'..'Z', 'a'..'z'] then
      Key = #$30 (*好像是十六制的零是三零*)
      

  2.   

    我想使用tyr...finally...来处理非整型输入的问题
      

  3.   

    有什么问题?可以用完全可以用Key来做,,不必要try...finally...end
      

  4.   

    是不是要遍历一遍stemp;
    能不能给出完整的代码;
      

  5.   

    刚写的一段代码,测试不能通过。
    能不能帮忙修改一下;
           for i=0 to index-1 do
           begin
              key:=stemp[i];
              if Key in ['A'..'Z', 'a'..'z'] then
                     Key = #$30 {*好像是十六制的零是三零*}
               stmp:='0';
               break;
           end;          thanks!!!!!!!!!!!!!