1. 程式人生 > >基於樹狀圖的族譜程式

基於樹狀圖的族譜程式

功能:初始建立一個祖先

在樹狀圖裡可以任意切換到一個人,使他生孩子

殺人只有祖先有權利,殺掉後,他的子女也死了

可以查詢族裡容易一個孩子

可以檢視全族

原始碼

#include<stdio.h>
#include<stdlib.h>
typedef struct treechain
{
     struct treechain *father;        //指向父節點的指標
     char name[20];                  //為該結構體命名
     int  gen;                      //記錄該節點在樹狀圖中的層數
     struct treechain *children[];  //指向兒子節點的指標陣列
}tree;
tree t1;                           //設定一個儲存在資料段的根節點
tree *head=&t1,*tail=NULL,*now=&t1;  //設定三個全域性指標,head永遠指向根,tail靈活使用,now代表當前所在節點  
int genum=0;
void addchild(tree *a)              //使a指向的節點增加一個兒子節點
{
    tree *b;
    b=(tree *)malloc(sizeof(tree)+sizeof(void *)*100);   //為柔性陣列和結構體提供空間
    printf("please enter your child name:");
    fgets(b->name,20,stdin);
    b->father=a;
    b->children[0]=NULL;             
    b->gen=(a->gen)+1;
    if( (b->gen) >genum )
        genum=b->gen;
    int c=0;
    while(a->children[c]!=NULL)
        c++;
    a->children[c]=b;
    c++;
    a->children[c]=NULL;
}
int seefa(tree *a)        //檢視a指向節點的父親節點
{
      if(NULL==a->father)
      {  
          printf("you are great mother of the gods\n");
          return 1;
      }
      else
      {
          a=a->father;
          printf("your father is %s",a->name);
          return 0;
      }
}
int seech(tree *a)     //檢視a指向節點的兒子節點們
{
 tree *b;
      if(NULL==a->children[0])
      {    
          printf("you have no children\n");
          return -1;
      }
      int c=0;
      while(NULL!=a->children[c])
      {
          printf("your children name is %s",a->children[c]);
          c=c+1;
      }
      return 0;
}
int bijiao(char *a, tree *b)   //比較a,b字串是否相同,相同則輸出b的父親和兒子
{
   if(!strcmp(a,b->name))
   {
       printf("name: %s",b->name);
       seefa(b);
       seech(b);
       return 0;
   }
   return 1;
}


int seach()     //查詢樹狀圖的一個節點,輸入該節點名字,輸出該節點的父親和兒子
{
    char a[20];
    printf("please enter seach name:");
    fgets(a,20,stdin);
    tail=head;
    tree *x;
    int c=0,i,j,k;
    while(NULL!=tail->children[0])   
        tail=tail->children[0];
    bijiao(a,tail);
    while(1)
    {
        if(tail->father!=NULL)
        {
          x=tail;k=0,c=0;
          tail=tail->father;
          while(tail->children[c]!=x)
              c++;
          if(tail->children[c+1]==NULL)
          {
              bijiao(a,tail);
              continue;
          }
          if(tail->children[c+1]!=NULL)
          {
               tail=tail->children[c+1];
               while(NULL!=tail->children[0])
                   tail=tail->children[0];
               bijiao(a,tail);
          }
        }
        if(tail->father==NULL)
        break;
    }
}
int switchtree()    //切換節點,可以使now指向任意節點
{
    char a[20];
    printf("please enter toggle name:");
    fgets(a,20,stdin);
    tail=head;
    tree *x;
    int c=0,i,j,k,su=2;
    while(NULL!=tail->children[0])
        tail=tail->children[0];
    su=2;
    su=bijiao(a,tail);
    if(0==su)
    {
       char xuanze[2];
       printf("Do you went to switch to that person?Y or N:");
       fgets(xuanze,2,stdin);
       if(xuanze[0]=='Y')
       now=tail;
       return 0;
    }
    while(1)
    {
        if(tail->father!=NULL)
        {
          x=tail;k=0,c=0;
          tail=tail->father;
          while(tail->children[c]!=x)
              c++;
          if(tail->children[c+1]==NULL)
          {
             su=2;
             su=bijiao(a,tail);
             if(0==su)
             {


char xuanze[2];
                 printf("Do you went to switch to that person?Y or N:");
                 fgets(xuanze,2,stdin);
                 if(xuanze[0]=='Y')
                 {
                     now=tail;
                     return 0;
                 }
               }
                 continue;
              }
          if(tail->children[c+1]!=NULL)
          {
               tail=tail->children[c+1];
               while(NULL!=tail->children[0])
                   tail=tail->children[0];
              su=2;
              su= bijiao(a,tail);
              if(0==su)
              {
                  char xuanze[2];
                  printf("Do you went to switch to that person?Y or N:");
                  fgets(xuanze,2,stdin);
                  if(xuanze[0]=='Y')
                  now=tail;
                  return 0;


               }
           }
        }
        if(tail->father==NULL)
        break;
    }
}
int killgad(tree *gd)  //刪除gd指向的節點,和他下面的節點
{
    tree *x2;
    int op=0;
    if(gd==head)
    {
        printf("you do not kill mother of god\n");
getchar();
getchar();
return 0;
    }
    x2=gd->father;
    while(x2->children[op]!=gd)
op++;
    while(x2->children[op+1]!=NULL)
    {
x2->children[op]=x2->children[op+1];
op++;
    }
    x2->children[op]=NULL;
    gd->father=NULL;   
    tail=gd;
    tree *x;
    int c=0;
    while(NULL!=tail->children[0])
        tail=tail->children[0];
    while(1)
    {
        if(tail->father!=NULL)
        {
          x=tail,c=0;
          tail=tail->father;
          while(tail->children[c]!=x)
              c++;
          free(x);
          if(tail->children[c+1]==NULL)
              continue;    
          if(tail->children[c+1]!=NULL)
          {
               tail=tail->children[c+1];
               while(NULL!=tail->children[0])
                   tail=tail->children[0];
          }
        }
        if(tail->father==NULL)
        {
            free(tail);
            return 0;
        }
   }
}
int killgads()  //查詢要刪除的節點
{
    char a[20];
    printf("please enter kill name:");
    fgets(a,20,stdin);
    tail=head;
    tree *x;
    int c=0,i,j,k,su=2;
    while(NULL!=tail->children[0])
        tail=tail->children[0];
    su=2;
    su=bijiao(a,tail);
    if(0==su)
    {
       char xuanze[2];
       printf("Do you went to kill to that person?Y or N:");
       fgets(xuanze,2,stdin);
       if(xuanze[0]=='Y')
           killgad(tail);
       return 0;
    }
    while(1)
    {
        if(tail->father!=NULL)
        {
          x=tail;k=0,c=0;
          tail=tail->father;
          while(tail->children[c]!=x)
              c++;
          if(tail->children[c+1]==NULL)
          {
             su=2;
             su=bijiao(a,tail);
             if(0==su)
             {


char xuanze[2];
                 printf("Do you went to kill to that person?Y or N:");
                 fgets(xuanze,2,stdin);
                 if(xuanze[0]=='Y')
                 {
                     killgad(tail);
                     return 0;
                 }
               }
                 continue;
              }
          if(tail->children[c+1]!=NULL)
          {
               tail=tail->children[c+1];
               while(NULL!=tail->children[0])
                   tail=tail->children[0];
              su=2;
              su= bijiao(a,tail);
              if(0==su)
              {
                  char xuanze[2];
                  printf("Do you went to kill to that person?Y or N");
                  fgets(xuanze,2,stdin);
                  if(xuanze[0]=='Y')
 {  
                      killgad(tail);
                      return 0;
 }


               }
           }
        }
        if(tail->father==NULL)
        break;
    }
}
int bijiao1(int a,tree *b)
{
    if(a==b->gen)
    {
        seefa(b);
        printf("%s",b->name);
        return 0;
    }
    return 1;


}
int putmap(int wgen)     //輸出全代
{
    tail=head;
    tree *x;
    int c=0,i,j,k;
    while(NULL!=tail->children[0])   
        tail=tail->children[0];
    bijiao1(wgen,tail);
    while(1)
    {
        if(tail->father!=NULL)
        {
          x=tail;k=0,c=0;
          tail=tail->father;
          while(tail->children[c]!=x)
              c++;
          if(tail->children[c+1]==NULL)
          {
              bijiao1(wgen,tail);
              continue;
          }
          if(tail->children[c+1]!=NULL)
          {
               tail=tail->children[c+1];
               while(NULL!=tail->children[0])
                   tail=tail->children[0];
               bijiao1(wgen,tail);
          }
        }
        if(tail->father==NULL)
        break;
    }
    return 0;
}
int putmmp()
{
    int i;
    for(i=0;i<=genum;i++)
    {
        printf("這是第%d代\n",i);
        putmap(i);
    }
}




int main()
{
system("clear");   
t1.father=NULL;   //根節點無父親節點
printf("enter mother of god name:");  
fgets(t1.name,20,stdin);
t1.children[0]=NULL;
t1.gen=0;
    char nb[20];
    while(1)
    {
system("clear");
         printf("      (當前節點的名字)hello %s\n",now->name);     //當前你在節點的名字
         printf("      Now you have the following choices:\n\n\n");   
         printf("      1:see your father\n");       //檢視自己的父親
         printf("      2:see your children\n");    //檢視自己的兒子
         printf("      3:search your relatives(檢視樹狀圖成員)\n");   //檢視親戚
         printf("      4:give birth to a child(生孩子)\n");  //生孩子
         printf("      5:toggle character(切換節點)\n");   //切換節點
         printf("      6:kill gads(只有根節點有許可權)\n");        //只有根節點有許可權刪除成員
         printf("      7:quit\n");
printf("      8:檢視族譜\n");
         printf("      enter:");
         fgets(nb,3,stdin);
         if(nb[1]!='\n')
{   
system("clear");
             continue;
getchar();
}

         if(nb[0]=='1')
{   
system("clear");
             seefa(now); 
getchar();
}
         if(nb[0]=='2')
{   
system("clear");
             seech(now);
getchar();
}
if(nb[0]=='3')
{   
system("clear");
             seach();    
getchar();
}
         if(nb[0]=='4')
{   
system("clear");
                         addchild(now);
}
         if(nb[0]=='5')
{   
system("clear");
                         switchtree();
getchar();
}
         if(nb[0]=='6')
{   
system("clear");
if(now!=head)
continue;
killgads();
}
         if(nb[0]=='7')
{   
system("clear");
break;
}
         if(nb[0]=='8')
{   
system("clear");
                         putmmp();
getchar();
}
}
}