基于树状图的族谱程序

功能:初始建立一个祖先

在树状图里可以任意切换到一个人,使他生孩子

杀人只有祖先有权利,杀掉后,他的子女也死了

可以查找族里容易一个孩子

可以查看全族

源代码

#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();
}
}
}


  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,我是一个AI语言模型,无法为您编写完整的Vue3族谱树状图代码。但是,我可以给您提供一些基本的思路和建议: 1. 确定数据结构:族谱树状图需要一个树形结构的数据,每个节点包含该人物的基本信息和他们的子女和父亲信息。可以使用一个对象或数组来表示每个节点,例如: ``` { name: '张三', gender: 'male', father: { name: '李四', gender: 'male' }, children: [ { name: '小红', gender: 'female' }, { name: '小明', gender: 'male', children: [ { name: '小刚', gender: 'male' }, { name: '小丽', gender: 'female' } ] } ] } ``` 2. 定义组件:族谱树状图需要至少两个组件,一个是节点组件,用于显示每个人物的信息和子女节点,另一个是树组件,用于显示整个家族树的结构。 3. 树组件递归显示节点:树组件需要递归显示每个节点和其子节点。可以使用`v-for`指令和递归组件来实现: ``` <template> <div> <node v-for="child in node.children" :node="child" :key="child.name" /> </div> </template> <script> import Node from './Node.vue' export default { name: 'Tree', components: { Node }, props: { node: Object } } </script> ``` 4. 节点组件显示节点信息:节点组件需要显示每个人物的信息和子女节点。可以使用`v-if`指令来判断该节点是否有子女,如果有则显示子节点。 ``` <template> <div> <div>{{ node.name }}</div> <div>{{ node.gender }}</div> <div v-if="node.children"> <tree :node="node" /> </div> </div> </template> <script> import Tree from './Tree.vue' export default { name: 'Node', components: { Tree }, props: { node: Object } } </script> ``` 5. 在Vue实例中使用树组件:最后,在Vue实例中使用树组件,并将家族树数据传递给树组件。 ``` <template> <div> <tree :node="familyTree" /> </div> </template> <script> import Tree from './Tree.vue' export default { name: 'App', components: { Tree }, data() { return { familyTree: {...} // 家族树数据 } } } </script> ``` 以上是一个基本的Vue3族谱树状图的思路和建议,具体实现还需要根据具体需求进行调整和完善。希望对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值