以文本方式查看主题

-  计算机科学论坛  (http://bbs.xml.org.cn/index.asp)
--  『 C/C++编程思想 』  (http://bbs.xml.org.cn/list.asp?boardid=61)
----  [讨论]为何求深度会出现时会出现debug error!  (http://bbs.xml.org.cn/dispbbs.asp?boardid=61&rootid=&id=17842)


--  作者:oceanblue
--  发布时间:5/3/2005 1:04:00 PM

--  [讨论]为何求深度会出现时会出现debug error!
#define HEAD 0
#define INTGR 1             
#define CH 2
#define LST 3
#define NULL 0
#include <iostream.h>
#include <stdlib.h>
#include<string.h>
class GenList;
class GenListNode
{
 friend class GenList;
    private:
 int utype;
 GenListNode *tlink;
 union
 {
  int ref;
  int intginfo;
  char charinfo;
  GenListNode *hlink; }value;
 public:
    
 
  int nodetype(GenListNode *elem)
  {
   return elem->utype;
  }
          int Getvalue()const
  {
          return utype;
  }
  
};
class GenList
{
private:    GenListNode* first;

 void Remove(GenListNode *ls);
public:   friend ostream &operator<<(ostream& out,const GenList &outlist);

 //friend int operator==(const GenList &l,const GenList &m);
 GenList();

 GenListNode &Head();
 GenListNode &Tail();
 GenListNode *First();
 GenListNode *Next(GenListNode *elem);
 void Push(GenListNode &x);   virtual int depth(GenListNode *ls);

 int Createlist(GenListNode *&ls,char *s);
 int sever(char *&str1,char *&hstr1);
};
GenList::GenList()
{
 GenListNode *first=new GenListNode;
 first->utype=0;
 first->value.ref=1;
 first->tlink=NULL;
 
}
GenListNode& GenList::Head()
{
 if(first->tlink==NULL)
 {
  cout<<"Illegal head operation."<<endl;
  exit(1);
 }
 else
 {
  GenListNode *temp=new GenListNode;
  temp->utype=first->tlink->utype;
  temp->value=first->tlink->value;
  return *temp;
 }
}
GenListNode& GenList::Tail()
{
 if(first->tlink==NULL)
 {
  cout<<"Illegal tail operation."<<endl;
  exit(1);
 }
 else
 {
  GenList*temp=new GenList;
  temp->first->tlink=first->tlink->tlink;
  return *temp->first;
 }
}
GenListNode *GenList::First()
{
 if(first->tlink==NULL)return NULL;
 else return first->tlink;
}
GenListNode *GenList::Next(GenListNode *elem)
{
 if(elem->tlink==NULL)return NULL;
 else return elem->tlink;
}
void GenList::Push(GenListNode &x)
{
 if(first->tlink==NULL)first->tlink=x.tlink;
 else
 {
  x.tlink=first->tlink;
  first->tlink=x.tlink;
  
 }
}

int GenList::depth(GenListNode *ls)

{

 if(ls->tlink==NULL)return 1;

 GenListNode *temp=ls->tlink;

 int m=0;

 while(temp!=NULL)

 {

  if(temp->utype==LST)

  {

   int n=depth(temp->value.hlink);

   if(m<n)m=n;

  }

  temp=temp->tlink;

 }

 return m+1;

}

int GenList::Createlist(GenListNode * &ls,char *s)
{
    char *sub,*hsub;

 ls=new GenListNode();
 ls->utype=HEAD;
 ls->value.ref=1;
 
 if(strlen(s)==0||!strcmp(s,"()"))ls->tlink=NULL;
 else
 {    
  sub=new char[strlen(s)-2];        
                strncpy(sub,s+1,strlen(s)-2);        
  
  GenListNode *p=ls;

  while(strlen(sub)!=0)

  {

   p=p->tlink=new GenListNode();

   if(sever(sub,hsub))

   {    
                 
    
    if(hsub[0]!='('&&hsub[0]!='\'')
    {

     p->utype=INTGR;
                  
      p->value.intginfo=atoi(hsub);
        cout<<"free digital"<<p->value.intginfo<<endl;
                   
    }

    else if(hsub[0]!='('&&hsub[0]=='\'')

    {
     p->utype=CH;
                   
     p->value.charinfo=hsub[1];
     
     cout<<"free char"<<p->value.charinfo<<endl;
    }

    else

    {
                   
     p->utype=LST;
            
     Createlist(p->value.hlink,hsub);

    }

   }

   else return 0;

  }

  p->tlink=NULL;

     
 
 }

 return 1;
 
}
int GenList::sever(char *&str1,char * &hstr1)

{

 char ch=str1[0];

 int n=strlen(str1);

 int i=0,k=0;
    
 while(i<n&&(ch!=','||k!=0))

 {

  if(ch=='(')k++;

  else if(  ch == ')')k--;

  i++;

  ch=str1[i];

 }

 if(i<n)
 {   
  hstr1=new char[i];

  strncpy( hstr1 , str1 , i);
          
  strncpy(str1 , str1+i+1 , n-i+1 );

       cout<<hstr1<<endl;

  return 1;

 }
 else if(k!=0)return 0;
 
 else
 {
     hstr1=new char[strlen(str1)+1];
  
  strcpy( hstr1 , str1 );
        
  str1[0] = '\0';

  return 1;
 }

 

}

void main()
{
char *str="(2,('b',7),(3),('d'))";
GenList aa;
GenListNode * bb;
aa.Createlist(bb,str);
cout<<aa.depth(bb);
}


--  作者:oceanblue
--  发布时间:5/3/2005 1:07:00 PM

--  
欢迎大家来讨论!
--  作者:oceanblue
--  发布时间:5/5/2005 9:45:00 AM

--  
#define HEAD 0
#define INTGR 1             
#define CH 2
#define LST 3
#define NULL 0
#include <iostream.h>
#include <stdlib.h>
#include<string.h>
class GenList;
class GenListNode
{
 friend class GenList;
    private:
 int utype;
 GenListNode *tlink;
 union
 {
  int ref;
  int intginfo;
  char charinfo;
  GenListNode *hlink; }value;
 public:
    
 
  int nodetype(GenListNode *elem)
  {
   return elem->utype;
  }
          int Getvalue()const
  {
          return utype;
  }
  
};
class GenList
{
private:    GenListNode* first;

 void Remove(GenListNode *ls);
public:   friend ostream &operator<<(ostream& out,const GenList &outlist);

 //friend int operator==(const GenList &l,const GenList &m);
 GenList();

 GenListNode &Head();
 GenListNode &Tail();
 GenListNode *First();
 GenListNode *Next(GenListNode *elem);
 void Push(GenListNode &x);   virtual int depth(GenListNode *ls);

 int Createlist(GenListNode *&ls,char *s);
 int sever(char *&str1,char *&hstr1);
};
GenList::GenList()
{
 GenListNode *first=new GenListNode;
 first->utype=0;
 first->value.ref=1;
 first->tlink=NULL;
 
}
GenListNode& GenList::Head()
{
 if(first->tlink==NULL)
 {
  cout<<"Illegal head operation."<<endl;
  exit(1);
 }
 else
 {
  GenListNode *temp=new GenListNode;
  temp->utype=first->tlink->utype;
  temp->value=first->tlink->value;
  return *temp;
 }
}
GenListNode& GenList::Tail()
{
 if(first->tlink==NULL)
 {
  cout<<"Illegal tail operation."<<endl;
  exit(1);
 }
 else
 {
  GenList*temp=new GenList;
  temp->first->tlink=first->tlink->tlink;
  return *temp->first;
 }
}
GenListNode *GenList::First()
{
 if(first->tlink==NULL)return NULL;
 else return first->tlink;
}
GenListNode *GenList::Next(GenListNode *elem)
{
 if(elem->tlink==NULL)return NULL;
 else return elem->tlink;
}
void GenList::Push(GenListNode &x)
{
 if(first->tlink==NULL)first->tlink=x.tlink;
 else
 {
  x.tlink=first->tlink;
  first->tlink=x.tlink;
  
 }
}

int GenList::depth(GenListNode *ls)

{

 if(ls->tlink==NULL)return 1;

 GenListNode *temp=ls->tlink;

 int m=0;

 while(temp!=NULL)

 {

  if(temp->utype==LST)

  {

   int n=depth(temp->value.hlink);

   if(m<n)m=n;

  }

  temp=temp->tlink;

 }

 return m+1;

}

int GenList::Createlist(GenListNode * &ls,char *s)
{
    char *sub,*hsub;

 ls=new GenListNode();
 ls->utype=HEAD;
 ls->value.ref=1;
 
 if(strlen(s)==0||!strcmp(s,"()"))ls->tlink=NULL;
 else
 {       
       sub=new char[strlen(s)-1];        
                strncpy(sub,s+1,strlen(s)-2);
    sub[strlen(s)-2]='\0';
  
  GenListNode *p=ls;

  while(strlen(sub)!=0)

  {

   p=p->tlink=new GenListNode();

   if(sever(sub,hsub))

   {    
                 
    
    if(hsub[0]!='('&&hsub[0]!='\'')
    {

     p->utype=INTGR;
                  
      p->value.intginfo=atoi(hsub);
        cout<<"free digital"<<p->value.intginfo<<endl;
                   
    }

    else if(hsub[0]!='('&&hsub[0]=='\'')

    {
     p->utype=CH;
                   
     p->value.charinfo=hsub[1];
     
     cout<<"free char"<<p->value.charinfo<<endl;
    }

    else

    {
                   
     p->utype=LST;
            
     Createlist(p->value.hlink,hsub);

    }

   }

   else return 0;

  }

  p->tlink=NULL;

     
 
 }

 return 1;
 
}
int GenList::sever(char *&str1,char * &hstr1)

{

 char ch=str1[0];

 int n=strlen(str1);

 int i=0,k=0;
    
 while(i<n&&(ch!=','||k!=0))

 {

  if(ch=='(')k++;

  else if(  ch == ')')k--;

  i++;

  ch=str1[i];

 }

 if(i<n)
 {   
  hstr1=new char[i+1];

  strncpy( hstr1 , str1 , i);
         hstr1[i]='\0';
  strncpy(str1 , str1+i+1 , n-i+1 );
        str1[n-i+1]='\0';
       cout<<hstr1<<endl;

  return 1;

 }
 else if(k!=0)return 0;
 
 else
 {
     hstr1=new char[strlen(str1)+1];
  
  strcpy( hstr1 , str1 );
        
  str1[0] = '\0';

  return 1;
 }

 

}

void main()
{
char *str="(2,('b',7),(((3)),('d')))";
GenList aa;
GenListNode * bb;
aa.Createlist(bb,str);cout<<aa.depth(bb);
}
ok,终于调出debug error


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
79.102ms